0

I am Changing the language of my app to Arabic using the piece of code below. But when it changes, resources inside a RecyclerView are not changing. It is only changing the direction (within Activity) all the other views are changing the values. (mipmap, string), but values for the RecyclerView inside the fragment are not changing.

Class File

Locale locale;
Sessions session = new Sessions(context);
if (session.getLanguage().equals("1")) {
 locale = new Locale("en-rU");
}
else{
 locale = new Locale("ar");
}
Locale.setDefault(locale);
Configuration config = new Configuration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
  config.setLocale(locale);
} else {
  config.locale = locale;
}
context.getResources().updateConfiguration(config, 
context.getResources().getDisplayMetrics());
}

Layout xml File

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/spotList"
    tools:listitem="@layout/recycler_spot"/>

Adapter Class

public class ItemsAdapter extends RecyclerView.Adapter<ItemsAdapter.CustomViewHolder> {
Context context;
OnItemClick onItemClick;

public ItemsAdapter(Context context){
    this.context=context;
}


@NonNull
@Override
public CustomViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view= LayoutInflater.from(context).inflate(R.layout.recycler_items,viewGroup,false);
    return new CustomViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull CustomViewHolder customViewHolder, int i) {
}

@Override
public int getItemViewType(int position) {
    return super.getItemViewType(position);
}

@Override
public int getItemCount() {
    return 6;
}

public class CustomViewHolder extends RecyclerView.ViewHolder {
    public CustomViewHolder(@NonNull View itemView) {
        super(itemView);
    }

}

public interface OnItemClick{
    public void onItemClick(int position);
}

}
Elletlar
  • 3,136
  • 7
  • 32
  • 38

3 Answers3

1

i found the solution.

i call this in the constructor of Recyclerview adapter. i dont know if it is the correct method,still my problem is solved

 @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
public static void setResources(Context context) {
    Locale locale;

    Sessions session = new Sessions(context); 
    //Log.e("Lan",session.getLanguage());
    if (session.getLanguage().equals("1")) {
        locale = new Locale("en-rUS");
    }else{
        locale = new Locale("ar");
    }

    Resources res=context.getResources();
    DisplayMetrics dm=res.getDisplayMetrics();
    android.content.res.Configuration configuration=res.getConfiguration();
    configuration.setLocale(locale);
    res.updateConfiguration(configuration,dm);
}
0

I change my app language using below function.

public void setUserMobileLanguage(String sUserLanguageCode) {
        try {
            String[] arrLngCode = sUserLanguageCode.split("-");
            if (arrLngCode.length > 1)
                sUserLanguageCode = arrLngCode[0];

            if (sUserLanguageCode.equals("he")) {
                sUserLanguageCode = "iw";
            }

            if (sUserLanguageCode.equals("en")) {
                new LocalHelper().setLocal(mContext, sUserLanguageCode); //set app en language
                getHelper().setAppLanguage(sUserLanguageCode);  //store for later use
            } else {
                if (!Utilty.isLanguageAvailableInMobile(sUserLanguageCode)) {  //check language available in mobile
                    sUserLanguageCode = "en";//set english as app language
                    makeToast(getString(R.string.selected_language_is_not_support_by_your_mobile));
                }
                new LocalHelper().setLocal(mContext, sUserLanguageCode); //set app language
                getHelper().setAppLanguage(sUserLanguageCode);  //store for later use
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Check System Support languages or not using below code.

public static boolean isLanguageAvailableInMobile(String lngCode) {
        return Arrays.toString(Locale.getAvailableLocales()).contains(lngCode);
    }

Example of my string resources file.

enter image description here

Using the above code and resources file I can change my app layout and text.

Kaushal Panchal
  • 1,785
  • 1
  • 11
  • 27
0

When you set adapter from your activity (NOT from fragment), make sure to call getBaseContext as Context parameter:

yourAdapter = new YourAdapter(userIDArr, profileIDArr, ... getBaseContext());

yourRecyclerView.setAdapter(yourAdapter);
ekashking
  • 387
  • 6
  • 19