Introduction
In this article, we will learn how to use Checkboxes using Android application. We will check one example to understand checkbox concept and how it works in an android application.
What is a CheckBox?
Checkboxes are the GUI elements which provide facility to select multiple options (from zero to all) from an available list of options. It is very useful while developing any kind of application and Android is not an exception. A CheckBox can have two states i.e, either checked or Unchecked.
We can make our logic according to these two states of a checkbox. In general, it can be said that it is not much different from any HTML input(<input type=”checkbox” name=”name1″ value=”value1″ />), ASP.NET checkbox(<asp:CheckBox ID=”CheckBox1″ runat=”server”>) etc. with very few differences.
Pre-requisites
The reader of this post doesn’t need to have a depth knowledge on Android apps development, in fact this is for beginners who have just started developing android apps and want to know the basics. This post may not help you in real-world project development but, is a short demonstration of CheckBox control in Android.doesn’t need to have a depth knowledge on Android apps development, in fact this is for beginners who have just started developing android apps and want to know the basics. This post may not help you in real-world project development but, is a short demonstration of CheckBox control in Android.
If you are a beginner and want to know the whole thing right from the start like installing Android SDK and creating your first application etc then you may check these post..If you are a beginner and want to know the whole thing right from the start like installing Android SDK and creating your first application etc then you may check these post..
- Installing Android SDK on windows – Complete steps
- Creating ‘HelloWorld’ application in Android with Eclipse Indigo
- Installing [dot]apk file on Emulator in Windows
- Simple example of Intent
- Using Button & Click-Event with example
CheckBox in Android
To demonstrate a very simple example using checkbox, I have dragged a TextView and two CheckBox to my main.xml and did the basic setting-up kind of things like id,text,textColor,background,width,height etc as per my need.
Now create a OnClickListener which will later listen to our on-click events on the two checkboxes. Then put a very simple logic in the onClick method to show text of the checked checkbox(s) in the TextView and let reset the text of the TextView if none of these two checkboxes are checked.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<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/tvDetails"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.17"
android:textSize="22dp"
android:background="@android:color/white"
android:textColor="@android:color/black" />
<CheckBox
android:id="@+id/cbSuvendu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/suvendu" />
<CheckBox
android:id="@+id/cbWordPress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/wordpress" />
</LinearLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, CheckBoxTutorialActivity!</string>
<string name="app_name">First CheckBox Tutorial</string>
<string name="suvendu">www.suvendugiri.com</string>
<string name="wordpress">www.wordpress.com</string>
</resources>
Activity.java
package com.suvendu.tutorial.cb;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.TextView;
public class CheckBoxTutorialActivity extends Activity {
TextView tv;
CheckBox cbS;
CheckBox cbW;
OnClickListener checkBoxListener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
cbS=(CheckBox)findViewById(R.id.cbSuvendu);
cbW=(CheckBox)findViewById(R.id.cbWordPress);
checkBoxListener =new OnClickListener() {
@Override
public void onClick(View v) {
tv=(TextView)findViewById(R.id.tvDetails);
tv.setText("I Like ");
if(cbS.isChecked()) {
tv.setText(tv.getText().toString()+" "+ cbS.getText().toString());
}
if(cbW.isChecked()) {
tv.setText(tv.getText().toString()+ " "+cbW.getText().toString());
}
if(!cbS.isChecked()&&!cbW.isChecked()) {
tv.setText("");
}
}
};
cbS.setOnClickListener(checkBoxListener);
cbW.setOnClickListener(checkBoxListener);
}
}
Note: You can download the full project from here or you can download the apk here.
Conclusion
So, finally we are done :) and now just download the project and run it using your emulator and if you have any doubts or suggestions, please place them in the comments.
Thanks :) and hope this post will help you :).