0

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.

JJJones_3860
  • 1,382
  • 2
  • 15
  • 35
  • you might wanna prefer RTL layout. Android studio provides it in the menu bar. – Rohit Rawat Oct 14 '19 at 12:45
  • My understanding is that is just the how text is displayed in the TextView. That it does not alter the order of TextViews in a linearlayout. Am I wrong? – JJJones_3860 Oct 14 '19 at 12:47
  • no. that's what RTL does. it does alters the layout for left handed use and doesn't change the order of the views – Rohit Rawat Oct 14 '19 at 12:49
  • The preferred way is to have a different view type for each item variant in a `ListView` or `RecyclerView`. RTL is used for locale-specific changes, OP stated that the user can set whether to use the app left-handed or right-handed, regardless of locale. I would have separate XMLs for each use case, with a shared XML for the 3 `TextView`s inside a `` tag. – Sandi Oct 14 '19 at 12:51
  • This is also a duplicate of https://stackoverflow.com/questions/13903611/reverse-the-direction-of-a-linearlayout – Sandi Oct 14 '19 at 13:02
  • The `right-hand or left-hand` settings can be saved somewhere in SharedPrefs or something. Then we create dedicated left-handed and right-handed views. And we can just use `ViewHolder` pattern to decide what view to bind to the ListView/ RecyclerView adapter with `getItemViewType`. This way the implementation doesn't depend on RTL, and the user has flexibility to change this setting independent of locale. – theThapa Oct 15 '19 at 20:59

3 Answers3

1

You can try to have 2 sets of TextViews in one xml layout. Changing the visibility for them will make the trick for left- or right-handed people. But, anyway, this is duplication of view.

Or you can use ConstraintLayout and change ConstraintSet for views programmatically depends on what preference is chosen.

val constrSet = ConstraintSet()
constrSet.clone(parentLayout)
constrSet.connect(textView1.id, ConstraintSet.END, textView2.id, ConstraintSet.START)
constrSet.applyTo(parentLayout)
0

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);
    }
JJJones_3860
  • 1,382
  • 2
  • 15
  • 35
0

You can keep one Layout file and alter them from code(based on the conditions):

left -> child.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);

right -> child.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);

Denathan
  • 141
  • 10