CheckBox in Android

Checkbox In Android

In this tutorial we will deal with the checkbox in Android with an example. Checkboxes are widely used in Android to activate or deactivate something and to select something from a group of items.

Example:
  • Create a new project and name it as Checkbox.
  • Design a main layout as follows.
File: activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.hss_24.checkbox.MainActivity">

    <TextView
        android:text="AndroidLovers"
        android:textSize="28sp"
        android:gravity="center"
        android:padding="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:text="Please select your Sport"
        android:textSize="24sp"
        android:gravity="center"
        android:layout_marginBottom="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

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

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

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

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

    <LinearLayout
        android:orientation="horizontal"
        android:gravity="center"
        android:layout_margin="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <Button
        android:id="@+id/btn_select"
        android:text="select"
        android:layout_marginRight="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/btn_cancel"
        android:text="cancel"
        android:layout_marginLeft="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    </LinearLayout>

    <TextView
        android:id="@+id/result"
        android:textStyle="bold"
        android:textColor="@color/colorAccent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

  • In this example we will show the selected item in a Textview with the help a StringBuffer.
  • Write the following code in java file as follows.
File: MainActivity.java:
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.TextView;

public class MainActivity extends AppCompatActivity {
    CheckBox chk_cricket, chk_football, chk_hockey, chk_swimming;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        chk_cricket = (CheckBox) findViewById(R.id.chk1);
        chk_football = (CheckBox) findViewById(R.id.chk2);
        chk_hockey = (CheckBox) findViewById(R.id.chk3);
        chk_swimming = (CheckBox) findViewById(R.id.chk4);

        Button btn_select = (Button) findViewById(R.id.btn_select);
        Button btn_cancel = (Button) findViewById(R.id.btn_cancel);

        btn_select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringBuilder result = new StringBuilder();
                result.append("You have selected:");
                if (chk_cricket.isChecked()) {
                    result.append("\nCricket");
                }
                if (chk_football.isChecked()) {
                    result.append("\nFootball");
                }
                if (chk_hockey.isChecked()) {
                    result.append("\nHockey");
                }
                if (chk_swimming.isChecked()) {
                    result.append("\nSwimming");
                }

                TextView text_result = (TextView) findViewById(R.id.result);
                text_result.setText(result);
            }
        });

        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}

  • Run the Application and enjoy the output.
Output:










Download project



Thank You!!!
Please like and share...



Comments