Reputation:
I have a dialog. I wanted to change checked attributes but I couldn't. I want to check checkbox if radio button is checked.
It is activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="336dp"
android:text="TextView"
android:textSize="40sp"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/start" />
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="64dp"
android:text="Button"
app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
It is activity_dialog.xml which provides my custom dialog.
<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@+id/start"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<RadioButton
android:id="@+id/singleA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:checked="false"
android:onClick="tytClick"
android:text="TYT" />
<LinearLayout
android:id="@+id/linearTYT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
android:orientation="vertical"
android:visibility="visible"
tools:context=".MainActivity">
<CheckBox
android:id="@+id/multi1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:checked="false"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Maths" />
<CheckBox
android:id="@+id/multi2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:checked="false"
android:text="Turkish"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"/>
<CheckBox
android:id="@+id/multi3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:checked="false"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="English" />
</LinearLayout>
<RadioButton
android:id="@+id/singleB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:checked="false"
android:onClick="aytClick"
android:text="AYT" />
</RadioGroup>
</android.support.constraint.ConstraintLayout>
And is MainActivity.class
public class MainActivity extends AppCompatActivity {
final Context context = this;
Button start;
TextView textView;
CheckBox multi1, multi2,multi3;
RadioButton singleA, singleB;
LinearLayout linearTYT;
RadioGroup radioGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = findViewById(R.id.start);
textView = findViewById(R.id.textView);
multi1 = findViewById(R.id.multi1);
multi2 = findViewById(R.id.multi2);
multi3 = findViewById(R.id.multi3);
singleA = findViewById(R.id.singleA);
singleB = findViewById(R.id.singleB);
linearTYT = findViewById(R.id.linearTYT);
radioGroup = findViewById(R.id.radioGroup);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.activity_dialog);
dialog.setTitle("Choose");
dialog.show();
}
});
if (singleA.isChecked()) {
multi1.setChecked(true);
multi2.setChecked(true);
multi3.setChecked(true);
linearTYT.setVisibility(View.VISIBLE);
} else {
multi1.setChecked(false);
multi2.setChecked(false);
multi3.setChecked(false);
linearTYT.setVisibility(View.GONE);
}
}}
I also tried to View.GONE the Linear Layout below of the Radio Button but it crashed. I think I can't change the view of dialog after appearing it. Or I couldn't do right.
Upvotes: 0
Views: 70
Reputation: 923
Welcome to StackOverflow.
Seems like you are a beginner. I am providing the solution but some modifications might be needed for the perfect functioning.
You need to modify your OnClickListener.
First of all, you are using your activity_dialog.class (Which I believe to be activity_dialog.xml
) in a dialog. So, you simply can't get it's views through findViewById
method of the Activity Class. There is a setOnCheckChangeListener
method for achieving what you want. It is similar to Button.setOnClickListener()
.
The OnClickListener -
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.activity_dialog);
dialog.setTitle("Choose");
dialog.show();
multi1 = dialog.findViewById(R.id.multi1);
multi2 = dialog.findViewById(R.id.multi2);
multi3 = dialog.findViewById(R.id.multi3);
singleA = dialog.findViewById(R.id.singleA);
singleB = dialog.findViewById(R.id.singleB);
singleA.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
multi1.setChecked(true);
multi2.setChecked(true);
multi3.setChecked(true);
}else{
multi1.setChecked(false);
multi2.setChecked(false);
multi3.setChecked(false);
}
}
});
singleB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
multi1.setChecked(false);
multi2.setChecked(false);
multi3.setChecked(false);
}else{
multi1.setChecked(true);
multi2.setChecked(true);
multi3.setChecked(true);
}
}
});
}
});
Some Tweaks will be needed as per your needs. Good Luck!!
Upvotes: 2