Getting Value from Radio button

Copy the code into main.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

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

        <RadioButton
            android:id="@+id/radio_two_way"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="Two way" />

        <RadioButton
            android:id="@+id/radio_one_way"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="one way" />
    </RadioGroup>

    <Button
        android:id="@+id/button_ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ok" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textAppearance=
"?android:attr/textAppearanceLarge" />

</LinearLayout>


Copy the code into activity class fime

package com.islington.pack;

import com.islington.pack.R.string;

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

public class TestRadioButtonActivity extends Activity {
    private RadioGroup choiceWay;
    int selectedValueId;
    RadioButton oneWay;
    TextView testmsg;
    String selectWay;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //declaring button
        Button btnOk =(Button) findViewById(R.id.button_ok);
        //declaring radio group
        choiceWay = (RadioGroup) findViewById(R.id.radioGroup1);
        //declaring radio button
        oneWay = (RadioButton) findViewById(R.id.radio_one_way);
        //text view just for test
        testmsg = (TextView) findViewById(R.id.textView1);
       
       
        //button click listener
        btnOk.setOnClickListener(new OnClickListener() {
           
            public void onClick(View v) {
                //getting the id of selected radio button
                selectedValueId = choiceWay.getCheckedRadioButtonId();
                //checking the id of the selected radio
                if(selectedValueId == oneWay.getId())
                {
                    //yo stirng database ma store gara hai ani sabai huncha
                    testmsg.append("one way");
                    selectWay = "one way";
                }
                else
                {
                    testmsg.append("two way");
                    selectWay ="two way";
                }
            }
        });
    }
}

download code

No comments:

Post a Comment