Tuesday, 20 January 2015

RadioButton Example2



String.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">RadioButton2</string>
    <string name="action_settings">Settings</string>
    <string name="gender">Select Gender</string>
    <string name="radiobutton_male">Male</string>
    <string name="radiobutton_female">Female</string>
    <string name="button">Checked Item</string>
   
</resources>


activity_main.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="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/activity_textView_gender"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="47dp"
        android:layout_marginTop="84dp"
        android:text="@string/gender"
        android:textSize="20sp" />

    <RadioGroup
        android:id="@+id/activity_radiogroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/activity_textView_gender"
        android:layout_centerHorizontal="true" >

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

     
  <RadioButton
            android:id="@+id/activity_radiobutton_female"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/radiobutton_female" />
    </RadioGroup>

    <Button
        android:id="@+id/activity_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/activity_radiogroup"
        android:layout_below="@+id/activity_radiogroup"
        android:text="@string/button" />

</RelativeLayout>

MainActivity.java

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{
      
       RadioGroup gender;
       RadioButton male,female;
       String item;
       Button button;

       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
             
              gender=(RadioGroup) findViewById(R.id.activity_radiogroup);
              button=(Button) findViewById(R.id.activity_button);
              button.setOnClickListener(new OnClickListener() {
                    @Override
                     public void onClick(View v) {
               
                 RadioButton buttonm = (RadioButton) 
                    findViewById(gender.getCheckedRadioButtonId());
                        String string= button.getText().toString(); 
                     Toast.makeText(getApplicationContext(), string+
                     " is Selected", Toast.LENGTH_LONG).show();
                    

            }
             
       });

     }
 }




No comments:

Post a Comment