The Android CheckBox

The Android CheckBox


The check box can be defined as that part of the widget that respond to on check listener. It function as a type of button which is use to listen to clickable checkable. It is declared using the
import android.widget.CheckBox;

Since the check box is a special type of button, more emphasis will be focused on our big story. I.e creating a restaurant app.

The Big Story

The big story for this tutorial will be creating a restaurant application. Imagine you walk into a restaurant, sit down and ready to order something to eat or drink. In front of you is an android device which is use in ordering your food or drink. You pick up the device, thick what you want and click the total button. The app show you how much is your food or drink and send a request to who so ever is bringing what you order. Here, we shall not be dealing with sending the request. We shall be focusing on when you click on the total button, the amount of food you want will appear.

The restaurant has only five set of food which are 
Pawpaw Rice
Orange stew
Guava Beans
Mango Chicken
Cashew Cake
This five set of food represent the five(5) check box we are creating. 
Also, a button you click to display the amount of food and
Lastly, a TextView which is visible to see the amount display.



Click on this link to know how to program using the OnClickListener of a button
The Android button


main.XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
android:orientation="vertical"
    android:gravity="center">

    <CheckBox
android:id="@+id/chk1"
        android:text="PawPaw Rice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:textSize="30dp" />
<CheckBox
android:layout_marginTop="30dp"
android:textSize="30dp"
android:id="@+id/chk2"
        android:text="Orange Stew"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
<CheckBox
android:layout_marginTop="30dp"
android:id="@+id/chk3"
        android:text="Guava Beans"
android:textSize="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
<CheckBox
android:layout_marginTop="30dp"
android:id="@+id/chk4"
        android:text="Mango Chicken"
android:textSize="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
<CheckBox
android:layout_marginTop="30dp"
android:id="@+id/chk5"
        android:text="Cashew Cake"
android:textSize="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
<Button
android:layout_marginRight="200dp"
android:layout_marginTop="30dp"
android:id="@+id/btn1"
        android:text="Total"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
<TextView
android:layout_marginTop="-40dp"
android:id="@+id/tv1"
        android:text="The Amount will be:"
android:textSize="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


</LinearLayout>



MainActivity.JAVA




import android.app.*;
import android.os.*;
import android.widget.*;
import android.view.*;
import android.content.*;

public class MainActivity extends Activity
{
CheckBox rice,stew,beans,chicken,cake;
Button button;
TextView textview;
int a,b,c,d,e;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
rice= (CheckBox) findViewById(R.id.chk1);
stew= (CheckBox) findViewById(R.id.chk2);
beans= (CheckBox) findViewById(R.id.chk3);
chicken= (CheckBox) findViewById(R.id.chk4);
cake= (CheckBox) findViewById(R.id.chk5);
button= (Button) findViewById(R.id.btn1);
textview= (TextView) findViewById(R.id.tv1);



rice.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if(rice.isChecked()){
a = 50;
}
else{
a = 0;
}

}
});

stew.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if(stew.isChecked()){
b = 30;
}
else{
b = 0;
}

}
});

beans.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if(beans.isChecked()){
c = 40;
}
else{
c = 0;
}
}
});

chicken.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if(chicken.isChecked()){
d = 60;
}
else{
d =0;
}
}
});

cake.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if(cake.isChecked()){
e = 100;
}
else{
e = 0;
}
}
});
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
textview.setText("your amount is $" + (a+b+c+d+e));
}
});



    }
}








When you thick on the check box and pressed the total button it shows you the amount of food

Firstly, what we did was to create an instance variable and find each variable name with their corresponding ID. That should be no problem for you.
The check box behave like the button. Therefore, an on click listener will be used to listen to    checkes.
Then, we use a method called isCheck() to know if actually it listen to on click listener and at the same time listening to checks.



rice.setOnClickListener(new View.OnClickListener(){
 @Override
 public void onClick(View v){
if(rice.isChecked()){
 a = 50;
}
else{
 a = 0;
}

 }
});

An integers of variable was created. This is use to know the amount of each food.
For instance, pawpaw rice is $50, Mango stew is $30 etc.

If paw paw rice is check, then the amount should be $50 else if pawpaw rice is not check, then the amount will be $0.
Different variable (a,b,c,d,e) was assign to each food item. When you click on the button, the TextView set each variable
TextView.SetText("" + (a+b+c+d+e)); 
Always remember to do all calculations in a bracket. This is a good practice to avoid in calculations.




Click on this link to learn how to navigate to another Activity using button

Navigating to Another Activity






GoodBye






















No comments:

Post a Comment