Android
CheckBox
Android CheckBox is a type of two state button either checked
or unchecked.
There can be a lot of usage of checkboxes. For example, it
can be used to know the hobby of the user, activate/deactivate the specific
action etc.
Android CheckBox class is the subclass of CompoundButton
class.
Android
CheckBox Class
The android.wifget.CheckBox class provides the facility of
creating the CheckBox.
Method
of CheckBox class
There are many inherited method of View, TextView, and Button
classes in the CheckBox class. Some of them are as follows:
Method
|
Description
|
Public
Boolean isChecked()
|
Returns true
if it is checked otherwise false.
|
Public void
setChecked(Boolean status)
|
Changes the
state of the CheckBox.
|
Android
CheckBox Example
Activity_manin.xml
File:activity_main.xml
<?xml
version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
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_centerHorizontal="true"
android:layout_marginTop="25dp"
android:textSize="25sp"
android:text="Pizza"/>
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/checkBox1"
android:layout_marginTop="15dp"
android:layout_centerHorizontal="true"
android:textSize="25sp"
android:text="Coffe"/>
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/checkBox2"
android:layout_marginTop="15sp"
android:textSize="25sp"
android:text="Burger"
android:layout_centerHorizontal="true"/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Order"
android:layout_marginTop="30dp"
android:layout_below="@+id/checkBox3"/>
</RelativeLayout>
Activity Class
File:MainActivity.java
package
com.mahesh.androidissimples.checkboxdemo;
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 pizza,coffe,burger;
Button buttonOrder;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButtonClick();
}
private void addListenerOnButtonClick() {
//Getting instance of CheckBoxes and
Button from the activity_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);
buttonOrder.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
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(MainActivity.this, result.toString(),
Toast.LENGTH_SHORT).show();
}
});
}
}
Output:
No comments:
Post a Comment