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!