0

I have set default language as Arabic (ar) and it works as expected on my Alcatel device (version 6 Marshmallow) But when I run this app on Samsung device (version 4.1.2 JellyBean) the toolbars(top & bottom) do not show as RTL layout elements. Check out the images of both below.

Alcatel View working well

enter image description here
.

And Samsung View not working as expected

enter image description here

You can see top toolbar & bottom toolbar's elements, not in the same sequence on both devices. I have two drawer elements attached to the top toolbar(left having Category Info Lists with Gravity Start & right having User account related Info List with Gravity End). So they are not showing well too on Samsung.

The last thing for now is in the screen area I have a vertical recyclerview that shows different layout types according to the data available. The Position of these Layouts is also not correct on Samsung.

Note: the recyclerview part does not behave because of language change and it is also different when I run the app in English.

Edit: my adapter Class where I'm preparing data and setting nested recyclerview also.

package com.qemasoft.alhabibshop.app.controller;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.qemasoft.alhabibshop.app.R;
import com.qemasoft.alhabibshop.app.Utils;
import com.qemasoft.alhabibshop.app.model.MyCategory;
import com.qemasoft.alhabibshop.app.model.MyItem;
import com.qemasoft.alhabibshop.app.model.MyPromotion;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import static com.qemasoft.alhabibshop.app.AppConstants.findStringByName;
import static com.qemasoft.alhabibshop.app.AppConstants.getHomeExtra;


/**
 * Created by Inzimam on 17-Oct-17.
 */

public class MainFragmentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final static int CATEGORY_VIEW = 0;
    private final static int PROMOTION_VIEW = 1;
    private final static int ITEM_VIEW = 2;

    private List<String> keysStrList;
    private CategoryAdapter categoryAdapter;
    private ItemAdapter itemAdapter;

    private List<Object> myAllItemsList = new ArrayList<>();


    private Context context;
    private Utils utils;


    public MainFragmentAdapter(List<String> keysList) {
        this.keysStrList = keysList;
        prepareData();
        Log.e("AllItemTypeSize = ", myAllItemsList.size() + "");
    }

    @Override
    public int getItemViewType(int position) {

        Log.e("getItemViewType ", myAllItemsList.get(position).getClass() + "");
        Object o = myAllItemsList.get(position);
        if (o instanceof List) {
            for (Object obj : (List) o) {
                if (obj instanceof MyCategory) {
                    Log.e("InsideInstenceof", "Success");
                    return CATEGORY_VIEW;
                }
            }
            return ITEM_VIEW;
        } else if (o instanceof MyPromotion) {
            return PROMOTION_VIEW;
        } else {
            return ITEM_VIEW;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        RecyclerView.ViewHolder viewHolder;
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        Log.e("itemType = ", "ViewTypeOnCreate " + viewType);
        switch (viewType) {
            case CATEGORY_VIEW:
                View v1 = inflater.inflate(R.layout.layout_main_frag_categories, parent, false);
                viewHolder = new ViewHolder1(v1);
                break;
            case PROMOTION_VIEW:
                View v2 = inflater.inflate(R.layout.discout_layout, parent, false);
                viewHolder = new ViewHolder2(v2);
                break;
            default:
                View v3 = inflater.inflate(R.layout.layout_main_frag_categories, parent, false);
                viewHolder = new ViewHolder1(v3);
                break;
        }
        this.context = parent.getContext();
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int itemType = holder.getItemViewType();
        Log.e("itemType = ", itemType + "");
        Object o = myAllItemsList.get(position);
        RecyclerView.LayoutManager mLayoutManagerCat = null;
        if (o instanceof List) {
            List<Object> list = (List<Object>) myAllItemsList.get(position);

            int layoutColumns = 2;
            if (list.size() < 4) {
                layoutColumns = 1;
            }
            mLayoutManagerCat =
                    new GridLayoutManager(context, layoutColumns,
                            LinearLayoutManager.HORIZONTAL, false);
        }
        switch (itemType) {
            case CATEGORY_VIEW:
                ViewHolder1 vh1 = (ViewHolder1) holder;
                vh1.getmRecyclerView().setLayoutManager(mLayoutManagerCat);
                vh1.getTitle().setText(findStringByName(keysStrList.get(position)));
                vh1.getmRecyclerView().setAdapter(new CategoryAdapter(
                        (List<MyCategory>) myAllItemsList.get(position)));
                break;
            case PROMOTION_VIEW:
                ViewHolder2 vh2 = (ViewHolder2) holder;
                configureViewHolder2(vh2, position);
                break;
            default:
                ViewHolder1 vh3 = (ViewHolder1) holder;
                vh3.getmRecyclerView().setLayoutManager(mLayoutManagerCat);
                vh3.getTitle().setText(findStringByName(keysStrList.get(position)));
                vh3.getmRecyclerView().setAdapter(new ItemAdapter(
                        (List<MyItem>) myAllItemsList.get(position)));
                break;
        }
    }

    @Override
    public int getItemCount() {
        return keysStrList.size();
    }


    private void configureViewHolder2(ViewHolder2 vh2, int position) {
        MyPromotion promotion = (MyPromotion) myAllItemsList.get(position);
        if (promotion != null) {
            vh2.getTitle().setText(promotion.getTitle());
            vh2.getDescription().setText(promotion.getDescription());
        }
    }

    private void prepareData() {

        List<MyCategory> myCategoryList = new ArrayList<>();
        List<MyItem> myItemList;

        String responseStr = getHomeExtra();
        try {
            JSONObject responseObject = new JSONObject(responseStr);
            Log.e("JSON_Response", "" + responseObject);
            boolean success = responseObject.optBoolean("success");
            if (success) {
                JSONObject homeObject = responseObject.optJSONObject("home");
                JSONObject modules = homeObject.optJSONObject("modules");

                for (int a = 0; a < keysStrList.size(); a++) {
                    Log.e("KeyStr = ", keysStrList.get(a));
                    if (keysStrList.get(a).equals("categories")) {
                        JSONArray featuredCategories = modules.optJSONArray(keysStrList.get(a));
                        for (int i = 0; i < featuredCategories.length(); i++) {
                            JSONObject categoryObject = featuredCategories.getJSONObject(i);
                            MyCategory myCategory = new MyCategory(categoryObject.optString("category_id"),
                                    categoryObject.optString("name"), categoryObject.optString("thumb"));
                            myCategoryList.add(myCategory);
                        }
                        myAllItemsList.add(myCategoryList);
                    } else if (keysStrList.get(a).equals("promotion")) {
                        JSONArray promotionArray = modules.optJSONArray("promotion");
                        JSONObject promotionObject = promotionArray.optJSONObject(0);
                        MyPromotion myPromotion = new MyPromotion(promotionObject.optString("id"),
                                promotionObject.optString("name"),
                                promotionObject.optString("description"));
                        myAllItemsList.add(myPromotion);

                    } else {
                        myItemList = new ArrayList<>();
                        JSONArray featuredProduct = modules.optJSONArray(keysStrList.get(a));
                        for (int i = 0; i < featuredProduct.length(); i++) {
                            JSONObject productObj = featuredProduct.getJSONObject(i);
                            MyItem myItem = new MyItem(productObj.optString("product_id"),
                                    productObj.optString("name"), productObj.optString("dis"),
                                    productObj.optString("price"), productObj.optString("thumb"));
                            myItemList.add(myItem);
                        }
                        myAllItemsList.add(myItemList);
                    }
                }
            } else {
                Log.e("SuccessFalse", "Within getCategories");
            }
        } catch (JSONException e) {
            e.printStackTrace();
            Log.e("JSONEx_CatAdapterTest", responseStr);
        }
    }

    public class GenericList<T> extends ArrayList<T> {
        private Class<T> genericType;

        public GenericList(Class<T> c) {
            this.genericType = c;
        }

        public Class<T> getGenericType() {
            return genericType;
        }
    }

}
Inzimam Tariq IT
  • 6,548
  • 8
  • 42
  • 69
  • 1
    1 .Please check this Link for Full RTL Support on Android Developer Blogs: https://android-developers.googleblog.com/2013/03/native-rtl-support-in-android-42.html 2. Are you correctly give the `Layout Manager` to `RecyclerView` on changing the language of your app. – Abhinav Suman Nov 23 '17 at 09:11
  • Ok, And what about recyclerview that do not working well in english as well. – Inzimam Tariq IT Nov 23 '17 at 09:19
  • If you want `RecyclerView` to work as `GridView` then you have to handle it either by using `Recyclerview's``ItemDecoration` Class and in another way you can do it just By Passing the `GridLayoutManager` with `SpanCount` in the method. Constructor `new GridLayoutManager(activity, 2)` is about `GridLayoutManager(Context context, int spanCount)` where `spanCount` is the number of columns in the grid. Finished rather you can **check here** : [https://stackoverflow.com/a/40587169/5343866](https://stackoverflow.com/a/40587169/5343866) – Abhinav Suman Nov 23 '17 at 09:38
  • @AbhinavSuman I'm already using GridLayoutManager. if the item count is < 4 spanCount is 1 and spanCount 2 otherwise which is working perfect. but the problem is with the different device – Inzimam Tariq IT Nov 23 '17 at 09:44
  • 1
    Maybe then it is Device specific Issue. @Inzimam Tariq IT Actually, either try to set the single item `Layout Params` dynamically or please put the code, so that it can make some help to find out the issue. – Abhinav Suman Nov 23 '17 at 10:02
  • Ok let me edit the question – Inzimam Tariq IT Nov 23 '17 at 10:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/159662/discussion-between-abhinav-suman-and-inzimam-tariq-it). – Abhinav Suman Nov 23 '17 at 10:05

1 Answers1

2

Full RTL support was added in Android 4.2. In Android 4.1 you have only limited support for bidirectional text in TextView and EditText.

Check this article on google blog: https://android-developers.googleblog.com/2013/03/native-rtl-support-in-android-42.html

Kriczer
  • 497
  • 2
  • 12