CheckBox in Android

In this tutorial we will learn about CheckBox in Android. CheckBox is kind of button which only have two states checked or unchecked. CheckBox can be used in my different ways for example you different options and user of your app can select multiple from those options, In this kind of situations you can use CheckBox.

CheckBox in Android

CheckBox in Android Learn Android for free.


In This tutorial we will learn how to add CheckBox in Android and how to use diffent methods related to CheckBox. Firstly we will learn to CheckBox in XML.

CheckBox Example
<?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:padding="16dp" android:orientation="vertical" tools:context=".MainActivity"> <CheckBox android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/owlbuddy" android:textSize="20dp" android:id="@+id/notifications" android:layout_marginBottom="10dp" android:text="Notifications"/> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginBottom="10dp" android:background="@color/colorPrimary"/> <CheckBox android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/owlbuddy" android:id="@+id/updates" android:layout_marginBottom="10dp" android:textSize="20dp" android:text="Auto Updates"/> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginBottom="10dp" android:background="@color/colorPrimary"/> <CheckBox android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_marginBottom="10dp" android:id="@+id/history" android:src="@drawable/owlbuddy" android:textSize="20dp" android:text="Clear History"/> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginBottom="10dp" android:background="@color/colorPrimary"/> </LinearLayout>

Here is output of XML Layout File...

Check Box in Android



Now we will learn how to perform some action when someone will check these check boxes. Firstly will initialize all these check boxes in our java file with findViewById() method and then we will call to setOnCheckedChangeListener() to these Check Boxes.

CheckBox Example

public class MainActivity extends AppCompatActivity {

    CheckBox notifications,updates,history;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        notifications=(CheckBox)findViewById(R.id.notifications);
        updates=(CheckBox)findViewById(R.id.updates);
        history=(CheckBox)findViewById(R.id.history);


        notifications.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonifView, boolean isChecked) {
                if(isChecked)
                    Toast.makeText(getApplication(),"Notifications On",Toast.LENGTH_SHORT).show();
            }
        });

        updates.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)
                    Toast.makeText(getApplication(),"Auto Updates On",Toast.LENGTH_SHORT).show();
            }
        });

        history.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)
                    Toast.makeText(getApplication(),"Clear History On",Toast.LENGTH_SHORT).show();
            }
        });

    }
}




Check Box in Android



Share this tutorial with your friends on your social Media..

CheckBox in Android CheckBox in Android CheckBox in Android CheckBox in Android



Now You Can Follow All These Tutorials on Your Android Mobile Just Download This App.


Owlbuddy.com