Browse By

Android : Check Box example in Android Studio 2.0

Hello…
Previously we have learned some Examples of Android UI controls in Android Studio 2.0:

  1.  EditText Example
  2.  AutoCompleteTextView example
  3.  Spinner (Drop-Down List) example
  4.  Radio Button and Radio Group controller

Today we are going to learn another example of UI Control Check Box example in Android Studio 2.0 .

Android : Check Box example in Android Studio 2.0

What is Check Box in Android?
A Check Box allow the user to select one or more options from a set. A Check Box is a specific type of two-states button that can be either checked or unchecked.The Check Box button is often used in lists of items where the user can select multiple items.

A Check Box is an on/off switch that can be toggled by the user.

The Android check box contains a text attribute that appears to the side of the check box. This is used in a similar way to the label of a basic button.

Direct Download Full Source Code

 

Android : Check Box example in Android Studio 2.0

Create a new project and fill the required details File ⇒ New ⇒ New Project.

  1.  After Creating Project, open res –> layout
  2. Open “content_main.xml” and add editext control in it as follows:
    <CheckBox
        android:id="@+id/chkPizza"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:layout_margin="5dp"
        android:text="Pizza">
    </CheckBox>
    
    <CheckBox
        android:id="@+id/chkBurger"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:layout_margin="5dp"
        android:text="Burger">
    </CheckBox>
    
    <CheckBox
        android:id="@+id/chkPasta"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:layout_margin="5dp"
        android:text="Pasta">
    </CheckBox>
    
    <CheckBox
        android:id="@+id/chkIce"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:layout_margin="5dp"
        android:text="Ice Cream">
    </CheckBox>
  3. Now open your “MainActivity.java” and initialize the CheckBox control in it.
    public class MainActivity extends AppCompatActivity  {
        //ui members
        CheckBox chkPizza, chkPasta, chkBurger, chkIce;
        TextView tvOrder, tvTotal;
       
       //local members
        int total = 0;
        int pizzaCost = 50, pastaCost = 45, burgerCost = 40, iceCost = 55;
        String order = "Placed Order :";
        String totalBill = "Total Bill is : ";
       
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
           
            // Init ui members
            chkPizza = (CheckBox) findViewById(R.id.chkPizza);
            chkPasta = (CheckBox) findViewById(R.id.chkPasta);
            chkBurger = (CheckBox) findViewById(R.id.chkBurger);
            chkIce = (CheckBox) findViewById(R.id.chkIce);
            tvOrder = (TextView) findViewById(R.id.tvOrder);
            tvTotal = (TextView) findViewById(R.id.tvTotal);
        }
    }
  4. Let’s “MainActivity.java” implements CompoundButton.OnCheckedChangeListener method. Attach listeners to checkbox in onCreate() method.
    chkPizza.setOnCheckedChangeListener(this);
    chkPasta.setOnCheckedChangeListener(this);
    chkBurger.setOnCheckedChangeListener(this);
    chkIce.setOnCheckedChangeListener(this);
  5. Before we proceed, lets have a look at some important methods of checkbox.

    a) setOnCheckedChangeListener() method allows to you to attached check box change listener.
    b) OnCheckedChanged() method of OnCheckedChangeListener allows you to listen check box change event.
    c) IsChecked() methods returns true if checkbox is checked else false.
    d) SetChecked() method allows you to check / uncheck checkbox

  6.  Handle Checkbox events using onCheckedChanged() method in “MainActivity.java”
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (buttonView == chkPizza) {
            if (isChecked) {
                total = total + pizzaCost;
                order = order + " " + buttonView.getText();
            } else {
                total = total - pizzaCost;
                order = order.replace(buttonView.getText(), "");
            }
        }
        if (buttonView == chkPasta) {
            if (isChecked) {
                total = total + pastaCost;
                order = order + " " + buttonView.getText();
            } else {
                total = total - pastaCost;
                order = order.replace(buttonView.getText(), "");
            }
        }
        if (buttonView == chkBurger) {
            if (isChecked) {
                total = total + burgerCost;
                order = order + " " + buttonView.getText();
            } else {
                total = total - burgerCost;
                order = order.replace(buttonView.getText(), "");
            }
        }
        if (buttonView == chkIce) {
            if (isChecked) {
                total = total + iceCost;
                order = order + " " + buttonView.getText();
            } else {
                total = total - iceCost;
                order = order.replace(buttonView.getText(), "");
            }
        }
        tvOrder.setText(order);
        tvTotal.setText(totalBill + total);
    }

7. Now execute application:

Android : Check Box example in Android Studio 2.0


I hope you like this article. Share your views to to improve content. Please stay tune with us. Happy Coding !!!

Subscribe for more articles!!

Download Full Source Code