Thursday 30 May 2013

Android Radio Buttons 

In Android, you can use “android.widget.RadioButton” class to render radio button, and those radio buttons are usually grouped by android.widget.RadioGroup. If RadioButtons are in group, when one RadioButton within a group is selected, all others are automatically deselected.

if you think that the user needs to see all available options side-by-side. If it's not necessary to show all options side-by-side, use a spinner instead.

RadioButton Click Events

When the user selects one of the radio buttons, the corresponding RadioButton object receives an on-click event.
To define the click event handler for a button, add the android:onClick attribute to the <RadioButton>element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. The Activity hosting the layout must then implement the corresponding method or you can also define click event individually for each radiobutton in activity class as radioButton.setOnClickListner().

In this tutorial, we show you how to use XML to create two radio buttons, and grouped in a radio group. When button is clicked, display which radio button is selected.

2. RadioButton


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<RadioGroup
android:id="@+id/radiobuttongrp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ronaldo"
android:checked="true" />

<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/messi" />

</RadioGroup>

<Button
android:id="@+id/btnDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_display" />

</LinearLayout>

3. Code 

package com.learnsimply.radiobuttonexample;


import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends Activity {

private RadioGroup radioGroup;
private RadioButton radioButton;
private Button btnDisplay;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

initializeClickListner();

}

public void initializeClickListner() {

radioGroup = (RadioGroup) findViewById(R.id.radiobuttongrp);
btnDisplay = (Button) findViewById(R.id.btnDisplay);

btnDisplay.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

// get selected radio button from radioGroup
int selectedId = radioGroup.getCheckedRadioButtonId();

// find the radiobutton by returned id
radioButton = (RadioButton) findViewById(selectedId);

Toast.makeText(MainActivity.this,
radioButton.getText(), Toast.LENGTH_SHORT).show();

}

});

}
}

Run the application

Result here is Ronaldo when you click on display button







Download Source Code RadionButtonExample







No comments:

Post a Comment