As described in the title, I am trying to have 2 TextViews next to eachother in a LinearLayout. Both of the textviews for example contain 10 words each. In that case, I want them to be 50% in width. However, when the first TextView has 10 words, and the other one has 1 word, I would like the first TextView to be wider (~ like 90% or so). How can I do so?
Note: I did try to search, but the only things I found was how to resize the font in a TextView.
I've added a bit of visual of how the TextViews look (don't mind the Android Studio random hover borders).
Also my current code, in case you want to help:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/title"
android:text="My title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textSize="16sp"
android:maxLines="1"
android:ellipsize="end"/>
<TextView
android:id="@+id/my_other_title"
android:text="My other title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="8"
android:textAlignment="textEnd"
android:maxLines="1"
android:ellipsize="end"/>
</LinearLayout>
Note: I would prefer to use only xml for it, but if it is only possible programatically, then that will do
In order to do so, you'll have to calculate the estimate weight of both of the TextViews, using the length of the text. The length of the the other title will be used as weight for the title, and the length of the title will be used for the weight of the other title.
LinearLayout.LayoutParams titleParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
titleParams.weight = myOtherTitle.getText().length();
LinearLayout.LayoutParams myOtherTitleParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
myOtherTitleParams.weight = title.getText().length();
title.setLayoutParams(titleParams);
myOtherTitle.setLayoutParams(myOtherTitleParams);