Reputation: 681
I want to display a dialog when I click on a button. I've done a custom dialog like that :
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dialog);
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.setCanceledOnTouchOutside(false);
Window window = dialog.getWindow();
window.setLayout(300, 450);
dialog.show();
As you can see, I define the size of the layout with window.setLayout in java, because it doesn't work on xml for the dialog (and I can't put the dialog theme on the activity). I would like to set a layout size in java depending of the screen size as for xml with layout-large and xlarge (as I explained, I can't use the xml to define the layout depending on the screen size).
Thanks,
Upvotes: 1
Views: 488
Reputation: 7011
You can configure it on the dimens.xml
file on the values
folder. You can specify a values folder for different screen sizes like this:
values-w820dp
and the dimens.xml file example:
<resources>
<dimen name="dialog_height">64dp</dimen>
<dimen name="dialog_width">64dp</dimen>
</resources>
Here you have more info about it.
Then on your code you can access it like this
int dialogHeight = (int) getResources().getDimension(R.dimen.dialog_height)
int dialogWidth = (int) getResources().getDimension(R.dimen.dialog_width)
getResources().getDimension()
will resolve the right dimension from the right folder based on the device automatically.
Here you have some common configurations:
- 320dp: a typical phone screen (240x320 ldpi, 320x480 mdpi, 480x800 hdpi, etc).
- 480dp: a tweener tablet like the Streak (480x800 mdpi).
- 600dp: a 7” tablet (600x1024 mdpi).
- 720dp: a 10” tablet (720x1280 mdpi, 800x1280 mdpi, etc).
Hope it helps.
Upvotes: 2
Reputation: 506
Here is the solution , i have been use it with costume layout
Here is the Code
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.dialog_customer);
try {
LinearLayout lytdialog = (LinearLayout) dialog.findViewById(R.id.dialog);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int height = size.y;
int width = size.x;
android.view.ViewGroup.LayoutParams layoutParams = lytdialog.getLayoutParams();
layoutParams.width = width;
layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
lytdialog.setLayoutParams(layoutParams);
} catch (Exception e) {
e.printStackTrace();
}
dialog.show();
}
Happy Coding
Upvotes: 0
Reputation: 2428
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
Upvotes: 0