Can we have vertical buttons in the Android alert dialog?

By default, we get two or three buttons that are horizontally aligned in the warning dialog box. Is it possible to align them vertically in the notification dialog box?

+6
source share
2 answers

Of course, you can use Dialog.setContentView () to set the contents of the dialog box as an arbitrary layout.

Dialog dialog = new Dialog(this); dialog.setContentView(R.layout.yourLayoutId); dialog.show(); 

Make yourself a layout file with a vertical LinearLayout that has the buttons you need, and call setContentView in your dialog box, passing the name of your layout file.

If you froze in AlertDialog, you can do something similar with builder.setView ()

  LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View layout = inflater.inflate(R.layout.yourLayoutId, (ViewGroup) findViewById(R.id.yourLayoutRoot)); AlertDialog.Builder builder = new AlertDialog.Builder(this) .setView(layout); AlertDialog alertDialog = builder.create(); alertDialog.show(); 
+8
source

There was a call to setItems (), which does this from API level 1. There is no reason to create a custom dialog if you do not want to change the appearance of the elements.

 CharSequence[] items = {"Foo", "Bar", "FooBar"}; new AlertDialog.Builder(activity) .setTitle("Choose a widget") .setItems(items, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch(which) { case FOO: // foo case break; .... } } } .create().show(); 
+6
source

Source: https://habr.com/ru/post/910378/


All Articles