MELJUN CORTES Android radio buttons example

15
Android Radio Buttons Example by Byron Kiourtzoglou on December 18th, 2012 | Filed in: RadioButton, RadioGroup Tags: Android Core, Android Radio Button, Android UI One of the easiest and most common ways to accept user input in an Android Application is the Radio Button component. Usually, we want radio buttons to be in groups, representing a set of options. In a group, when a radio button is selected all the other options in this group are automatically deselected. But you can also choose to have interdependent radio buttons in your application, in which case the previous rule doesn’t apply. In this tutorial we are going to see how to set up and display radio buttons with some options for the user as well as how to put them in a group. Furthermore, we are going to use a button that when it’s pressed, a message will be displayed reflecting the selected option of the user. 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.

Transcript of MELJUN CORTES Android radio buttons example

Android Radio Buttons Example by Byron Kiourtzoglou on December 18th, 2012 | Filed in: RadioButton, RadioGroup Tags: Android Core, Android Radio

Button, Android UI

One of the easiest and most common ways to accept user input in an Android Application is the

Radio Button component. Usually, we want radio buttons to be in groups, representing a set of

options. In a group, when a radio button is selected all the other options in this group are

automatically deselected. But you can also choose to have interdependent radio buttons in your

application, in which case the previous rule doesn’t apply.

In this tutorial we are going to see how to set up and display radio buttons with some options for the

user as well as how to put them in a group. Furthermore, we are going to use a button that when it’s

pressed, a message will be displayed reflecting the selected option of the user.

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.

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

04 <string name="app_name">RadioButtonExample</string>

05 <string name="hello_world">Hello world!</string>

06 <string name="menu_settings">Settings</string>

07 <string name="male_ch">Male</string>

08 <string name="female_ch">Female</string>

09 <string name="button_label">Display</string>

10

11 </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 RadioButton Group

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 <RadioGroup

08 android:id="@+id/radioGenderGroup"

09 android:layout_width="wrap_content"

10 android:layout_height="wrap_content" >

11

12 <RadioButton

13 android:id="@+id/radio_male"

14 android:layout_width="wrap_content"

15 android:layout_height="wrap_content"

16 android:text="@string/male_ch"/>

17

18 <RadioButton

19 android:id="@+id/radio_female"

20 android:layout_width="wrap_content"

21 android:layout_height="wrap_content"

22 android:text="@string/female_ch"

23 android:checked="true" />

24

25 </RadioGroup>

26

27 <Button

28 android:id="@+id/button"

29 android:layout_width="wrap_content"

30 android:layout_height="wrap_content"

31 android:text="@string/button_label" />

32

33 </LinearLayout>

As you can see we use the RadioGroup tag in order to create a group of options, and we place the

radio buttons we want inside that group. In the code of the second radio button (the “Female” option)

, notice the android:checked="true" attribute. You may use this when you want an option to be

selected by default. Now you may open the Graphical layout editor to preview the User Interface you

created:

4. Adding a ClickListener 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.radiobuttonexample;

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.RadioButton;

09 import android.widget.RadioGroup;

10 import android.widget.Toast;

11

12 public class MainActivity extends Activity {

13

14 private RadioGroup radioGroupId;

15 private RadioButton radioGenderButton;

16 private Button button;

17

18 @Override

19 public void onCreate(Bundle savedInstanceState) {

20 super.onCreate(savedInstanceState);

21 setContentView(R.layout.main);

22

23 addButtonListener();

24

25 }

26

27 public void addButtonListener() {

28

29 radioGroupId = (RadioGroup) findViewById(R.id.radioGenderGroup);

30

31 button = (Button) findViewById(R.id.button);

32

33 button.setOnClickListener(new OnClickListener() {

34

35 @Override

36 public void onClick(View v) {

37

38 // get the selected radio button from the group

39 int selectedOption = radioGroupId.getCheckedRadioButtonId();

40

41 // find the radiobutton by the previously returned id

42 radioGenderButton = (RadioButton) findViewById(selectedOption);

43

44 Toast.makeText(MainActivity.this, radioGenderButton.getText(),

Toast.LENGTH_SHORT).show();

45

46 }

47

48 });

49

50 }

51 }

5. Run the application

This is the main screen of our Application. You can see that the option “Female” is checked by

default: