I have layout which has a button. In the graphical layout editor, the button fills width and has wrap_content height which gives the text some padding (as a usual button does). However, when launching an activity with this layout, the button sometimes decides to closely wrap the text, making a very small thin button.
When i say sometimes, I do mean sometimes, performing exactly the same action in my app may or may not generate this result.
I think the issue is related to the activity this is launched from, which is one with a camera preview. Like others have done, this app uses a landscape layout with vertical components on it to make it looks right while holding the phone in portrait (its a mess, i know, but we want to support api7).
So, my problematic activity/layout is launched from this cameraActivity, and in the process, the orientation changes. I believe this to be the source of the issue.
There is the button layout:
<Button android:id="@+id/ContinueBtn"
android:text="@string/btn_continue"
style="@style/main_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:typeface="normal"
android:onClick="onButtonAction"
android:background="@drawable/button_animated" >
</Button>
And for reference, the activity sets itself to portrait BEFORE setting the layout which includes this button.
So to summarise, I am getting a thin, wide button, some of the time. The other times I am getting a normal, healthy, non starved, button. I believe this is due to screen orientation changing – as the orientation changes it seems the layout engine sometimes manages to miss the default padding a button has. My question is firstly, what is causing this? Am I right? Secondly: how can I prevent this?
I should also add, that I think this has something to do with my custom background for the image – I haven’t been able to make it happen with a default button.
Also, here are some images of the problem:


EDIT2: so, some solutions have been suggested in the comments below. but can anyone explain why this effect is so inconsistent? is there some kind of race condition?
Thanks to Espiandev and Wasaig for suggesting some solutions to this problem. My issue turned out to be with ‘constantSzie=”true”‘ in my selector for the button (which admittedly, I didn’t provide in the original question.
Turning this off will fix the issue (as will either of the two other solutions mentioned in comments).
As it stands however, constantSize is NOT working (as far as my understanding goes). I believe there is a bug somewhere in the code where it obtains the constant size (I’ve looked through the source code here, and cannot spot anything immediately wrong: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.3_r1/android/graphics/drawable/DrawableContainer.java#DrawableContainer.DrawableContainerState.computeConstantSize%28%29 ). I suspect the intrinsicSize is being returned as ‘-1’.
I have submitted an android bug report here which includes a sample project that generated the bug:
http://code.google.com/p/android/issues/detail?id=36432