3

This is a follow up question to this question:

Force TextView to mutiline without \n

I got a TextView which is declared like this:

<TextView
        android:id="@+id/myTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.4"
        android:gravity="left|start"
        android:text="TextView"
        android:singleLine="false"/>

In the code I set a long text, and the text view display it as a long single line instead of breaking it to multiple lines. The accepted answer in the linked question suggests setting the android:maxWidth attribute, but I don't want to do it like that. I want the text lines to automatically break if the length of the text exceeds the 0.4 weight as set in the declaration of the text view. Is there a way of doing that and not using a constant size?

As requested this is the text view with its parent:

<LinearLayout
    android:id="@+id/linearLayoutBottomData"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/linearLayoutTopData"
    android:layout_marginBottom="5dp"
    android:layout_marginTop="10dp"
    android:orientation="horizontal"
    android:weightSum="1">

    <TextView
        android:id="@+id/myTextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.4"
        android:gravity="left|start"
        android:text="TextView"
        android:textColor="@android:color/black"
        android:textSize="@dimen/large_font_size"
        android:singleLine="false"/>

    <TextView
        android:id="@+id/myTextView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.4"
        android:gravity="left|start"
        android:text="TextView"
        android:textColor="@android:color/black"
        android:textSize="@dimen/large_font_size"/>
</LinearLayout>
CodeMonkey
  • 11,196
  • 30
  • 112
  • 203
  • Try to follow this answer [here](https://stackoverflow.com/questions/6674578/multiline-textview-in-android) about multiple lines in TextView. – grrigore Nov 12 '17 at 12:19
  • android:layout_weight="0.4". this is the line forcing text view to wrap in particular height . Remove it .If you want to do it in constant size Make TextView scrolable . Or just put TextView inside a Scroll View with constant height – ADM Nov 12 '17 at 12:21
  • @CristianAndreiGrigore that didn't work – CodeMonkey Nov 12 '17 at 12:27
  • @ADM The layout_weight is for the width, not the height (the text view is inside a horizontal linear layout). I want the textview to remain with the 0.4 weight for the width and make the text wrap in this size only – CodeMonkey Nov 12 '17 at 12:28
  • Can you post the parent layout of the textview ? with all the childs – Nongthonbam Tonthoi Nov 12 '17 at 12:28
  • @NongthonbamTonthoi added – CodeMonkey Nov 12 '17 at 12:31
  • set Text View width ='0dp' it will work . iF your Parent layout Orientation is Horizontal – ADM Nov 12 '17 at 12:31
  • In LinearLayout with orientation horzontal you should use width 0dp and if it is veritcal you should use height 0dp. Setting that should work – Nongthonbam Tonthoi Nov 12 '17 at 12:35

1 Answers1

5

Use the below layout I have mentioned in comments too.

<LinearLayout
android:id="@+id/linearLayoutBottomData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/linearLayoutTopData"
android:layout_marginBottom="5dp"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:weightSum="1">

<TextView
    android:id="@+id/myTextView1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="0.4"
    android:gravity="left|start"
    android:text="TextView"
    android:textColor="@android:color/black"
    android:textSize="@dimen/large_font_size"
    android:singleLine="false"/>

<TextView
    android:id="@+id/myTextView2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="0.4"
    android:gravity="left|start"
    android:text="TextView"
    android:textColor="@android:color/black"
    android:textSize="@dimen/large_font_size"/>

ADM
  • 20,406
  • 11
  • 52
  • 83