Qazi
Qazi

Reputation: 120

what can i do to make the buttons evenly distribute along the xml layout and make the text fit in?

I have used android:layout_height="0dp" while using android:layout_weight="1" on every button .. but the size still changes if the string is too long. How can I make button size constant while making the text fit in?

(It's a quiz app and I am generating random strings on four buttons so I have used two linear layouts with horizontal orientation)

<LinearLayout 
    android:id="@+id/ll1"
    android:layout_marginTop="30dp"
    android:layout_below="@+id/textView2"
    android:weightSum="2"
    android:orientation="horizontal"
    android:layout_width="0dp"
    android:layout_height="wrap_content">

    <Button
        android:layout_weight="1"
        android:layout_marginRight="2dp"
        android:textColor="#000"
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/mybutton"
        android:text="Button" />

    <Button
        android:layout_marginLeft="2dp"
        android:layout_weight="1"
        android:textColor="#000"
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         android:background="@drawable/mybutton"
        android:text="Button" />



    </LinearLayout>

        <LinearLayout 

        android:id="@+id/ll2"
        android:layout_marginTop="20dp"
        android:layout_below="@+id/ll1"
        android:weightSum="2"
        android:orientation="horizontal"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        >

     <Button

        android:layout_marginRight="2dp"
        android:layout_weight="1"
        android:textColor="#000"
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/mybutton"
        android:text="Button" />

    <Button
        android:layout_marginLeft="2dp"
        android:textColor="#000"
        android:layout_weight="1"
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/mybutton"
        android:text="Button" />



</LinearLayout>

Upvotes: 3

Views: 140

Answers (1)

toidiu
toidiu

Reputation: 965

I think from your description you are using LinearLayout wrong. With horizontal orientation you want to set the width to 0dp. Look at the code below.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="two"/>
    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="two"/>
</LinearLayout>

android:singleLine="true" 

Add the above to the button field. Its going to ellipsis your text but it will enforce a single line...

You really have a few choices: ellipsis, smaller font size, layout alias for finer control (http://developer.android.com/guide/topics/resources/providing-resources.html)

With layout alias you can specify a different layout for different sized/dpi/orientation/width/height screens and therefore specify a font size that works and fits.

Upvotes: 2

Related Questions