Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have implemented my own way of <strong>Handling Multiple Screen Resolutions</strong>.</p> <p>You could certainly avoid this problem by setting <code>LayoutParams</code> at run time in terms of <strong>Percentage</strong></p> <p>The <strong>Problem</strong> occurs only with <code>Views/Layouts</code> that have some constant <strong>width</strong> or <strong>height</strong> lets say <code>280dp</code>. <strong>Solution</strong> is quite simple if we programmatically set Layout Params of our <code>Views/Layouts</code> in terms of <strong>Percentage</strong> and only use constant <strong>width</strong> or <strong>height</strong> where necessary, elsewhere try to use <code>match_parent</code> to fill up empty space or use <code>weights</code> and define every <code>View</code> relative to other <code>Views</code> this will help your layout to look good in almost every screen resolutions</p> <p>Here is a sample xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" &gt; &lt;LinearLayout android:id="@+id/mLayout" android:layout_width="280px" android:layout_height="300px" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p><strong>Notice:</strong> I have used <strong>px</strong> for fixed sized layout's <strong>width/height</strong> because in <code>LayoutParams layoutParams = new LayoutParams(int width, int height);</code> the <code>width</code> and <code>height</code> take value as <strong>pixels</strong></p> <p>Here is an example code of setting <strong>width</strong> and <strong>height</strong> in terms of percentage</p> <pre><code>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); } }); </code></pre> <p>Now might be some people are wondering what is happening here</p> <p><code>float widthInPercentage = ( (float) 280 / 320 ) * 100</code></p> <p>Let me explain <strong>280</strong> is the width of my <code>LinearLayout</code> and <strong>320</strong> is the width of my device screen(on which i'm developing), i know currently i'm testing on a device having resolution <strong>320 x 480</strong>, what i'm doing is calculating how much <strong>area</strong> my layout is covering in terms of <strong>percentage</strong> and then </p> <p><code>int mLayoutWidth = (int) ( (widthInPercentage * deviceWidth) / 100 )</code> </p> <p>here i'm calculating the new width for my layout according to the screen resolution and by this way your <code>Views/Layouts</code> will look exactly the same on every <strong>screen resolution</strong>.</p> <p><strong>Conclusion:</strong> If you need to set some constant <strong>width/height</strong> for your <code>Views/Layouts</code> always set value in <strong>px</strong> in layout file (i.e xml) and then programmatically set <code>LayoutParams</code>.</p> <p>A Suggestion for <strong>Google Android Engineers</strong>, i guess you guys should seriously think of changing the <code>dp/dip</code> units to <code>percentage</code></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload