Check Box Button Handling-Android Studio.

CheckBox Tutorial With Example In Android Studio. In AndroidCheckBox is a type of two state button either unchecked or checked in Android. Or you can say it is a type of on/off switch that can be toggled by the users. CompoundButton is the parent class of CheckBox class.

Lets write a simple example for checkBox, whenever we check the box it will display a Toast message. The code for MainActivity.java and acitivity.xml is shown below.



MainActivity.java 


  1. package com.gcrt.checkboxhandling;
  2. import android.support.v7.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.Button;
  6. import android.widget.CheckBox;
  7. import android.widget.Toast;
  8. public class MainActivity extends AppCompatActivity {
  9. Button b1;
  10. CheckBox ck1,ck2,ck3,ck4;
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_main);
  15. b1 = (Button) findViewById(R.id.button);
  16. ck1 = (CheckBox) findViewById(R.id.checkBox);
  17. ck2 = (CheckBox) findViewById(R.id.checkBox2);
  18. ck3 = (CheckBox) findViewById(R.id.checkBox3);
  19. ck4 = (CheckBox) findViewById(R.id.checkBox4);
  20. }
  21. public void click(View view) {
  22. CheckBox checkBox = (CheckBox) view;
  23. boolean isSelected = checkBox.isChecked();
  24. switch (view.getId()) {
  25. case R.id.checkBox:
  26. if (isSelected == true) {
  27. Toast.makeText(MainActivity.this, "APPLE IS SELECTED", Toast.LENGTH_SHORT).show();
  28. }
  29. break;
  30. case R.id.checkBox2:
  31. if (isSelected == true) {
  32. Toast.makeText(MainActivity.this, "MOTO G IS SELECTED", Toast.LENGTH_SHORT).show();
  33. }
  34. break;
  35. case R.id.checkBox3:
  36. if (isSelected == true) {
  37. Toast.makeText(MainActivity.this, "REDME IS SELECTED", Toast.LENGTH_SHORT).show();
  38. }
  39. break;
  40. case R.id.checkBox4:
  41. if (isSelected == true) {
  42. Toast.makeText(MainActivity.this, "NOKIA IS SELCTED", Toast.LENGTH_SHORT).show();
  43. }
  44. break;
  45. }
  46. }
  47. public void clicked(View view) {
  48. if (ck1.isChecked()== false || ck4.isChecked() == false || ck2.isChecked() == false || ck3.isChecked() == false) {
  49. Toast.makeText(MainActivity.this, "Select at least one", Toast.LENGTH_SHORT).show();
  50. }
  51. }
  52. }

acitivity.xml


  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
  4. android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
  5. android:paddingRight="@dimen/activity_horizontal_margin"
  6. android:paddingTop="@dimen/activity_vertical_margin"
  7. android:orientation="vertical"
  8. android:background="#FF9"
  9. android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
  10. <CheckBox
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:text="APPLE"
  14. android:layout_marginTop="30dp"
  15. android:textSize="60px"
  16. android:onClick="click"
  17. android:id="@+id/checkBox" />
  18. <CheckBox
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:text="MOTO G"
  22. android:layout_marginTop="30dp"
  23. android:onClick="click"
  24. android:textSize="60px"
  25. android:id="@+id/checkBox2" />
  26. <CheckBox
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. android:text="REDMI"
  30. android:layout_marginTop="20dp"
  31. android:onClick="click"
  32. android:textSize="60px"
  33. android:id="@+id/checkBox3" />
  34. <CheckBox
  35. android:layout_width="wrap_content"
  36. android:layout_height="wrap_content"
  37. android:text="NOKIA"
  38. android:layout_marginTop="20dp"
  39. android:onClick="click"
  40. android:textSize="60px"
  41. android:id="@+id/checkBox4" />
  42. <Button
  43. android:layout_width="wrap_content"
  44. android:layout_height="wrap_content"
  45. android:text="CHECK"
  46. android:onClick="clicked"
  47. android:id="@+id/button" />
  48. </LinearLayout>



For more detail checkout the video below. Do subscribe to our channel.




Hope this post is helpful,for more useful post like and share with your friends, we also have Youtube channel for more details check out the video below, do like share and subscribe to our channel it will boost our energy and help to improve our work, Have Nice Day.

/span>
Check Box Button Handling-Android Studio. Check Box Button Handling-Android Studio. Reviewed by gcrt on March 20, 2018 Rating: 5

No comments:

'; (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();
Powered by Blogger.