Android Example To Select From Checkbox

      No Comments on Android Example To Select From Checkbox

A checkbox is specified by two states button that can be either checked or unchecked.
Drag the three checkbox and one button from the pallete.

activity_main.xml

<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: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=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="58dp"
        android:layout_marginTop="31dp"
        android:text="CheckBox 1" />

    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/checkBox1"
        android:layout_below="@+id/checkBox1"
        android:layout_marginTop="16dp"
        android:text="CheckBox 2" />

    <CheckBox
        android:id="@+id/checkBox3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/checkBox2"
        android:layout_below="@+id/checkBox2"
        android:layout_marginTop="26dp"
        android:text="CheckBox 3" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/checkBox3"
        android:layout_below="@+id/checkBox3"
        android:layout_marginTop="72dp"
        android:text="Show" />

</RelativeLayout>

Activity class

Write the code in activity class

File name:-MainActivity.java
package com.example.scanfcode;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.*;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity implements OnClickListener
{
	 CheckBox c1,c2,c3;
	 Button b1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	    
		b1=(Button)findViewById(R.id.button1);
		c1=(CheckBox)findViewById(R.id.checkBox1);
		c2=(CheckBox)findViewById(R.id.checkBox2);
		c3=(CheckBox)findViewById(R.id.checkBox3);
		
		b1.setOnClickListener(this);
		
	}
	
	@Override
	public void onClick(View v)
	{
		if(c1.isChecked())
		{
			Toast.makeText(getApplicationContext()," CheckBox1 is selected", Toast.LENGTH_SHORT).show();
		}
		
		if(c2.isChecked())
		{
			Toast.makeText(getApplicationContext()," CheckBox2 is selected", Toast.LENGTH_SHORT).show();
		}
		
		if(c3.isChecked())
		{
			Toast.makeText(getApplicationContext()," CheckBox3 is selected", Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

OUTPUT:-

scanfcode

Leave a Reply

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