Issue
here is my problem, I have a layout with 3 radioButton to change the language, a class which manages the change of language, it works well, I have a hamberger menu with an item which creates a dialBox by calling my layout which displays the radioButton when I click on it, when checking a radioButton, it doesn't change radioButton, I have a class which handles the listener, I tried .setCheckable (true) in all the different ways, creating some functions, in the activity, in the dialBox, in the class, impossible to see the change when creating the activity, do you have a solution for me because I found a temporary solution but it's very ugly ?
code in activity :
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.langue:
alert2 = new AlertDialog.Builder( this ).setView(
R.layout.choose_language).create();
alert2.show();
String lang = Locale.getDefault().toString();
rdioGrp = alert2.findViewById(R.id.rdioGrpLang);
rdFr = alert2.findViewById(R.id.radioBtnFr);
rdEn = alert2.findViewById(R.id.radioBtnEn);
rdIt = alert2.findViewById(R.id.radioBtnIt);
if(lang.equals("fr") || lang.equals("fr_FR"))rdFr.setChecked(true);
if(lang.equals("it")) {
assert rdIt != null;
rdIt.setChecked(true);
method in class ManageMenu :
public void setRadioListener() {
this.rdgrp.setOnCheckedChangeListener((group, checkedId) -> {
RadioButton rb = group.findViewById(checkedId);
if (rb != null) {
switch (checkedId) {
case R.id.radioBtnFr:
changeLang("fr");
act.recreate();
break;
case R.id.radioBtnEn:
changeLang("en");
act.recreate();
break;
case R.id.radioBtnIt:
changeLang("it");
act.recreate();
break;
default:
changeLang("fr");
act.recreate();
}
}
});
}
// method to change language
public void changeLang(String l) {
Locale locale = new Locale(l);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
this.context.getResources().updateConfiguration(config, this.context.getResources().getDisplayMetrics());
}
xml :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/txtLangue"
android:orientation="horizontal"
android:gravity="center"
>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="@dimen/dim_10"
android:id="@+id/rdioGrpLang"
tools:ignore="UselessParent">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioBtnFr"
android:buttonTint="@color/white"
android:textColor="@color/white"
android:text="@string/langueFr">
</RadioButton>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioBtnEn"
android:text="@string/langueEn"
android:buttonTint="@color/white"
android:textColor="@color/white"
android:layout_marginLeft="@dimen/dim_10"
android:layout_marginRight="@dimen/dim_10">
</RadioButton>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioBtnIt"
android:buttonTint="@color/white"
android:textColor="@color/white"
android:text="@string/langueIt">
</RadioButton>
</RadioGroup>
</LinearLayout>
Solution
To fix the problem I did the following.
I have a new button in XML for recreate, I have deleted recreate() in the class, and I have a new method for onClick to recreate activity. Now the code is clean, radioButton changes when clicked.
Code in activity:
case R.id.langue:
alert2 = new AlertDialog.Builder( this ).setView( R.layout.choose_language).create();
alert2.show();
rdioGrp = alert2.findViewById(R.id.rdioGrpLang);
btnRestartActivity = alert2.findViewById(R.id.btnRestartActivity);
ManageMenu mm = new ManageMenu(getBaseContext(),rdioGrp, this );
mm.setRadioListener();
btnRestartActivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mm.recreateActivity();
}
});
break;
Class:
public void setRadioListener() {
this.rdgrp.setOnCheckedChangeListener((group, checkedId) -> {
RadioButton rb = group.findViewById(checkedId);
if (rb != null) {
switch (checkedId) {
case R.id.radioBtnFr:
changeLang("fr");
break;
case R.id.radioBtnEn:
changeLang("en");
break;
case R.id.radioBtnIt:
changeLang("it");
break;
default:
changeLang("fr");
}
}
});
}
New method in class ManageMenu:
// recreate activity
public void recreateActivity() {
this.act.recreate();
}
XML:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/txtLangue"
android:orientation="horizontal"
android:id="@+id/layout_rdiGrp"
android:gravity="center"
>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="@dimen/dim_10"
android:id="@+id/rdioGrpLang"
tools:ignore="UselessParent">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioBtnFr"
android:buttonTint="@color/white"
android:textColor="@color/white"
android:text="@string/langueFr">
</RadioButton>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioBtnEn"
android:text="@string/langueEn"
android:buttonTint="@color/white"
android:textColor="@color/white"
android:layout_marginLeft="@dimen/dim_10"
android:layout_marginRight="@dimen/dim_10">
</RadioButton>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioBtnIt"
android:buttonTint="@color/white"
android:textColor="@color/white"
android:text="@string/langueIt">
</RadioButton>
</RadioGroup>
</LinearLayout>
<Button
android:layout_below="@+id/layout_rdiGrp"
android:text="@string/btnAppliquer"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
style="@style/myTheme.btn_music_artist_best_record"
android:textColor="@color/white"
android:textAllCaps="false"
android:layout_centerInParent="true"
android:id="@+id/btnRestartActivity"
/>
Answered By - Nicolas Zanforlini
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.