Wednesday, 7 March 2018

check box with button toast make activity in android with xml and java file

check box with button toast 
checked or unchecked

check_box.xml
check_box.java


check_box.xml preview:-



check_box.xml code :-


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <CheckBox
        android:id="@+id/A1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="A1" />

    <CheckBox
        android:id="@+id/A2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="A2" />

    <CheckBox
        android:id="@+id/A3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="A3" />

    <Button
        android:id="@+id/btn_checkbox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="submit" />
</LinearLayout>




check_box.java code :-



package com.example.store.gried_and_list_view;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

public class check_box extends AppCompatActivity {

    CheckBox A1 ,A2,A3;
    Button btn_checkbox ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        A1 = (CheckBox)findViewById(R.id.A1);
        A2 = (CheckBox)findViewById(R.id.A2);
        A3 = (CheckBox)findViewById(R.id.A3);

        btn_checkbox = (Button)findViewById(R.id.btn_checkbox);

     
     
        //for check and unchecked indivisuale 
      /*  A1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

                if(b){
                    Toast.makeText(check_box.this, A1.getText().toString() + " is checked..", Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(check_box.this, A1.getText().toString() + " is unchecked..", Toast.LENGTH_SHORT).show();
                }

            }
        });

        A2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

                if (b){
                    Toast.makeText(check_box.this, A2.getText().toString()+"is checked", Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(check_box.this,A2.getText().toString()+ "is unchecked", Toast.LENGTH_SHORT).show();
                }
            }
        });
        A3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b){
                    Toast.makeText(check_box.this,A3.getText().toString()+ "is checked..", Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(check_box.this,A3.getText().toString()+ "is unchecked", Toast.LENGTH_SHORT).show();
                }
            }
        });
        */

        btn_checkbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s= "";
                if (A1.isChecked()){
                    s=A1.getText().toString()+"is checked ...";
                }
                if (A2.isChecked()){
                    s=s+A2.getText().toString()+"is checked ...";
                }
                if (A3.isChecked()){
                    s=s+A3.getText().toString()+"is checked ...";
                }
                Toast.makeText(check_box.this, s, Toast.LENGTH_SHORT).show();
            }
        });

    }
}


video preview for that activity:-








No comments:

Post a Comment

B Remi Calculator

Loan Calculator Loan Calculator Loan Amount PF Cross Sell (LI % GI) No Yes EMI CARD No Ye...