Reputation: 484
I cannot uncheck initial radiobutton after setting default checked button.
<RadioGroup
android:id="@+id/stateRadioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RadioButton
android:id="@+id/attendanceRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="School"
android:textSize="18sp" />
<RadioButton
android:id="@+id/hometimeRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Home"
android:textSize="18sp" />
</RadioGroup>
I cannot understand why it happened. I already read this article from stackoverflow. But it is not helpful to me and also too old post.
I remove the line, android:checkedButton="@+id/attendanceRadioButton"
I works well. (I mean never both buttons are clicked simultaneously) But there is no default checked button. So this is not what I want.
I already add 2 lines in onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
But not helpful. (Actually these views are on Fragment.)
stateRadioGroup.clearCheck();
attendanceRadioButton.setChecked(true);
Same results as above images.
Android Studio : 3.0.1
Gradle : 4.1
compileSdkVersion : 27
minSdkVersion : 21
targetSdkVersion : 23
Running Device : LG V10, Android 7.0
==============
I made new Project for solving this problem. I think this problem has a relationship with Fragment. But I don't know how to solve this. So, please help me :(
There is MainActivity
and RadioButtonFragment
and I will inflate Fragment in RadioButtonFragment
from MainActivity
like below codes.
<android.support.constraint.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="Your Package Name">
<fragment
android:id="@+id/fragmentLayout"
android:layout_width="0dp"
android:layout_height="0dp"
android:name="io.github.tyeolrik.testradiobutton.RadioButtonFragment"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.add(R.id.fragmentLayout, new RadioButtonFragment());
fragmentTransaction.commit();
setContentView(R.layout.activity_main);
}
<!-- Of course there is ConstraintLayout which wraps RadioGroup -->
<RadioGroup
android:id="@+id/buttonGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:checkedButton="@+id/button1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RadioButton
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="School" />
<RadioButton
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Home" />
</RadioGroup>
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_radio_button, container, false);
return view;
}
It works well when I just make a RadioGroup and some RadioButtons in Activity. But, in that case, making RadioButtons in Fragment, two buttons checking problem is happened.
Is there any idea to solve this?
Not android.support.v4.app.Fragment
But android.app.Fragment
In MainActivity.java
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
In RadioButtonFragment.java
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
Upvotes: 1
Views: 1548
Reputation: 753
Try not setting check attribute for radioButton. Instead, add this to your radioGroup
android:checkedButton="@+id/attendanceRadioButton"
Edit:
Also, you dont have to init your radioButtons, you can listen to it using your radioGroup
Upvotes: 1
Reputation: 1453
Try this, remove below code from your onCreateView() method
stateRadioGroup.clearCheck();
attendanceRadioButton.setChecked(true);
And add the following in your xml.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<RadioGroup
android:id="@+id/stateRadioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/attendanceRadioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:text="School" />
<RadioButton
android:id="@+id/hometimeRadioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Home" />
</RadioGroup>
</LinearLayout>
Upvotes: 2