Wednesday, 28 January 2015

Android Working With Check boxes

Step 1 :
Make an android project named “CheckboxExample”.
Step 2 :
Paste the following code in MainActivity.java file.
package com.example.checkboxexample;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

public class MainActivity extends Activity
{
       CheckBox pizza, coffe, burger;
       Button buttonOrder;

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

       public void addListenerOnButtonClick() {
              // Getting instance of CheckBoxes and Button from the activty_main.xml
              // file
              pizza = (CheckBox) findViewById(R.id.checkBox1);
              coffe = (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 OnClickListener() {

                     @Override
                     public void onClick(View view) {
                           int totalamount = 0;
                           StringBuilder result = new StringBuilder();
                           result.append("Selected Items:");
                           if (pizza.isChecked()) {
                                  result.append("\nPizza 100Rs");
                                  totalamount += 100;
                           }
                           if (coffe.isChecked()) {
                                  result.append("\nCoffe 50Rs");
                                  totalamount += 50;
                           }
                           if (burger.isChecked()) {
                                  result.append("\nBurger 120Rs");
                                  totalamount += 120;
                           }
                           result.append("\nTotal: " + totalamount + "Rs");
                           // Displaying the message on the toast
                           Toast.makeText(getApplicationContext(), result.toString(),
                                         Toast.LENGTH_LONG).show();
                     }

              });
       }

}
Step 3 :
Paste the following code in activity_main.xml file.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Pizza" />

    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Coffee" />

    <CheckBox
        android:id="@+id/checkBox3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Burger" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Order" />


</LinearLayout>

No comments:

Post a Comment