Reputation: 12621
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.
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,
if( radio_selected == R.id.rdb1 && (edit1.getText().toString().equals("admin")) && (edit2.getText().toString().equals("admin")))
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?
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()>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 && (edit1.getText().toString().equals("admin")) && (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 && (edit1.getText().toString().equalsIgnoreCase("user")) && (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();
}
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to wipro"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<RadioGroup
android:id="@+id/rad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/rdb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text = "Admin"
>
</RadioButton>
<RadioButton
android:id="@+id/rdb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text = "User"
>
</RadioButton>
</RadioGroup>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/lbl1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="USER NAME"
android:textSize="6pt"
android:textColor = "#00aa00"
>
</TextView>
<EditText
android:id="@+id/txt1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text=""
android:textSize="6pt"
android:textColor = "#aa0000"
>
</EditText>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<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"
>
</TextView>
<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"
>
</EditText>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SUBMIT"
>
</Button>
</LinearLayout>
</LinearLayout>
/* 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;
}
}
Upvotes: 2
Views: 15679
Reputation: 868
RadioGroup radioGroup ;
radioGroup = (RadioGroup)findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId){
case R.id.radio1:
Toast.makeText(getApplicationContext(),"RadioButton1selected",Toast.LENGTH_LONG.show();
case R.id.radio2:
Toast.makeText(getApplicationContext(),"RadioButton2selected",Toast.LENGTH_LONG).show();}
Upvotes: 1
Reputation: 2145
boolean rad1,rad2=false;
radio1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(radio1.isChecked())
{
rad1=true;
}
}
});
radio2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
ifradio2.isChecked())
{
rad2=true;
}
}
});
Upvotes: 2
Reputation: 1555
Hey Angus I think that you almost got it right. The reason your radio_selected variable is printing that value is because you are trying to obtain which radio button is selected while the activity is being created and there are no selected radio buttons, so its value will be -1.
Apart from this I would recommend you to implement OnCheckedChangeListener in your activity class, which would allow you to override the onCheckedChanged method to capture the selection of a radio button. In your case you declared your radio_selected as a final variable, which is an error! Final means that once that variable is defined its value cannot be changed so even if it worked, it would work for the first time and not again. So, with these pieces of advice your activity could look like this:
//all of the imports
public class TestActivity extends Activity implements OnCheckedChangeListener{
private int radio_selected;//now radio_selected is a class variable
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
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()>5)
{
Toast.makeText(getApplicationContext(), "EXCEEDED the MAX LIMIT", Toast.LENGTH_LONG).show();
}
return false;
}
});
final RadioGroup radgrp = (RadioGroup)findViewById(R.id.rad);
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 && (edit1.getText().toString().equals("admin")) && (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 && (edit1.getText().toString().equalsIgnoreCase("user")) && (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(this); //you set this activity as the listener for the events on the radio group...
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch(checkedId){
case R.id.rdb1:
radio_selected = R.id.rdb1;
Toast.makeText(getApplicationContext(),"ADMIN",Toast.LENGTH_LONG).show();
case R.id.rdb2:
radio_selected = R.id.rdb2;
Toast.makeText(getApplicationContext(),"USER",Toast.LENGTH_LONG).show();
}
}
}
Notice that I set the value of radio_selected whenever a radio button is selected, this way it will be updated once the submit button is pressed. I hope I have been clear enough, code can still be improved but that's for another answer ;) Cheers!
Upvotes: 1