Android Button Text on same line

So I have a button on each row in my expandable list, and below is my XML layout file for the text view and button (which are supposed to be on the same line), but the text "Assign Badge" is too long and unless I make the text size smaller, it doesn't fit, with single line = true, it doesn't help because then it will not show half the text, without it, the text in the button will appear on two lines...is there a way to get it so the button could be wider? Thanks.

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

<TextView
    android:id="@+id/listItem"
    android:layout_width="match_parent"
    android:layout_height="35dp"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:gravity="center_vertical"
    android:layout_weight="0.2"
    android:textSize="17dip"
    android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:layout_weight="0.8"
    android:textSize="8dp"
    android:singleLine="true"
    android:text="Assign Badge"
    android:id="@+id/assign" />

</LinearLayout>

Upvotes: 0

Views: 3172

Answers (4)

MD.Riyaz
MD.Riyaz

Reputation: 412

android:singleLine="true" is deprecated. Use this instead:

android:maxLines="1"

Like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="55dip"
android:orientation="horizontal" >
<TextView
    android:id="@+id/listItem"
    android:layout_width="0dp"
    android:layout_height="35dp"
    android:layout_weight="0.7"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:gravity="center_vertical"
    android:text="TEST" 
    android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
    android:textSize="17dip" />

<Button
    android:id="@+id/assign"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="0.3"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:maxLines="1"
    android:text="Assign Badge"
    android:textSize="8dp" />
</LinearLayout>

Upvotes: 0

snowdragon
snowdragon

Reputation: 439

If you want to make your wider you can modify the weights of the two elements.

Using weights automatically distributes the space between the two elements taking the relations of the width into account. The weight attribute in android seems to function counter-intuitive though (the element with the bigger weight occupies a smaller space).

In my tests using a nexus 5 this led to the desired results:

(I left out unchanged code for readability)

[...]
<LinearLayout 
    [...]

    <TextView
        [...]
        android:id="@+id/listItem"
        android:layout_weight="0.3" />

    <Button
        [...]
        android:layout_weight="0.7"
        android:id="@+id/assign" />

</LinearLayout>

Note the weight_sum element in your LinearLayout though. If your weights don't add up to this number the layout will be smaller or bigger than the layout. If you remove the attribute the elements will occupy all available space in the layout.

Upvotes: 0

Chitrang
Chitrang

Reputation: 5097

See if this helps,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="55dip"
android:orientation="horizontal" >
<TextView
    android:id="@+id/listItem"
    android:layout_width="0dp"
    android:layout_height="35dp"
    android:layout_weight="0.7"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:gravity="center_vertical"
    android:text="TEST" 
    android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
    android:textSize="17dip" />

<Button
    android:id="@+id/assign"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="0.3"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:singleLine="true"
    android:text="Assign Badge"
    android:textSize="8dp" />
</LinearLayout>

Upvotes: 2

Prasanth Louis
Prasanth Louis

Reputation: 5076

You can use padding

    android:paddingLeft="xxdip"
    android:paddingRight="xxdip"

where xx is your value

Upvotes: 0

Related Questions