Search code examples
androidandroid-layoutlistviewitem

Android ListView Item layout variations


I have a ListView (will move to RecyclerView) with a ListView Item XML layout that contains 3 TextViews inside a LinearLayout. My user preferences allow for left-handed or right-handed use. In which case, I want to change the order of the TextViews in the Item Layout file.

What is the preferred Design Pattern here? How do I do that?

  1. Do I create two different Item XML Layout files, one for left-hand and one for right-hand? Which seems like a duplication of effort with same TextViews duplicated in different XML layout files; just changing the order.
  2. Do I programmatically alter the order of the TextViews in code? How?
  3. Is there some other way I am not aware of?

Just looking to learn and do it the right way.


Solution

  • The duplicate post mentioned by Sandi did what I wanted very easily, and without any duplication of XML files or code to rearrange order. Thanks to everyone that responded and got me here. I just wanted to make sure that I posted what easily worked for me.

    I have a TAB with two different ListViews each with their own ListItem XML layouts, and this reversed the order perfectly in the onCreateView for the TabFragment.java in the main Layout and in each of the ListItem layouts!

        // Inflate this Tab's checklist XML
        root = inflater.inflate(R.layout.tab_fragment, container, false);
        if (leftHanded) {
            LinearLayout linearLayout = root.findViewById(R.id.tabLinearLayout);
            linearLayout.setLayoutDirection(LinearLayout.LAYOUT_DIRECTION_RTL);
        }