meljun cortes android checkbox example
TRANSCRIPT
Android Checkbox Example by Ilias Tsagklis on December 17th, 2012 | Filed in: CheckBox Tags: Android Checkbox, Android Core, Android UI
One of the easiest and most common ways to accept user input in an Android Application is the
Checkbox component. In this tutorial we are going to see how to set up and display a checkbox list
with some options for the user. Furthermore, we are going to use a button that when it’s pressed, a
message will be displayed reflecting the status of the checkboxes and that is checked or unchecked.
For this tutorial, we will use the following tools in a Windows 64-bit platform:
1. JDK 1.7
2. Eclipse 4.2 Juno
3. Android SKD 4.2
1. Create a new Android Project
Open Eclipse IDE and go to File -> New -> Project -> Android -> Android Application Project. You
have to specify the Application Name, the Project Name and the Package name in the appropriate
text fields and then click Next.
In the next window make sure the “Create activity” option is selected in order to create a new activity
for your project, and click Next. This is optional as you can create a new activity after creating the
project, but you can do it all in one step.
Select “BlankActivity” and click Next.
You will be asked to specify some information about the new activity. In the Layout Name text field
you have to specify the name of the file that will contain the layout description of your app. In our
case the file res/layout/main.xml will be created.Then, click Finish.
2. Adding resources
Use the Package Explorer in Eclipse to navigate to res/values/strings.xml
When you open the strings.xml file, Eclipse will display the graphical Resources View editor :
That’s a nice and easy tool you can use to add several resources to your application like strings,
integers, color values etc. But we are going to use the traditional way and that is editing
the strings.xml file by hand. In the bottom of the screen, press the string.xml tab and paste
the following code :
01 <?xml version="1.0" encoding="utf-8"?>
02 <resources>
03 <string name="app_name">CheckboxExample</string>
04 <string name="linux_box">Linux</string>
05 <string name="macos_box">Mac OS</string>
06 <string name="windows_box">Windows</string>
07 <string name="display_label">Display</string>
08 <string name="menu_settings">Settings</string>
09
10 </resources>
So we’ve just created some string resources that we can use in many ways and in many places in
our app.
3. Creating a Ceckbox list
Open res/layout/main.xml file :
And paste the following code :
01 <?xml version="1.0" encoding="utf-8"?>
02 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03 android:layout_width="fill_parent"
04 android:layout_height="fill_parent"
05 android:orientation="vertical" >
06
07 <CheckBox
08 android:id="@+id/linux_option"
09 android:layout_width="wrap_content"
10 android:layout_height="wrap_content"
11 android:text="@string/linux_box" />
12
13 <CheckBox
14 android:id="@+id/macos_option"
15 android:layout_width="wrap_content"
16 android:layout_height="wrap_content"
17 android:text="@string/macos_box"
18 android:checked="true" />
19
20 <CheckBox
21 android:id="@+id/windows_option"
22 android:layout_width="wrap_content"
23 android:layout_height="wrap_content"
24 android:text="@string/windows_box" />
25
26 <Button
27 android:id="@+id/button"
28 android:layout_width="wrap_content"
29 android:layout_height="wrap_content"
30 android:text="@string/display_label" />
31
32 </LinearLayout>
In the code of the second checkbox (the “Mac OS” option), notice
the android:checked="true" attribute. You may use this when you want an option to be checked
by default. Now you may open the Graphical layout editor to preview the User Interface you created:
4. Adding a ClickListener to a checkbox and to the Button
Go to the java file that contains the code of the activity you’ve just created and paste the following
code:
01 package com.javacodegeeks.android.checkboxexample;
02
03 import android.app.Activity;
04 import android.os.Bundle;
05 import android.view.View;
06 import android.view.View.OnClickListener;
07 import android.widget.Button;
08 import android.widget.CheckBox;
09 import android.widget.Toast;
10
11 public class MainActivity extends Activity {
12
13 private CheckBox linux, macos, windows;
14 private Button button;
15
16 @Override
17 public void onCreate(Bundle savedInstanceState) {
18 super.onCreate(savedInstanceState);
19 setContentView(R.layout.main);
20
21 addListenerOnChkWindows();
22 addListenerOnButton();
23 }
24
25 public void addListenerOnChkWindows() {
26
27 windows = (CheckBox) findViewById(R.id.windows_option);
28
29 windows.setOnClickListener(new OnClickListener() {
30
31 @Override
32 public void onClick(View v) {
33
34 if (((CheckBox) v).isChecked()) {
35 Toast.makeText(MainActivity.this,"Bro, try Linux :)",
Toast.LENGTH_LONG).show();
36 }
37
38 }
39 });
40
41 }
42
43 public void addListenerOnButton() {
44
45 linux = (CheckBox) findViewById(R.id.linux_option);
46 macos = (CheckBox) findViewById(R.id.macos_option);
47 windows = (CheckBox) findViewById(R.id.windows_option);
48 button = (Button) findViewById(R.id.button);
49
50 button.setOnClickListener(new OnClickListener() {
51
52 @Override
53 public void onClick(View v) {
54
55 StringBuffer result = new StringBuffer();
56 result.append("Linux check : ").append(linux.isChecked());
57 result.append("\nMac OS check : ").append(macos.isChecked());
58 result.append("\nWindows check :").append(windows.isChecked());
59
60 Toast.makeText(MainActivity.this, result.toString(),
Toast.LENGTH_LONG).show();
61
62 }
63 });
64
65 }
66 }
The above code adds two ClickListeners. So:
1. If the checkbox component with id “windows_option” was clicked, a message suggesting to use
Linux :) will be displayed.
2. If the button was pressed, display a message reflecting the status of the checkboxes.
5. Run the application
This is the main screen of our Application:
When we check the Windows option:
And when we press the Button :