-1

I have Radiogroup and inside two radio button is there.But I cannot able to get the string value of both the radio button.

Here is my code:-

public  void checkFieldsForEmptyValue(){
        phonenumber=phone_number.getText().toString();
        String password=Password.getText().toString();
        RadioGroup radioGroup=(RadioGroup) findViewById(R.id.rg_lgoinscreen);
        RadioButton radioButton_student=(RadioButton)findViewById(R.id.Radio_button_student);
        RadioButton radioButton_teacher=(RadioButton)findViewById(R.id.Radio_button_teacher);
        String radio_button_student=Integer.toString(radioButton_student.getId());

        if(phonenumber.length() > 0 && password.length() > 0 && radio_button_student.length()>0)
        {
            Toast.makeText(this, "Else working", Toast.LENGTH_LONG).show();
         login.setEnabled(true);
        }

Here is the XML file

<RadioGroup
        android:id="@+id/rg_lgoinscreen"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:layout_margin="1dp"
        android:layout_below="@id/login_password"
        android:orientation="horizontal"
        >
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:buttonTint="@color/white"
            android:text="Student"
            android:textColor="@color/white"
            android:id="@+id/Radio_button_student"
            android:layout_marginLeft="19dp"/>
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="50dp"
            android:buttonTint="@color/white"
            android:text="Teacher"
            android:textColor="@color/white"
            android:id="@+id/Radio_button_teacher"/>
    </RadioGroup>

2 Answers2

0

I don't know why are you use

Integer.toString(radioButton_student.getId())

You just need the string of Radio Button then just use this :

radioButton_student.getText()

And if you need to check radioButton is checked or not,use:

radioButton_student.isChecked()
manhtuan21
  • 2,388
  • 2
  • 10
  • 24
0

Use this code

public  void checkFieldsForEmptyValue(){
    phonenumber=phone_number.getText().toString();
    String password=Password.getText().toString();
    RadioGroup radioGroup=(RadioGroup) findViewById(R.id.rg_lgoinscreen);
    RadioButton radioButton_student=(RadioButton)findViewById(R.id.Radio_button_student);
    RadioButton radioButton_teacher=(RadioButton)findViewById(R.id.Radio_button_teacher);

` int selectedId = radioGroup.getCheckedRadioButtonId();
  radioButton = (RadioButton) findViewById(selectedId);
   String radio_button_student=Integer.toString(radioButton.getText());

    if(phonenumber.length() > 0 && password.length() > 0 && radio_button_student.length()>0)
    {
        Toast.makeText(this, "Else working", Toast.LENGTH_LONG).show();
     login.setEnabled(true);
    }
Mayank Bhatnagar
  • 1,316
  • 2
  • 13
  • 21