Thursday, 29 December 2016

Android CheckBox

Android Check Box has two types of Button. One is checked and second is unchecked.There is many uses of check boxes. For Example to check user main favorite dishes, hobbies, dreams, restaurant bills etc. 
In this Example i will tell you example of restaurant dishes selected and Its price
Android check Box Example
For android Check Box Example add three check boxes and one button in 
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
   
xmlns:android="http://schemas.android.com/apk/res/android"
   
android:orientation="vertical"
   
android:id="@+id/activity_main"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
  
>

    <
CheckBox
       
android:id="@+id/checkBox1"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_alignParentLeft="true"
       
android:textSize="30dp"
       
android:layout_alignParentTop="true"
       
android:text="Dosa" />

    <
CheckBox
       
android:id="@+id/checkBox2"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_alignParentTop="true"
       
android:layout_toRightOf="@+id/checkBox1"
       
android:textSize="30dp"
       
android:text="Coldrink" />

    <
CheckBox
       
android:id="@+id/checkBox3"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_alignParentTop="true"
       
android:textSize="30dp"
       
android:layout_toRightOf="@+id/checkBox2"
       
android:text="Burger" />

    <
Button
       
android:id="@+id/button1"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_below="@+id/checkBox2"
       
android:layout_marginTop="32dp"
       
android:layout_toLeftOf="@+id/checkBox3"
       
android:text="Order" />
</
LinearLayout>


Now add these line in 
MainActivity.java 



import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    CheckBox dosa,coldrink,burger;
    Button buttonOrder;
    @Override
   
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        OnButtonClick();
    }
    public void OnButtonClick(){
        //Getting instance of CheckBoxes and Button from the activty_main.xml file
       
dosa=(CheckBox)findViewById(R.id.checkBox1);
        coldrink=(CheckBox)findViewById(R.id.checkBox2);
        burger=(CheckBox)findViewById(R.id.checkBox3);
        buttonOrder=(Button)findViewById(R.id.button1);

        //Applying the Listener on the Button click
       
buttonOrder.setOnClickListener(new View.OnClickListener(){

            @Override
           
public void onClick(View view) {
                int totalamount=0;
                StringBuilder result=new StringBuilder();
                result.append("Selected Items:");
                if(dosa.isChecked()){
                    result.append("\nDosa 200Rs");
                    totalamount+=200;
                }
                if(coldrink.isChecked()){
                    result.append("\nCoffe 60Rs");
                    totalamount+=60;
                }
                if(burger.isChecked()){
                    result.append("\nBurger 80Rs");
                    totalamount+=80;
                }
                result.append("\nTotal: "+totalamount+"Rs");
                //Displaying the message on the toast
               
Toast.makeText(getApplicationContext(), result.toString(), Toast.LENGTH_LONG).show();
            }

        });
    }
}
You can the project then this output Show

You can directly see my other posts directly this links

  1. Best Education Courses and Hosting Discount
  2. LeadBolt Ads in Android Studio 
  3. Share Button
  4. Exit Button
  5. Splash Screen
 

No comments:

Post a Comment