Thursday, 14 May 2015

Android Radio Button Example

Radio Group

          A RadioGroup class is used for set of radio buttons. If we check one radio button that belongs to a radio group, it automatically unchecks any previously checked radio button within the same group.



Step 1).main_activity.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="20dp"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:paddingTop="20dp"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:text="AndroidDhina" />

    <RadioGroup
        android:id="@+id/myRadioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text" >

        <RadioButton
            android:id="@+id/apple"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Apple" />

        <RadioButton
            android:id="@+id/android"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Android" />

        <RadioButton
            android:id="@+id/silent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Java" />
    </RadioGroup>

    <Button
        android:id="@+id/saveBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/myRadioGroup"
        android:layout_below="@+id/myRadioGroup"
        android:layout_marginTop="36dp"
        android:text="Save" />

</RelativeLayout>


Step 2).Mainactivity.xml


package com.radio;

import android.app.Activity;
import android.os.Bundle;
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 selectedButton;
Button button;
String Value1;

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

radioGroup = (RadioGroup) findViewById(R.id.myRadioGroup);
button = (Button) findViewById(R.id.saveBtn);
int r1selectedId = radioGroup.getCheckedRadioButtonId();
selectedButton = (RadioButton) findViewById(r1selectedId);

button .setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) 
      {
try {
Toast.makeText(getApplicationContext(),selectedButton.getText().to                                                   String(),Toast.LENGTH_LONG).show();
  } 
          catch (Exception e) 
                {
           Toast.makeText(getApplicationContext(),"please select any option",                                                       Toast.LENGTH_LONG).show();                                    
}
}
});

}
}