Pomanh
Pomanh

Reputation: 640

How to properly assign a layout to a certain mobile size

normally, android gives the option to have 4 different sizes namely; small, normal, large, xlarge. however, the normal size can contain devices from 3.7" to 4.95"

My question is, how to target certain device sizes, in my case from 3.7" to 4" only ?

I tried to make a combination between normal and hdpi from the qualifier section, but then realized that there are devices that are in that size range but xhdpi

another way to ask my question is: I have two layouts, one should be shown for devices having sizes of 3.7" to 4" and the other will be shown in devices having size more than 4"

Upvotes: 1

Views: 58

Answers (1)

koksalb
koksalb

Reputation: 431

You can try this then:

After having x and y of the screen you will pretty much know what size the screen is, then you can inflate different XML files on your onCreate method to change the outlooking of the app.

Just one problem would be that when you define some buttons etc on one of them they might not exist on the other one. Try to create one giant fragment space to inflate your activities as fragments in it, so you dont have to write your whole code inside OnCreate if's of one java file.

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int width = size.x;
        int height = size.y;

        if(//some code here to calculate what size the screen is lets say +4")
        {
            setContentView(R.layout.layout1);
        }
        else if(//another block for lets say 3.7")
        {
            setContentView(R.layout.layout2);                    
        }
    }

Good luck hope it works

Upvotes: 1

Related Questions