Styles, minHeight/maxHeight: I cannot get the maxHeight to override the theme minHeight

Im thinking that more "global" styles are always overridden by more "local" Styles. For example, if I redefine all Buttons to have textSize=40dip (apply that Style as a Theme for the Application) and then apply another Style to a specific Button that says textSize=10dip, then that specific Button should get 10dip textSize.

And that is how it works, usually. But not when it comes to maxHeight. Here is the scenario:

In my styles.xml I have one Style where I inherit the default Button and change textSize and minHeight, and then another Style that sets some other values (but also inherits from Button), like this:

<?xml version="1.0" encoding="utf-8"?>
<style name="Button" parent="@android:style/Widget.Button">
    <item name="android:textSize">26dip</item>
    <item name="android:minHeight">60dip</item>

<style name="ButtonHeader" parent="@android:style/Widget.Button">
    <item name="android:textSize">18dip</item>
    <item name="android:minWidth">70dip</item>
    <item name="android:maxHeight">10dip</item>

I apply the first Style as a theme for my Activity which makes all the buttons larger (minHeight=60dip). But I have a "header" (where I have some other buttons) that I do not want to have a minHeight of 60dip, and for those buttons I want to use the ButtonHeader, setting the maxHeight to 10dip.

In my header.xml it looks like this:

<Button style="@style/ButtonHeader" android:text="UPP" android:id="@+id/Header_Button_UPP" android:layout_width="wrap_content" android:layout_height="wrap_content" ></Button>
<Button style="@style/ButtonHeader" android:text="ALT" android:id="@+id/Header_Button_ALT" android:layout_width="wrap_content" android:layout_height="wrap_content" ></Button>
<Button style="@style/ButtonHeader" android:text="NAV" android:id="@+id/Header_Button_NAV" android:layout_width="wrap_content" android:layout_height="wrap_content" ></Button>
<Button style="@style/ButtonHeader" android:text="HIS" android:id="@+id/Header_Button_HIS" android:layout_width="wrap_content" android:layout_height="wrap_content" ></Button>

I am specificly styling the buttons, overriding the "global" theme. It works in some parts; the textSize for these header-buttons is correctly set to 18dip, but the maxHeight is ignored - these buttons also increase in height to 60dip.

If I, in the style for ButtonHeader, set android:minHeight="100dip" the buttons in the header will increase in size to 100dip, overriding the Theme. But, as stated above, when I have android:maxHeight instead, nothing happens.

What am I missing?

Asked by: Walter643 | Posted: 24-01-2022

Answer 1

Shouldn't your ButtonHeader style override the minHeight property? It seems like you end up with minHeight=60dp and maxHeight=10dp, and minHeight wins. If you explictly set the minHeight to something else, you shouldn't have that problem.

Answered by: Hailey268 | Posted: 25-02-2022

Answer 2

as I underestand you have 2 different style for Button (Button - HeaderButton) and in HeaderButton Styles you set minHieght and MaxHeight (max height isworking) and now MinHeight face with an issue,

I implement your code and find that your MinHeight is not suitable for 18dip textsize, so you should either decrease your tex size or increase minheight size

let me know, whats happen to you

Answered by: Michael462 | Posted: 25-02-2022

Similar questions

Still can't find your answer? Check out these communities...

Android Google Support | Android Community | Android Community (Facebook) | Android