Swapnil Desai Swapnil Desai - 3 months ago 9
Android Question

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?

Answer

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>
Comments