0

I am creating an activity that is styled like a dialog. The code works great, but the dialog looks different on my Galaxy S3 than my Nexus 5. How do make sure that all resolutions will show the button?

Code:

    requestWindowFeature(Window.FEATURE_LEFT_ICON);
    setContentView(R.layout.widget_dialog);
    setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,R.drawable.icon);
    //Setup Dialog Activity Parameters
    this.setFinishOnTouchOutside(false);
    setContentView(R.layout.widget_dialog);
    LayoutParams params = getWindow().getAttributes();
    params.x = -30;
    params.height = 350;
    params.width = 550;
    params.y = -30;
    getWindow().setAttributes(params);

Screenshot on my Samsung Galaxy S3 enter image description here

Screenshot on my Nexus 5 enter image description here

Notice that the button is missing.

Peter Sun
  • 1,675
  • 4
  • 27
  • 50

2 Answers2

0

This is certainly not the way to design a layout in Android. You are specifying the dimensions of the dialog in pixels, but bear in mind devices have different screen densities.

To better understand how to cope with different screens, I suggest reading the Android training on Supporting Different Screen Sizes.

Specifically for creating a dialog, I would suggest that you read the Android guide on doing so. The important thing to take away is to use an AlertDialog, and define the layout using setView().

You can find an example of doing this in a related question:

LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(
        R.layout.dialog_layout, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(view);
builder.show();

Bear in mind that adding buttons is as easy as builder.setPositiveButton(...), builder.setNeutralButton(...) and builder.setNegativeButton(...).

Community
  • 1
  • 1
Paul Lammertsma
  • 37,593
  • 16
  • 136
  • 187
0

Supporting different resolutions and screen sizes is very easy in android. You have probably seen the folders for different densities in your project structure (in res folder there are

    drawable-xhdpi/
    drawable-hdpi/
    drawable-mdpi/
    drawable-ldpi/
    ...

You can use those for different images included in your project depending on the resolution and screen size. But there is similar solution for different layouts when it comes to screen sizes and resolutions. Everything is in naming the folders for your layouts. See:

res/layout/my_layout.xml             // layout for normal screen size ("default")
res/layout-small/my_layout.xml       // layout for small screen size
res/layout-large/my_layout.xml       // layout for large screen size
res/layout-xlarge/my_layout.xml      // layout for extra large screen size
res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation

And for the record here are some numbers to look at:

xlarge screens are at least 960dp x 720dp
large screens are at least 640dp x 480dp
normal screens are at least 470dp x 320dp
small screens are at least 426dp x 320dp

You just need to place adjusted copy of your layouts in different folders created in your res folder. (you will need to create those folders with exact naming if your IDE is not creating those for you) Find out more on http://developer.android.com/guide/practices/screens_support.html

Hope this helps :)

Nikola Milutinovic
  • 731
  • 1
  • 8
  • 23