In this tutorial Explain how to create CheckBox in android. a checkBox very usefull widgets in android, and the CheckBox class is subclass of compoundButton class.
CheckBox methods how to call , we will explain below.
Returns true if it is checked otherwise false.
CheckBox methods how to call , we will explain below.
- public boolean isChecked()
Returns true if it is checked otherwise false.
- public void setChecked(boolean status)
activtiy_main.xml
.............................................................................................................................................................
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="AndroidBeginner"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Pizza"
android:layout_marginTop="30dp" />
<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:text="Tea" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/checkBox2"
android:text="Snacks" />
<Button
android:id="@+id/button1"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_below="@+id/checkBox2"
android:layout_marginTop="32dp"
android:layout_toLeftOf="@+id/checkBox3"
android:text="Order" />
</LinearLayout>
Main_Activity.xml
.............................................................................................................................................................
package com.androidbeginner.testcode;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
public class MainActivity extends Activity {
CheckBox pizza,Tea,sancks;
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);
Tea=(CheckBox)findViewById(R.id.checkBox2);
sancks=(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(Tea.isChecked()){
result.append("\nCoffe 50Rs");
totalamount+=50;
}
if(sancks.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();
}
});
}
/* public boolean isChecked();
Returns true if it is checked otherwise false.
public void setChecked(boolean status)
Changes the state of the CheckBox. */
}
0 comments