Selecting a RadioButton in a Radiogroup
<p>I'm new to android and I was trying out an example program using the radiogroups. I have declared 2 radio buttons under the radiogroup. I wanted which radio button is selected in the 
radiogroup. as wanted to use it in the if and the elseif condition of the Anonymous class new onClickListener.</p>

<p>I tried to declare an integer variable radio_selected and to get the return value of the  radgrp.getCheckedRadioButtonId()
radio_selected =  radgrp.getCheckedRadioButtonId(); and use the constant for comparision in the if and else if condition as,</p>

<p>if( radio_selected == R.id.rdb1  &amp;&amp; (edit1.getText().toString().equals("admin")) &amp;&amp; (edit2.getText().toString().equals("admin")))</p>

<p>I tried to print the value of radio_selected . Its printing me -1 which means empty selection. But radgrp.getCheckedRadioButtonId(); is returning a integer value.
However the above program compiles and works fine but i wanted to know why i could not use radio_selected constant to do the comparision?</p>

<h2>.java file</h2>

<pre><code>package android.button;


import android.app.Activity;

import android.os.Bundle;

import android.text.Editable;

import android.text.method.KeyListener;

import android.util.Log;

import android.view.KeyEvent;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.View.OnKeyListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.RadioGroup;

import android.widget.Toast;

import android.widget.RadioGroup.OnCheckedChangeListener;


public class Android_eg2 extends Activity {


/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

final int radio_selected;


final EditText edit1 = (EditText)findViewById(R.id.txt1);

final EditText edit2 = (EditText)findViewById(R.id.txt2);

edit2.setOnKeyListener(new OnKeyListener() {


public boolean onKey(View v, int keyCode, KeyEvent event) {

// TODO Auto-generated method stub

if(edit2.getText().toString().length()&gt;5){

Toast.makeText(getApplicationContext(), "EXCEEDED the MAX LIMIT", Toast.LENGTH_LONG).show();

}

return false;

}

});

final RadioGroup radgrp = (RadioGroup)findViewById(R.id.rad);

//radio_selected = radgrp.getCheckedRadioButtonId();

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

bt.setOnClickListener(new OnClickListener() {


public void onClick(View v) {

//System.out. println(radgrp.getCheckedRadioButtonId());

//System.out.println(radio_selected);

// TODO Auto-generated method stub

if(R.id.button == v.getId()){

if( radgrp.getCheckedRadioButtonId() == R.id.rdb1 &amp;&amp; (edit1.getText().toString().equals("admin")) &amp;&amp; (edit2.getText().toString().equals("admin"))){

Toast.makeText(getApplicationContext(),"LOGIN SUCCESS FOR ADMIN",Toast.LENGTH_LONG).show();

Log.v("TAG","In admin");

}

else if(radgrp.getCheckedRadioButtonId() == R.id.rdb2 &amp;&amp; (edit1.getText().toString().equalsIgnoreCase("user")) &amp;&amp; (edit2.getText().toString().equalsIgnoreCase("user"))){

Toast.makeText(getApplicationContext(),"LOGIN SUCCESS FOR USER",Toast.LENGTH_LONG).show();

Log.v("TAG","In user");

}

else{

Toast.makeText(getApplicationContext(),"INVALID ENTRY",Toast.LENGTH_LONG).show();

Log.v("TAG","Invalid");

}

}

}

});


radgrp.setOnCheckedChangeListener(new OnCheckedChangeListener() {


public void onCheckedChanged(RadioGroup group, int checkedId) {

// TODO Auto-generated method stub

switch(checkedId){

case R.id.rdb1:

//Toast.makeText(getApplicationContext(),"ADMIN",Toast.LENGTH_LONG).show();

case R.id.rdb2:

//Toast.makeText(getApplicationContext(),"USER",Toast.LENGTH_LONG).show();

}

}

});

}

}
</code></pre>

<h2>main.xml file</h2>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;

&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

&gt;

&lt;TextView 

android:layout_width="wrap_content" 

android:layout_height="wrap_content" 

android:text="Welcome to wipro"

/&gt;


&lt;LinearLayout

android:orientation="vertical"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

&gt;


&lt;RadioGroup

android:id="@+id/rad"

android:layout_width="wrap_content" 

android:layout_height="wrap_content" 

android:orientation="horizontal"

&gt;

&lt;RadioButton

android:id="@+id/rdb1"

android:layout_width="wrap_content" 

android:layout_height="wrap_content"

android:text = "Admin"

&gt;

&lt;/RadioButton&gt;

&lt;RadioButton

android:id="@+id/rdb2"

android:layout_width="wrap_content" 

android:layout_height="wrap_content"

android:text = "User"

&gt;

&lt;/RadioButton&gt;

&lt;/RadioGroup&gt;


&lt;LinearLayout

android:orientation="horizontal"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

&gt;




&lt;TextView 

android:id="@+id/lbl1"

android:layout_width="100dp"

android:layout_height="wrap_content"

android:text="USER NAME"

android:textSize="6pt"

android:textColor = "#00aa00"

&gt;


&lt;/TextView&gt;


&lt;EditText

android:id="@+id/txt1"

android:layout_width="100dp"

android:layout_height="wrap_content"

android:text=""

android:textSize="6pt"

android:textColor = "#aa0000"

&gt;

&lt;/EditText&gt;


&lt;/LinearLayout&gt;


&lt;LinearLayout

android:orientation="horizontal"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

&gt;

&lt;TextView

android:id="@+id/lbl2"

android:layout_width="100dp"

android:layout_height="wrap_content"

android:text="PASSWORD"

android:textSize="6pt"

android:textColor="#00aa00"

android:layout_weight="1"


&gt;


&lt;/TextView&gt;


&lt;EditText

android:id="@+id/txt2"

android:layout_width="100dp"

android:layout_height="wrap_content"

android:text=""

android:textSize="6pt"

android:textColor = "#aa0000"

android:password = "true"

&gt;

&lt;/EditText&gt;


&lt;/LinearLayout&gt;


&lt;/LinearLayout&gt;


&lt;LinearLayout

android:orientation="horizontal"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

&gt;

&lt;Button

android:id="@+id/button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="SUBMIT"

&gt;

&lt;/Button&gt;


&lt;/LinearLayout&gt;


&lt;/LinearLayout&gt;
</code></pre>

<h2>R.java file</h2>

<pre><code>/* AUTO-GENERATED FILE. DO NOT MODIFY.

*

* This class was automatically generated by the

* aapt tool from the resource data it found. It

* should not be modified by hand.

*/


package android.button;


public final class R {

public static final class attr {

}

public static final class drawable {

public static final int icon=0x7f020000;

}

public static final class id {

public static final int button=0x7f050007;

public static final int lbl1=0x7f050003;

public static final int lbl2=0x7f050005;

public static final int rad=0x7f050000;

public static final int rdb1=0x7f050001;

public static final int rdb2=0x7f050002;

public static final int txt1=0x7f050004;

public static final int txt2=0x7f050006;

}

public static final class layout {

public static final int main=0x7f030000;

}

public static final class string {

public static final int app_name=0x7f040001;

public static final int hello=0x7f040000;

}

}
</code></pre>
