Android Check Box

Android Development

Android Check Box :-


⇛ layout > .xml file :

<?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"
tools:context=".First"
android:orientation="vertical"
android:gravity="center"
android:padding="10dp">

<TextView

android:id="@+id/Idtextview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Which fruit do you like ?"
android:textColor="#ae0810"
android:textStyle="italic"
android:textSize="20sp"/>

<CheckBox

android:id="@+id/Idcheckbox1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#0db719"
android:textSize="20sp"
android:text="Mango"/>

<CheckBox

android:id="@+id/Idcheckbox2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#0db719"
android:textSize="20sp"
android:text="Orange"/>

<CheckBox

android:id="@+id/Idcheckbox3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#0db719"
android:textSize="20sp"
android:text="Apple"/>

<Button

android:id="@+id/Idbutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:textColor="#ca0ed7"
android:padding="10dp"
style="@style/TextAppearance.AppCompat.Light.SearchResult.Subtitle"/>

<TextView

android:id="@+id/Idtextview2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:textStyle="bold"
android:textAlignment="center"
android:textAllCaps="false"/>

</LinearLayout>

⇛ java file :

package com.example.dell.checkbox;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;
public class First extends AppCompatActivity {

private CheckBox cb1, cb2, cb3;

private Button btn1;

private TextView txt1, txt2;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);

txt1 = (TextView) findViewById(R.id.Idtextview1);

cb1 = (CheckBox) findViewById(R.id.Idcheckbox1);

cb2 = (CheckBox) findViewById(R.id.Idcheckbox2);

cb3 = (CheckBox) findViewById(R.id.Idcheckbox3);

btn1 = (Button) findViewById(R.id.Idbutton1);

txt2 = (TextView) findViewById(R.id.Idtextview2);

/*When click on any Checkbox, the listener will execute*/
btn1.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

if(cb1.isChecked() == true && cb2.isChecked()==true && cb3.isChecked()==true){

txt2.setText("You like all fruits.");

}else if (cb1.isChecked() == true && cb2.isChecked()==true){

txt2.setText("You like Mango & Orange.");

}else if (cb1.isChecked() == true && cb3.isChecked()==true){

txt2.setText("You like Mango & Apple.");

}else if (cb2.isChecked() == true && cb3.isChecked()==true){

txt2.setText("You like Orange & Apple.");

}else if (cb1.isChecked() == true){

txt2.setText("You like Mango.");

}else if (cb2.isChecked() == true){

txt2.setText("You like Orange.");

}else if (cb3.isChecked() == true){

txt2.setText("You like Apple.");

}else {

txt2.setText("You don't like these fruits.");

}
}
});
}
}

No comments

Powered by Blogger.