8

I have an activity with a Save and Cancel button at the bottom.

In AlertDialog, the buttons are displayed inside a styled container view of some sort.

How could I give the buttons in my Activity that same appearance? Specifically, how could I apply the style of the button container view in the AlertDialog to say a LinearLayout in my Activity containing the buttons?

Thanks

Jodes
  • 14,118
  • 26
  • 97
  • 156

2 Answers2

32

There are solutions given elsewhere that work. In short, you can simply use style attributes in your xml to achieve this. For instance, style="?android:attr/buttonBarStyle" and style="?android:attr/buttonBarButtonStyle" will do the job (for API 11+). Here is an example of two buttons horizontally put together.

<LinearLayout
    style="?android:attr/buttonBarStyle"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:measureWithLargestChild="true"
    android:orientation="horizontal"
    android:paddingTop="0dip" >

    <Button
        style="?android:attr/buttonBarButtonStyle"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text= "Ok" />

    <Button
        style="?android:attr/buttonBarButtonStyle"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Cancel" />
</LinearLayout>

The only thing that remains, is that there is a horizontal line right above the buttons in an alertDialog, which the above code will not create. If you want to have that horizontal line, it should be added manually in the xml, above the LinearLayout. This will give you the horizontal line:

<View
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:layout_marginBottom="0dp"
    android:background="?android:attr/dividerVertical" /> 
Community
  • 1
  • 1
hadi
  • 3,046
  • 1
  • 19
  • 11
1

I do some thing like this:

LinearLayout dialogLayout = (LinearLayout) ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.dialog_addeditrecord, null);

I then use the dialogLayout to call findViewById() to pull in the buttons and other views and setup OnClickListeners and such...

then to show the dialog:

builder = new AlertDialog.Builder(this);

builder.setView(dialogLayout);

builder.create().show();
Maximus
  • 8,351
  • 3
  • 29
  • 37
  • Thanks, I'll mull over that and see if I can implement it. To be honest though, I was hoping I could just add an attribute to a LinearLayout I have defined in the Activity's xml layout file, like a simple reference to a background image or something! – Jodes Apr 20 '11 at 19:29
  • I have never figured out if there is any way to modify the background image of the "Positive," "Negative," and "Neutral" buttons... this was the only way I could work around it. – Maximus Apr 20 '11 at 19:45
  • Thanks for replying again, I don't understand how your code works, where would I put it? In the onCreate of the activity that has the buttons I want to style? Very confused! – Jodes Apr 20 '11 at 20:50
  • Well, when to show the dialog would be up to you. Something similar to this could be implemented with the onCreateDialog() and onPrepareDialog() Activity methods... or you could (instead of just showing) use builder.create() to return an AlertDialog that you call show on later. – Maximus Apr 20 '11 at 20:53
  • I'm not sure we're on the same track, I didn't want to open a dialog from my activity, but only to style the activity's buttons as if they were in a dialog? – Jodes Apr 20 '11 at 20:59
  • Oh my... I'm sorry, yes I completely misunderstood. Are you looking to theme the entire activity as a dialog? That can be done in the Manifest as a theme attribute to the Activity. android:theme="@android:style/Theme.Dialog" – Maximus Apr 20 '11 at 21:54
  • Not quite what I wanted, thanks tho. I found a site which went into detail how to implement what I think you were proposing: [link](http://androiddevblog.wordpress.com/2011/02/09/sharing-complex-dialog-interactions-across-multiple-activities/) – Jodes Apr 20 '11 at 22:38