How to stop checkbox from disappearing in code below when text in the TextView is too long? I'm not interested in hardcoding max_width for the TextView and I want to display my whole text.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/tv"
style="@style/Text"
android:layout_weight="0"
android:text="@string/multiple_sounds" />
<CheckBox
android:id="@+id/cb"
style="@style/Text"
android:layout_gravity="right"
android:layout_weight="1"/>
</LinearLayout>
styles.xml
<style name="Text">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:padding">@dimen/margin</item>
<item name="android:textSize">@dimen/text</item>
<item name="android:textAlignment">center</item>
</style>
I would use weight. Add android:weightSum
to your LinearLayout
with value 1
.
For each element in your LinearLayout
add weight. For example 0.8
for textview and 0.2 for Checkbox
.
Then set width to 0dp
for each element !
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="@+id/tv"
style="@style/Text"
android:layout_weight="0.8"
android:text="@string/multiple_sounds" />
<CheckBox
android:id="@+id/cb"
style="@style/Text"
android:layout_gravity="right"
android:layout_weight="0.2"/>
</LinearLayout>
And update your style :
<style name="Text">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">0dp</item>
<item name="android:padding">@dimen/margin</item>
<item name="android:textSize">@dimen/text</item>
<item name="android:textAlignment">center</item>
</style>