1

Hello I want to ask about the most efficient way to adjust layout in all devices mobile and tablets sometimes I can't use wrap_content and layout_weight I set size in some percentage to the device size in java like this:

   ImageView img;
   Display display = getWindowManager().getDefaultDisplay();
   width = display.getWidth();
   height = display.getHeight();
   img.getLayoutParams().width = width* 7 / 10;

and when rotating screen I use this method to change percentage

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE&& getResources().getBoolean(R.bool.isTablet)) {
width=(int) (width * 0.7);
}

I am asking If this procedure is more efficient than using multi XML files for each screen size / orientation

Charuක
  • 12,953
  • 5
  • 50
  • 88
Radwa
  • 325
  • 4
  • 21
  • 1
    Android tools convert XML layouts into code, so there's no inherent problem with using XML layouts versus dynamic – Charuක Dec 25 '16 at 10:20
  • Possible duplicate of [Best practices: Layouts on Android (Programmatic vs XML)](http://stackoverflow.com/questions/9827819/best-practices-layouts-on-android-programmatic-vs-xml) – Charuක Dec 25 '16 at 10:20
  • @Charuka it's not the same as my question I am not saying I am using java to construct views instead of XML nope I am using XML and my issues was controlling width & height even dynamically or in XML – Radwa Dec 25 '16 at 10:56

1 Answers1

2

Actually it depends on the scenario. Sometimes maintaining xml is efficient and easy sometimes dynamic calculation is necessary. You can go through the link https://developer.android.com/guide/practices/screens_support.html . It will give you some ideas. In your above code for width/height calculation sometimes you may not get proper result for some devices. Below is the code that will support all version of android device Resolution(Width, Height) accurately at runtime.

private void calculateDeviceResolution(Activity context) {
    Display display = context.getWindowManager().getDefaultDisplay();

    if (Build.VERSION.SDK_INT >= 17) {
        //new pleasant way to get real metrics
        DisplayMetrics realMetrics = new DisplayMetrics();
        display.getRealMetrics(realMetrics);
        realWidth = realMetrics.widthPixels;
        realHeight = realMetrics.heightPixels;

    } else if (Build.VERSION.SDK_INT >= 14) {
        //reflection for this weird in-between time
        try {
            Method mGetRawH = Display.class.getMethod("getRawHeight");
            Method mGetRawW = Display.class.getMethod("getRawWidth");
            realWidth = (Integer) mGetRawW.invoke(display);
            realHeight = (Integer) mGetRawH.invoke(display);
        } catch (Exception e) {
            //this may not be 100% accurate, but it's all we've got
            realWidth = display.getWidth();
            realHeight = display.getHeight();
            Constants.errorLog("Display Info", "Couldn't use reflection to get the real display metrics.");
        }

    } else {
        //This should be close, as lower API devices should not have window navigation bars
        realWidth = display.getWidth();
        realHeight = display.getHeight();
    }
}