Multiple screen resolution [duplicate]

I have implemented my own way of Handling Multiple Screen Resolutions.

You could certainly avoid this problem by setting LayoutParams at run time in terms of Percentage

The Problem occurs only with Views/Layouts that have some constant width or height lets say 280dp. Solution is quite simple if we programmatically set Layout Params of our Views/Layouts in terms of Percentage and only use constant width or height where necessary, elsewhere try to use match_parent to fill up empty space or use weights and define every View relative to other Views this will help your layout to look good in almost every screen resolutions

Here is a sample xml

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:id="@+id/mLayout"
            android:layout_width="280px"
            android:layout_height="300px" />

    </RelativeLayout>

Notice: I have used px for fixed sized layout’s width/height because in LayoutParams layoutParams = new LayoutParams(int width, int height); the width and height take value as pixels

Here is an example code of setting width and height in terms of percentage

final ViewTreeObserver mLayoutObserver = mLayout.getViewTreeObserver();

    mLayoutObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() 
    {

        @Override
        public void onGlobalLayout() 
        {
            DisplayMetrics metrics = getResources().getDisplayMetrics();

            int deviceWidth = metrics.widthPixels;

            int deviceHeight = metrics.heightPixels;

            float widthInPercentage =  ( (float) 280 / 320 )  * 100;

            float heightInPercentage =  ( (float) 300 / 480 ) * 100;

            int mLayoutWidth = (int) ( (widthInPercentage * deviceWidth) / 100 );

            int mLayoutHeight = (int) ( (heightInPercentage * deviceHeight) / 100 );

            LayoutParams layoutParams = new LayoutParams(mLayoutWidth, mLayoutHeight);

            mLayout.setLayoutParams(layoutParams);
        }
    });

Now might be some people are wondering what is happening here

float widthInPercentage = ( (float) 280 / 320 ) * 100

Let me explain 280 is the width of my LinearLayout and 320 is the width of my device screen(on which i’m developing), i know currently i’m testing on a device having resolution 320 x 480, what i’m doing is calculating how much area my layout is covering in terms of percentage and then

int mLayoutWidth = (int) ( (widthInPercentage * deviceWidth) / 100 )

here i’m calculating the new width for my layout according to the screen resolution and by this way your Views/Layouts will look exactly the same on every screen resolution.

Conclusion: If you need to set some constant width/height for your Views/Layouts always set value in px in layout file (i.e xml) and then programmatically set LayoutParams.

A Suggestion for Google Android Engineers, i guess you guys should seriously think of changing the dp/dip units to percentage

Leave a Comment

tech