Search code examples
androidandroid-layoutandroid-recyclerviewlocaleandroid-resources

RecyclerView not changing resource values when language change


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);
}

}

Solution

  • 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);
    }