How do I get my AlertDialog to fill parent width
I'm building an alert dialog dynamically with an edit text field with two buttons. The dialog box is only开发者_C百科 wrapping the width which is very small, I want it to stretch to fill the parent width
public void showMessageDialog(String name){
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
this.etMessage = new EditText(this);
etMessage.setText("");
alert.setMessage("Message " + name);
alert.setView(etMessage);
alert.setPositiveButton("Message", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
updateLocation();
}
});
alert.setNeutralButton("Map", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
showMap();
}
});
alert.show();
}
I don't know how to do it from code, but if you inflate a view from XML, you can add the layout_width="fill_parent"
attribute to the top level view, and it should fill the screen.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
You can then inflate the view like so:
View dialogLayout = getLayoutInflater().inflate(R.layout.dialog_view, null);
and add it to the AlertDialog.Builder:
builder.setView(dialogLayout);
精彩评论