1

Let's say one has a simple series of TextViews inside LinearLayouts like the ones below, with text dynamically populating to the respective TextViews.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/textView2" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/textView3" />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/textView4" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/textView5" />
    </LinearLayout>
</LinearLayout>

How could one make it that if the text of 2 TextViews, let's say textView2 and textView3, or textView4 and textView5, are too wide to fit inline, then the orientation of their parent LinearLayout dynamically changes to vertical, to place one above the other instead, so that the text isn't broken off by the end of the screen in an odd way?

Thanks in advance!

inferKNOX
  • 109
  • 1
  • 3
  • 12

1 Answers1

1

So in order to change layout orientation programatically:

LinearLayout layout = /* ... */;
layout.setOrientation(LinearLayout.VERTICAL);

You can check if textView fit by calling getLineCount(). You can play with both of those to get expected result.

What I would suggest tho is to change the layout to vertical, if you accept this scenario anyway.

You could also use autofitTextView control so that it would always take only 1 line and change the size of the font dynamically, if you it's ok for you. You can find one here:

https://github.com/grantland/android-autofittextview

Rybzor
  • 183
  • 3
  • 12
  • Thank you very much for your response. I will try use what you've said to test the textview linecount and change the layout to vertical if it exceeds 1. I need it to stay horizontal, because most values fit, it's just occasionally that it's too large. It is for RecyclerView items, so if I was to make the font change depending on the width, then the whole interface would look a bit funny. :) As soon as I get it working, I'll approve your answer. ;) – inferKNOX Sep 14 '16 at 20:45
  • Yeah it really depends on the case. If it is recyclerView... I would also suggest to consider using two viewHolders (one with vertical view and one with horizontal view). You can switch them dynamically depends on the circumstances. You can find more about this here: http://stackoverflow.com/questions/26245139/how-to-create-recyclerview-with-multiple-view-type – Rybzor Sep 14 '16 at 20:55
  • I managed to get just what I want with the following: textView3.post(new Runnable() { @Override public void run() { int lineCount = textView3.getLineCount(); if (lineCount > 1) { linearLayout.setOrientation(LinearLayout.VERTICAL); } else { linearLayout.setOrientation(LinearLayout.HORIZONTAL); } } }); Thanks for your help!! – inferKNOX Sep 14 '16 at 23:06
  • 1
    'linearLayout.setOrientation(textView3.getLineCount() > 1 ? LinearLayout.VERTICAL : LinearLayout.HORIZONTAL)' this would be much easier approach for me : D. Glad to help. – Rybzor Sep 15 '16 at 06:41
  • Thanks for the suggestion. I've shortened it to that, but kept it in the `post` or else `getLineCount()` always returns 0. [See here](http://stackoverflow.com/questions/12037377/how-to-get-number-of-lines-of-textview). – inferKNOX Sep 16 '16 at 14:39