How to Android Holo Theme style dialog box buttons

AndroidAndroid Theme

Android Problem Overview


I'm creating a Dialog Box on Holo Theme and want to follow the OS default way of displaying the buttons. So far I have created the dialog box but the buttons don't render in the way it does in the apps done in Holo for ICS. How can I do this? My intended look & feel is No. 3rd in this image and I am able to reach till here Notice the Signup and Login buttons

Android Solutions


Solution 1 - Android

a bit late, but maybe someone is still interested in that.

this works pretty good for me.

...
<!--
EDIT: be carefull, "?android:attr/dividerHorizontal" is only supported since API 11
      just avoid it in prior OSs.
-->
<View
    android:layout_width="fill_parent"
    android:layout_height="1dip"
    android:background="?android:attr/dividerHorizontal" />
<LinearLayout 
    style="?android:attr/buttonBarStyle"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingTop="0dip"
	android:paddingLeft="2dip"
	android:paddingRight="2dip"
	android:measureWithLargestChild="true">
    
    <Button 
        android:id="@+id/cancel"
        style="?android:attr/buttonBarButtonStyle"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@android:string/cancel"/>
    <Button 
        android:id="@+id/ok"
        style="?android:attr/buttonBarButtonStyle"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@android:string/ok"/>
</LinearLayout>
...

the activity that loads this layout needs the Holo.Dialog theme.

android:theme="@android:style/Theme.Holo.Dialog"

Solution 2 - Android

This is what works:

<LinearLayout
    android:id="@+id/buttonHolder"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:orientation="horizontal"
    >

    <Button
        android:id="@+id/cmdSignup"
        style="@android:style/Widget.Holo.Light.Button.Borderless.Small"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/Signup" />

    <Button
        android:id="@+id/cmdLogin"
        style="@android:style/Widget.Holo.Light.Button.Borderless.Small"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/Login" />
</LinearLayout>

The property style="@android:style/Widget.Holo.Light.Button.Borderless.Small" gives the flat look and feel, and the 50% weight distribution is because of combining 100$ sizing of LinearLayout by android:layout_width="match_parent" and android:layout_weight="1"`for buttons

Solution 3 - Android

You can set the theme through the Android Manifest xml or inside the Activity's onCreate with setTheme(android.R.style.Theme_Holo);

The buttons size is not related to the theme itself. The size is up to your xml definitions. In the image you sent, it seems that the buttons received the Holo theme so there's nothing wrong here...

Here's an xml layout that will stretch the buttons to fill the whole dialog width:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
		android:orientation="vertical"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		>
    <LinearLayout
    			android:orientation="horizontal"
    			android:layout_width="fill_parent"
    			android:layout_height="wrap_content"
    			android:layout_marginTop="5dip"
    			>
    			<Button
    			    android:id="@+id/okButton"
    			    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="OK"
    			/>
    			<Button
    			    android:id="@+id/cancelButton"
    			    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Cancel"
    			/>			
    	</LinearLayout>
</LinearLayout>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionkishu27View Question on Stackoverflow
Solution 1 - AndroidSimonSaysView Answer on Stackoverflow
Solution 2 - Androidkishu27View Answer on Stackoverflow
Solution 3 - AndroidLior IluzView Answer on Stackoverflow