Site icon WebArtDeveloper

Basic Concept of CheckBox Control in Android

In this tutorial basic concepts of CheckBox control of Android is shown.

Code:

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:layout_alignParentLeft=”true”
android:layout_alignParentTop=”true”
android:layout_marginLeft=”46dp”
android:layout_marginTop=”55dp”
android:text=”Hobbies” />

<CheckBox
android:id=”@+id/c1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignBaseline=”@+id/textView1″
android:layout_alignBottom=”@+id/textView1″
android:layout_marginLeft=”40dp”
android:layout_toRightOf=”@+id/textView1″
android:onClick=”onCheck”
android:text=”Reading” />

<CheckBox
android:id=”@+id/c2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/c1″
android:layout_below=”@+id/c1″
android:layout_marginTop=”18dp”
android:onClick=”onCheck”
android:text=”Collecting coins” />

<CheckBox
android:id=”@+id/c3″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/c2″
android:layout_below=”@+id/c2″
android:layout_marginTop=”18dp”
android:onClick=”onCheck”
android:text=”Sports” />

<CheckBox
android:id=”@+id/c4″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/c3″
android:layout_centerVertical=”true”
android:onClick=”onCheck”
android:text=”Others” />

<Button
android:id=”@+id/b1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_below=”@+id/c4″
android:layout_marginTop=”44dp”
android:layout_toRightOf=”@+id/textView1″
android:onClick=”onSubmit”
android:text=”Submit” />

<TextView
android:id=”@+id/t2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentBottom=”true”
android:layout_centerHorizontal=”true”
android:layout_marginBottom=”41dp” />

</RelativeLayout>

MainActivity.java

package com.example.demo;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

public class MainActivity extends Activity{

TextView t2;
String mes=””;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t2=(TextView)findViewById(R.id.t2);
}

public void onCheck(View v)
{
Boolean ch=((CheckBox)v).isChecked();

switch(v.getId())
{
case R.id.c1:
if(ch)
mes=mes+”Reading “;
break;
case R.id.c2:
if(ch)
mes=mes+”Collecting Coins “;
break;
case R.id.c3:
if(ch)
mes=mes+”Sports “;
break;
case R.id.c4:
if(ch)
mes=mes+”Others “;
break;
}
}

public void onSubmit(View v)
{
t2.setText(mes);
}

@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;
}

}

https://youtu.be/xbui5E2Ati4

Exit mobile version