RadioButton selection in the radio group

I am new to android and I tried a sample program using radio groups. I announced 2 radio buttons under the radio group. I wanted the radio button to be selected in RadioGroup. I wanted to use it in the if and elseif clause of the anonymous class new onClickListener.

I tried to declare an integer variable radio_selected and get the return value radgrp.getCheckedRadioButtonId () radio_selected = radgrp.getCheckedRadioButtonId (); and use the constant for comparison in the if and else if condition, since

if (radio_selected == R.id.rdb1 && (edit1.getText (). toString (). equals ("admin")) && (edit2.getText (). toString (). equals ("administrator")))

I tried to print the value of radio_selected. His seal me -1, which means an empty choice. But radgrp.getCheckedRadioButtonId (); returns an integer value. However, the above program compiles and works fine, but I wanted to know why I could not use the radio_selected constant for comparison?

.java file

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(); } } }); } } 

main.xml file

 <?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> 

R.java File

 /* 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; } } 
+2
source share
3 answers
  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; } } }); 
+2
source
Hey Angus. I think you almost got it right. The reason your radio_selected variable prints this value is because you are trying to get which switch is selected at the time the activity was created and there are no switches selected, so its value will be -1.

In addition, I recommend that you implement OnCheckedChangeListener in your activity class, which will allow you to override the onCheckedChanged method to grab the switch selection. In your case, you declared your radio_selected as the final variable, which is a mistake! The final means that after defining this variable, its value cannot be changed, so even if it works, it will work for the first time, and not again. So, with these tips, your activity might 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(); } } } 

Please note that I set the radio_selected value when the radio button is selected, so it will be updated after clicking the submit button. Hopefully I was clear enough, the code can still be improved, but for a different answer;) Hooray!

+1
source
  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();} 
+1
source

Source: https://habr.com/ru/post/1440688/


All Articles