11

Having a TextView, its width should not go over 1/3 of its parent's width. If its width is smaller than 1/3 of parent, it should have wrap_content behavior. Its horizontal sibling will always start next to it.

Tried following, it always has hard cut of 1/3 and 2/3, so if the text1 has less space than 1/3 the TextView two will not start next to it.

change the LinearLayout to RelativeLayout, then the android:layout_weight="n" does not work

basically, need to define width is wrap_content and the maxWidth does not go over 1/3.

any suggestion?

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

    <TextView 
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:singleLine="true"
        android:ellipsize="end"
    />

    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:singleLine="true"
        android:ellipsize="end"
        android:layout_weight="2"
    />
</LinearLayout>
lannyf
  • 9,865
  • 12
  • 70
  • 152

5 Answers5

4

basically, need to define width is wrap_content and the maxWidth does not go over 1/3.

If that's all you need, then my suggestion is to scrap the Weight approach and dynamically set the TextView's maxWidth value.

Something like this:

tv.setMaxWidth(((LinearLayout)tv.getParent()).getWidth()/3);
user3829751
  • 712
  • 7
  • 20
3

I think that you'll have to dynamically check the parent layout width every time you update the textView, something like (I have tested this code using a button and edit text to change the textView - works without problem) :

<LinearLayout
    android:id="@+id/myLayout" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView 
        android:id="@+id/tvA"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:ellipsize="end"
    />

    <TextView 
        android:id="@+id/tvB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:ellipsize="end"
    />
</LinearLayout>

code:

            TextView tvA = (TextView) findViewById(R.id.tvA);
            TextView tvB = (TextView) findViewById(R.id.tvB);
            LinearLayout myLayout = (LinearLayout) findViewById(R.id.myLayout);


            // code to use when the textView is updated
            // possibly button onClickListener?
            tvA.measure(0, 0);
            int textWidth = tvA.getMeasuredWidth();
            myLayout.measure(0,0);
            int layoutWidth = myLayout.getWidth();

            if (textWidth > (layoutWidth / 3)) {
                tvA.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));
                tvB.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 2.0f));

            } else {
                tvA.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
                tvB.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            }
Mark
  • 9,604
  • 5
  • 36
  • 64
  • I don't want it to be always 1/3 but max to 1/3, it should be allow less than 1/3 and the sibling should next to it without space. – lannyf May 04 '15 at 15:07
  • 1
    Thanks Mark! if cannot achieve with in xml your answer will the one. – lannyf May 04 '15 at 18:36
2

The solution is simple: Your second Textview's width must be "0dp" like the first one.

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

    <TextView 
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:singleLine="true"
        android:ellipsize="end"
    />

    <TextView 
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:ellipsize="end"
        android:layout_weight="2"
    />
</LinearLayout>
Oğuzhan Döngül
  • 7,856
  • 4
  • 38
  • 52
  • sorry, i has the android:layout_width="0dp" and was copy error not there. It does not work. – lannyf May 04 '15 at 17:34
  • This should be the accepted solution as it does not require to programmatically calculate the width. – AlexB Feb 04 '22 at 19:29
0

How about put it in a layout which has 1/3rd width of the parent?

<LinearLayout
  .. this is main parent
  android:weightSum="1" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.33"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>
</LinearLayout>
Froyo
  • 17,947
  • 8
  • 45
  • 73
  • it achieves 1/3rd of parent but does not have "the sibling should flow right if it has less then 1/3rd of parent's width". – lannyf May 23 '15 at 20:49
0

With introduction of the ConstraintLayout this can be achieved like this:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:singleLine="true"
        app:layout_constrainedWidth="true"
        app:layout_constraintEnd_toStartOf="@+id/guideline"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.333"/>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toEndOf="@+id/textView1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:ellipsize="end"
        android:singleLine="true"
        android:text="initial text"/>
</android.support.constraint.ConstraintLayout>
Sergey
  • 1,020
  • 11
  • 22