Ray Ray - 1 month ago 5
Android Question

Button layout_height=wrap_content inside linear layout has height much bigger then wrap_content

I have LinearLayout with TextView and Button. I set layout_height attribute to wrap_content but layout_height has much bigger size. If I remove this button then all is alright. I try to decrease button text size but frame of the button still the same. I don't want hardcode button size. What can be the reason this strange result?

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1" >

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="@dimen/left_margin"
android:layout_marginTop="@dimen/top_margin"
android:layout_weight=".3"
android:text="@string/contact" />

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:layout_marginLeft="@dimen/left_margin"
android:layout_marginTop="@dimen/top_margin"
android:orientation="horizontal"
android:weightSum="1" >

<TextView
android:id="@+id/contact"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/contact" android:textSize="@dimen/button_font_size"/>

</LinearLayout>

Answer

Button has background and other layout properties and does not seem possible to reduce its height. An option would be to use a TextView with custom style and handle click event so that it behaves like a Button.

Example:

<TextView
    android:layout_height="wrap_content"
    android:background="#ddd"
    android:padding="6dp"
    android:layout_width="wrap_content"
    android:text="Contact" />

It requires some work and time spent to style the TextView properly, but if you have no other choice, it may do the trick for you.