Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button is not wrapping its content

I am using a simple horizontal LinearLayout to put 3 buttons next to each other, all the same width. I achieve this by the following xml text:

   <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/activity_vertical_margin"
            android:baselineAligned="false" >

            <Button
                android:id="@+id/help"
                style="android:buttonBarButtonStyle"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/help" />

            <Button
                android:id="@+id/close"
                style="android:buttonBarButtonStyle"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/closemultigame" />

            <Button
                android:id="@+id/ok"
                style="android:buttonBarButtonStyle"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/restartmultigame" />

        </LinearLayout>

Now the layout looks like this: screenshot from designer

The button layout_height is defined as wrap_content, but the rightmost button doesn't wrap its content. It looks different on different devices, on some it looks good. What I would actually like to achieve is three buttons, same width and same height, each with its text centered both horizontally and vertically. What approach would you propose?

ADDED: the whole layout as requested:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/activity_horizontal_margin" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:drawableLeft="@drawable/matchandfit"
        android:drawablePadding="@dimen/activity_horizontal_margin"
        android:gravity="center_vertical"
        android:padding="@dimen/activity_horizontal_margin"
        android:text="@string/multigameover"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/status"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="@string/multigameresult"
        android:textAppearance="?android:attr/textAppearanceMedium" />

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

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/masterresultinfo"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/activity_vertical_margin"
            android:baselineAligned="false" >

            <Button
                android:id="@+id/help1"
                style="android:buttonBarButtonStyle"
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="@string/help" />

            <Button
                android:id="@+id/close1"
                style="android:buttonBarButtonStyle"
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="@string/closemultigame" />

            <Button
                android:id="@+id/ok1"
                style="android:buttonBarButtonStyle"
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="@string/restartmultigame" />

        </LinearLayout>



    </LinearLayout>
</LinearLayout>

</ScrollView>

As Wasim Ahmed pointed out, it is due to the ScrollView. Now I use the ScrollView to make sure that the buttons stay accessible even in landscape on small devices. (The layout is for a Dialog). Is there an other way to always keep the buttons accessible/visible?

WORKAROUND: The solution or better workaround I found was adding a TextView to the end of the enclosing LinearLayout. This TextView is vertically clipped at about half of its height, But it contains no text, so nobody will notice. Adding some padding to the bottom of the enclosing LinearLayout didn't help

like image 863
Gerhard Avatar asked Jun 13 '14 07:06

Gerhard


People also ask

How do I make text not wrap the button?

To prevent the text from wrapping, you can use the CSS white-space property with the “nowrap” or “pre” value.

How do I wrap text in a button in CSS?

CSS word-wrap property is used to break the long words and wrap onto the next line. This property is used to prevent overflow when an unbreakable string is too long to fit in the containing box.

How do you wrap content in Android?

The Android wrap_content, as the name suggests, sets a view's size to wrap_content which will only expand enough to contain the values. In simple words, the view wrapped with wrap_content will just be big enough to enclose its contents.

Where is the Wrap text button?

On the Home tab, in the Alignment group, click Wrap Text.


1 Answers

Workaround to this is to set minHeight of Button like this

android:minHeight="1dp"

It should work

like image 123
Hassan Jawed Avatar answered Oct 19 '22 23:10

Hassan Jawed