skip to Main Content

I want to set the radio button checked by taking the value from the previous activity through intent. But it’s not working.

Here’s the intent I send from the MyProfileActivity to EditProfileActivity.

Intent intent = new Intent(getContext(), EditProfileActivity.class);
                intent.putExtra("nameResult", nameResult);
                intent.putExtra("emailResult", emailResult);
                intent.putExtra("phoneResult", phoneResult);
                intent.putExtra("genderResult", genderResult);
                intent.putExtra("dobResult", dobResult);
                startActivity(intent);

Here’s the intents I’m receiving from MyProfileActivity,

Intent intent = getIntent();
    binding.updateName.setText(intent.getStringExtra("nameResult"));
    binding.updateEmail.setText(intent.getStringExtra("emailResult"));
    binding.updateNumber.setText(intent.getStringExtra("phoneResult"));
    String selectedRadioTxt = getIntent().getStringExtra("genderResult");
    binding.updateDob.setText(intent.getStringExtra("dobResult"));

I want to set the radio button checked. I have also checked the value using toast and made sure that value is coming right. Now the value is coming male. But the radio button male is not getting selected after this code.

String male = "male";
String female = "female";

RadioButton maleButton = findViewById(R.id.male);
RadioButton femaleButton = findViewById(R.id.female);

    if(selectedRadioTxt.equals(male)){
        maleButton.setChecked(true);
        Toast.makeText(this, male, Toast.LENGTH_SHORT).show();
    }
    else if(selectedRadioTxt.equals(female)){
        femaleButton.setChecked(true);
        Toast.makeText(this, female, Toast.LENGTH_SHORT).show();
    }

Here is the xml file,

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/frameLayout5"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".EditProfile" >

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/editDp"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_marginTop="50dp"
        android:src="@drawable/dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/updateName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="40dp"
        android:layout_marginStart="40dp"
        android:layout_marginTop="30dp"
        android:background="@drawable/custom_edit_text"
        android:ems="10"
        android:fontFamily="@font/amarante"
        android:hint="@string/full_name"
        android:importantForAutofill="no"
        android:inputType="textPersonName"
        android:padding="10dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editDp" />

    <EditText
        android:id="@+id/updateEmail"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="@drawable/custom_edit_text"
        android:ems="10"
        android:fontFamily="@font/amarante"
        android:hint="@string/email"
        android:importantForAutofill="no"
        android:inputType="textEmailAddress"
        android:padding="10dp"
        app:layout_constraintEnd_toEndOf="@+id/updateName"
        app:layout_constraintStart_toStartOf="@+id/updateName"
        app:layout_constraintTop_toBottomOf="@+id/updateName" />

    <TextView
        android:id="@+id/updateNumber"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="@drawable/custom_edit_text"
        android:ems="10"
        android:textSize="18sp"
        android:fontFamily="@font/amarante"
        android:hint="@string/mobile_number"
        android:importantForAutofill="no"
        android:inputType="phone"
        android:padding="10dp"
        app:layout_constraintEnd_toEndOf="@+id/updateEmail"
        app:layout_constraintStart_toStartOf="@+id/updateEmail"
        app:layout_constraintTop_toBottomOf="@+id/updateEmail" />


    <TextView
        android:id="@+id/updateDob"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="@drawable/custom_edit_text"
        android:ems="10"
        android:fontFamily="@font/amarante"
        android:hint="@string/date_of_birth"
        android:importantForAutofill="no"
        android:inputType="textPersonName"
        android:padding="10dp"
        android:textSize="18sp"
        app:layout_constraintEnd_toEndOf="@+id/updateGender"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="@+id/updateGender"
        app:layout_constraintTop_toBottomOf="@+id/updateGender" />

    <Button
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="72dp"
        android:layout_marginEnd="30dp"
        android:layout_marginStart="30dp"
        android:layout_marginTop="20dp"
        android:background="@drawable/bg_button"
        android:fontFamily="@font/aclonica"
        android:hapticFeedbackEnabled="true"
        android:text="@string/update"
        android:textAllCaps="false"
        android:textSize="18sp"
        android:textStyle="bold"
        app:backgroundTint="@null"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@+id/updateDob"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="@+id/updateDob"
        app:layout_constraintTop_toBottomOf="@+id/updateDob"
        app:layout_constraintVertical_bias="0.451" />

    <RadioGroup
        android:id="@+id/updateGender"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="@drawable/custom_edit_text"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="@+id/updateNumber"
        app:layout_constraintStart_toStartOf="@+id/updateNumber"
        app:layout_constraintTop_toBottomOf="@+id/updateNumber">

        <RadioButton
            android:id="@+id/male"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:fontFamily="@font/amarante"
            android:onClick="checkButton"
            android:text="@string/male" />

        <RadioButton
            android:id="@+id/female"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:fontFamily="@font/amarante"
            android:onClick="checkButton"
            android:text="@string/female" />

        <RadioButton
            android:id="@+id/other"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:fontFamily="@font/amarante"
            android:onClick="checkButton"
            android:text="@string/other" />
    </RadioGroup>
</androidx.constraintlayout.widget.ConstraintLayout>

But it’s not getting selected.

3

Answers


  1. None gets checked cause the String.valueOf(R.id.male) returns a value different from what you are equating it to in the if statement.

    String.valueOf(R.id.male) is an int which is just converted to a string. Meaning selectedRadioTxt can never be equal to String.valueOf(R.id.male) or String.valueOf(R.id.female)

    In other wards:Eg. selectedRadioTx = "male" but String.valueOf(R.id.male) ="some Int vale eg 20133321"

    Solution
    Change your if statement to:

    if(selectedRadioTxt.trim().equals("male")){
            binding.male.setChecked(true);
            Toast.makeText(this, "male", Toast.LENGTH_SHORT).show();
        }
        else if(selectedRadioTxt.trim().equals("female")){
            binding.female.setChecked(true);
            Toast.makeText(this, "female", Toast.LENGTH_SHORT).show();
        }
    

    Make sure you take note of the caps on the Strings male != Male, and female != "Female". Make sure the value u get from intent selectedRadioTx either equals male or female as per for the solution in the if statement.

    Login or Signup to reply.
  2. Can you check value of genderResult in first activity before intent

    Login or Signup to reply.
  3. As you are using RadioGroup, can you please try

    RadioGroup gender = findViewById(R.id.updateGender);
    
    if(selectedRadioTxt.trim().equals(male.trim())) {
        gender.check(R.id.male);
    } else {
        gender.check(R.id.female);
    }
    

    I used trim() to remove unnecessary spaces. You can avoid if not necessary.

    UPDATE

    This is full functional code.

    MainActivity.java

    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            String genderResult = "male";
    
            Button button = findViewById(R.id.button);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
                    intent.putExtra("genderResult", genderResult);
                    startActivity(intent);
                }
            });
        }
    }
    

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    SecondActivity.java

    public class SecondActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_second);
    
            String male = "male";
            String female = "female";
    
            String selectedRadioTxt = getIntent().getStringExtra("genderResult");
    
            RadioGroup gender = findViewById(R.id.gender);
    
            if(selectedRadioTxt.equals(male)) {
                gender.check(R.id.male);
            } else if(selectedRadioTxt.equals(female)) {
                gender.check(R.id.female);
            }
    
        }
    }
    

    activity_second.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".SecondActivity">
    
        <RadioGroup
            android:id="@+id/gender"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
    
            <RadioButton
                android:id="@+id/male"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Male" />
            <RadioButton
                android:id="@+id/female"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Female" />
        </RadioGroup>
    </LinearLayout>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search