Reputation: 24500
I have standard AlertDialog popping up with results of my game, i want to make it bigger. This is my dialog creation and setting code:
@Override
protected Dialog onCreateDialog(int id) {
adb = new AlertDialog.Builder(this);
win_lose_view = (RelativeLayout) getLayoutInflater().inflate(R.layout.win_lose_screen, null);
adb.setView(win_lose_view);
upper_text = (TextView) win_lose_view.findViewById(R.id.upper_text);
upper_text.setRotation(180);
lower_text = (TextView) win_lose_view.findViewById(R.id.lower_text);
upper_decide = (Button) win_lose_view.findViewById(R.id.upper_decide);
upper_decide.setRotation(180);
lower_decide = (Button) win_lose_view.findViewById(R.id.lower_decide);
Dialog d = adb.create();
d.setCanceledOnTouchOutside(false);
return d ;
}
@Override
protected void onPrepareDialog(int id, Dialog dialog){
super.onPrepareDialog(id, dialog);
upper_decide = (Button) win_lose_view.findViewById(R.id.upper_decide);
upper_decide.setRotation(180);
lower_decide = (Button) win_lose_view.findViewById(R.id.lower_decide);
};
if(first_player_points > second_player_points){
upper_text.setText("you win! score: " + first_player_points);
lower_text.setText("you lose! score: "+ second_player_points);
}
}
here is how it looks on the device:
I want to make it wider - how do I do that?
Upvotes: 0
Views: 2771
Reputation: 66
Use getWindow().setLayout(width,height) after the show dialog.
alertDialogBuilder
.setMessage("Click yes to exit!")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
MainActivity.this.finish();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
}).show().getWindow().setLayout(600,500);
Upvotes: 2
Reputation: 1247
Just use layoutParams , and change the attributes.
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("Test Dialog")
.setMessage("This should expand to the full width")
.show();
//Grab the window of the dialog, and change the width
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
Window window = dialog.getWindow();
lp.copyFrom(window.getAttributes());
//This makes the dialog take up the full width
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(lp);
Upvotes: 1
Reputation: 55
You can create you costume layout and set on the onCreateDialog Method.
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final View view = View.inflate(getActivity(), R.layout.thalicancle_dialog, null);
final Dialog dialog = new Dialog(getActivity(), 0);
dialog.setContentView(view);
}
Custome Layout XML.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/popupbg"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingTop="@dimen/activity_vertical_margin">
Upvotes: 1