0

i am making an app with alert dialogs and i have a problem, my alertdialog shown on the right if I put a negative the two are to the right how can i center the positiveButton?

    public void showBandDialog(){       
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater     = LayoutInflater.from(getActivity());
    final View view2            = inflater.inflate(R.layout.bluetooth_dialog, null);        
    if(bluetoothIsActive){
        ImageView bluetootIndicator = (ImageView) view2.findViewById(R.id.bluetooth_indicator);
        bluetootIndicator.setImageResource(R.drawable.checked);
    }
    if(bluetoothIsSaved){
        ImageView bluetootSavedIndicator = (ImageView) view2.findViewById(R.id.bluetooth_saved_indicator);
        bluetootSavedIndicator.setImageResource(R.drawable.checked);
    }       
    builder.setTitle(Html.fromHtml("<font color='#55BFE7'>Bluetooth</font>"));
    builder.setPositiveButton("Done",
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,
                int which) {}
    });                 
    builder.setView(view2);
    final AlertDialog alertDialog = builder.create();
    alertDialog.setOnShowListener(new OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            Button okButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
            okButton.setOnClickListener(new View.OnClickListener() {                    
                @Override
                public void onClick(View v) {
                    alertDialog.dismiss();                                                                                  
                }
            });             
        }
    });
    alertDialog.show();         
}
Guillermo Merino
  • 3,197
  • 2
  • 17
  • 34
GusDev
  • 255
  • 6
  • 20
  • You can use a custom layout file with Button placed according to you and simply call builder.setView(R.layout.dialog_view); and setup OnClickListener for these Buttons – Sanjeev Jul 21 '15 at 16:36
  • 1
    I think you should not change that alignment from the standard (which is both positive and negative buttons right aligned. – Booger Jul 21 '15 at 16:37
  • You should probably read Design Guidelines for Dialog https://www.google.com/design/spec/components/dialogs.html# – XH6 user Jul 21 '15 at 16:37

3 Answers3

1

You can always use the Neutral Button to achieve the same effect of the positive button without having to create a specific layout file for your dialog.

AlertDialog.Builder - Neutral Button

Also: Android: Difference between positive, negative, and neutral button

Community
  • 1
  • 1
VulfCompressor
  • 1,390
  • 1
  • 11
  • 26
  • It's a good alternative to avoid creating a custom layout file. – Machado Jul 21 '15 at 16:41
  • shift to neutral and now goes to the left, negative and positive are to the right, there is some align that can be used? – GusDev Jul 21 '15 at 16:47
  • Intriguing. Which version of Android are you using for testing purposes? @portal47 – VulfCompressor Jul 21 '15 at 16:51
  • I am using 5.1.1 but the client wants the button in the center – GusDev Jul 21 '15 at 16:54
  • As recommended by the other answer, you'll have to create a custom layout. This happens because Material design changed the orientation of the buttons. The new arrangement since 5.0 is NEUTRAL (floating on left) and NEGATIVE / POSITIVE (floating on the right side). Although it's possible to achieve what you want with custom layouts, it is discouraged by google since the release of the Material Design theme. @portal47 – VulfCompressor Jul 21 '15 at 17:03
1

you could use a custom layout, forexample create an extra activity tha will be called when you want to show some notification. to receive data from this notification activity you can start it using startActivityForResult() and when you receive this make some action. to your custom notification activity you could use in manifest:

    <activity
        android:name=".NotifiactionActivity"
        android:theme="@android:style/Theme.Holo.Light.Dialog" >
    </activity>

there are other themes that you could use but it seem like an alert.

Rene Limon
  • 614
  • 4
  • 16
  • 34
0

Not possible using an AlertDialog (or use a neutral button as suggested), you will have to create your own custom dialog.

This example on how to do so.

Nickolaus
  • 4,785
  • 4
  • 38
  • 60