Swapnil Desai
Swapnil Desai

Reputation: 373

Android : Decreasing size of the Button

I am using Button in one of the layouts. I want Button size to be as small as the text/label size.

I am using the following code.

<Button 
android:text="@string/cancle_button" 
android:id="@+id/button1" 
android:background="@drawable/back_button" 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="12sp"
android:textColor="#ffffff"
android:textStyle="bold" />

But I want Button height to be same as text/label height. Whenever I decrease or increase the text height, Button height remains the same. How should I solve this problem?

Upvotes: 36

Views: 35731

Answers (6)

ilyassavli
ilyassavli

Reputation: 71

You can use

android:minHeight="0dp" -> To decrease button height as much as label size

android:minWidth="0dp" -> To decrease button width as much as label size

So;

<Button 
    android:text="@string/cancle_button"
    android:minHeight="0dp"
    android:minWidth="0dp" 
    android:id="@+id/button1"
    android:background="@drawable/back_button" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textSize="12sp"
    android:textColor="#ffffff"
    android:textStyle="bold" />

Upvotes: 1

Coding With Parth
Coding With Parth

Reputation: 1

You can solve this by changing layout_weight of the button as by default it is set to "1"

Just set it to 0, and then you change the button_height by just changing the layout_height of the button. Here is the code for a normal button-

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:text="Button" />

Upvotes: 0

Vijeesh
Vijeesh

Reputation: 191

You can use a relative layout to achieve this. Check this sample code,

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="TextView" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/textView1"
        android:layout_alignTop="@id/textView1"
        android:layout_alignBottom="@id/textView1"
        android:text="Button" />

</RelativeLayout>

Upvotes: 1

Sreekanth
Sreekanth

Reputation: 2917

It's been almost a year since the question was asked. But I faced the problem now and got a solution. :) May be it will help someone.

You can achieve this by setting android:minHeight="0dp" for the Button widget.

And I guess this behavior is because there is some default value (may be a 48dp) set for Button widgets by the framework considering a minimum touch area.

Upvotes: 188

jyomin
jyomin

Reputation: 1967

You can solve this issue by setting the button height to a specific value which is same as the height of your label.

<Button 
android:text="CANCEL" 
android:id="@+id/button1" 
android:background="@drawable/back_button" 
android:layout_width="wrap_content"
android:layout_height="20sp"
android:gravity="center"
android:textSize="15sp"
android:textColor="#ffffff"
android:textStyle="bold" />

Upvotes: 1

Gerald Horton
Gerald Horton

Reputation: 114

Use can take the image view in this case or if your taking text view in this case so you can take the image with proper size so that it would not bluer through these measures you can set small size for image Best way is you can take the image with that size you want to create its good way

Upvotes: 3

Related Questions