Checkbox Example

Checkbox Example in Android

Class File

package com.example.malik.test;

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

public class MainActivity extends Activity {

CheckBox checkBox1, checkBox2, checkBox3;
Button select;

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

addListenerOnButton();
addListenerOnCheckBox();

}

private void addListenerOnCheckBox() {
checkBox1 = (CheckBox) findViewById(R.id.cbDog);
checkBox1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
Toast.makeText(getApplicationContext(), “Dog is selected”, Toast.LENGTH_SHORT).show();
}
}
});
}

private void addListenerOnButton() {
checkBox1 = (CheckBox) findViewById(R.id.cbDog);
checkBox2 = (CheckBox) findViewById(R.id.cbCat);
checkBox3 = (CheckBox) findViewById(R.id.cbCow);
select = (Button) findViewById(R.id.bSelect);

select.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringBuffer result = new StringBuffer();
result.append(“Dog : “).append(checkBox1.isChecked());
result.append(“\nCat : “).append(checkBox2.isChecked());
result.append(“\nCow : “).append(checkBox3.isChecked());
Toast.makeText(getApplicationContext(), result.toString(), Toast.LENGTH_SHORT).show();
}
});
}
}

XML FILE

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:orientation=”vertical”
android:paddingBottom=”@dimen/activity_vertical_margin”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
tools:context=”com.example.malik.test.MainActivity”>
<CheckBox
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Dog”
android:id=”@+id/cbDog”
android:layout_marginTop=”67dp”
android:checked=”false”
android:layout_alignParentTop=”true”
android:layout_alignParentRight=”true”
android:layout_alignParentEnd=”true” />

<CheckBox
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Cat”
android:id=”@+id/cbCat”
android:layout_marginTop=”38dp”
android:checked=”false”
android:layout_below=”@+id/cbDog”
android:layout_alignParentLeft=”true”
android:layout_alignParentStart=”true” />

<CheckBox
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Cow”
android:id=”@+id/cbCow”
android:layout_below=”@+id/cbCat”
android:layout_alignParentLeft=”true”
android:layout_alignParentStart=”true”
android:layout_marginTop=”32dp”
android:checked=”false” />

<Button
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Select”
android:id=”@+id/bSelect”
android:layout_below=”@+id/cbCow”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”55dp” />
</RelativeLayout>

 

Output :

pic

 

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.