-1

I am tired of this, I am a newbie in Android, and I made an app , but I face many problems with compatiblity, I don't know how to adjust every screen to my app, and how to make it work for api min 15.Right now it is 19 because I have many problems with lower API.

Problem android Screen

So I decided to make files like

res\layout res\layout-sw600dp res\layout-sw720dp

But, still it does not work in small screens, Can someone help me on this? How can I adjust to every screen or at least with. I will so happy if someone can help me.

Anoop Kanyan
  • 618
  • 7
  • 19
M121
  • 25
  • 1
  • 6

1 Answers1

0

I have 3 ways to do this!

  1. You can create multiple layout

    res\layout res\

    See link on Google Developer
    See link on Stackoverflow

  2. You can create multiple dimen

    This is example on Stackoverflow

  3. using code

    • Create class SetViewSizeByPixel.class

    This is code

public class SetViewSizeByPixel {

private LayoutParams params = null;
private DisplayMetrics metrics = null;
private Context mContext;
public static final int match_parent = -1;

public SetViewSizeByPixel(Context context) {
    metrics = getMetrics(context);
    this.mContext = context;
}

/**
 * 
 */

public int RH(int x) {
    return metrics.heightPixels * x / 1920;
}

public int RW(int x) {
    return metrics.widthPixels * x / 1080;
}

public int RH720p(int x) {
    return metrics.heightPixels * x / 1280;
}

public int RW720p(int x) {
    return metrics.widthPixels * x / 720;
}

public int RH(float x) {
    return (int) (metrics.heightPixels * x / 1920);
}

public int RW(float x) {
    return (int) (metrics.widthPixels * x / 1080);
}

/**
 * Metrics
 */

public DisplayMetrics getMetrics(Activity activity) {
    metrics = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
    return metrics;
}

public DisplayMetrics getMetrics(Context context) {
    return getMetrics((Activity) context);
}

public DisplayMetrics getMetrics() {
    return metrics;
}

public int heightPixels() {
    return metrics.heightPixels;
}

public int widthPixels() {
    return metrics.widthPixels;
}

public float density() {
    return metrics.density;
}

public int densityDpi() {
    return metrics.densityDpi;
}

/**
 * Size
 */

public boolean height(View view, int height) {
    return size(view, -1, height);
}

public boolean width(View view, int width) {
    return size(view, width, -1);
}

public boolean square(View view, int x) {
    return size(view, x, x);
}

public boolean size(View view, int width, int height) {
    try {
        params = view.getLayoutParams();
        if (width != -1)
            params.width = width;
        else
            params.width = LayoutParams.MATCH_PARENT;

        if (height != -1)
            params.height = height;
        else
            params.height = LayoutParams.MATCH_PARENT;
        view.setLayoutParams(params);
        return true;
    } catch (Exception e) {
        Log.e("SetSizeByPercent", e.toString());
        return false;
    }
}

/**
 * Size with RW, RH.
 */

public boolean heightR(View view, int height) {
    return size(view, -1, this.RH(height));
}

public boolean widthR(View view, int width) {
    return size(view, this.RW(width), -1);
}

public boolean squareR(View view, int x) {
    return size(view, this.RW(x), this.RH(x));
}

public boolean sizeR(View view, int width, int height) {
    return size(view, RW(width), RH(height));
}

/**
 * Margin R
 */
public void marginTopR(View v, int top) {
    marginTop(v, RH(top));
}

public void marginR(View v, int lef, int top, int rig, int bot) {
    margin(v, RH(lef), RH(top), RH(rig), RH(bot));
}

public void marginR(View v, int x) {
    margin(v, RW(x), RH(x), RW(x), RH(x));
}

/**
 * Margin
 */
public void marginLeft(View v, int lef) {
    margin(v, lef, -1, -1, -1);
}

public void marginTop(View v, int top) {
    margin(v, -1, top, -1, -1);
}

public void marginRight(View v, int rig) {
    margin(v, -1, -1, rig, -1);
}

public void marginBottom(View v, int bot) {
    margin(v, -1, -1, -1, bot);
}

public void marginLeft(View v, int lef, boolean keepOldMargin) {
    int k = keepOldMargin ? -1 : 0;
    margin(v, lef, k, k, k);
}

public void marginTop(View v, int top, boolean keepOldMargin) {
    int k = keepOldMargin ? -1 : 0;
    margin(v, k, top, k, k);
}

public void marginRight(View v, int rig, boolean keepOldMargin) {
    int k = keepOldMargin ? -1 : 0;
    margin(v, k, k, rig, k);
}

public void marginBottom(View v, int bot, boolean keepOldMargin) {
    int k = keepOldMargin ? -1 : 0;
    margin(v, k, k, k, bot);
}

public void margin(View v, int margin) {
    margin(v, margin, margin, margin, margin);
}

public void margin(View v, int lef, int top, int rig, int bot) {
    if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
        ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
        lef = (lef == -1) ? p.leftMargin : lef;
        top = (top == -1) ? p.topMargin : top;
        rig = (rig == -1) ? p.rightMargin : rig;
        bot = (bot == -1) ? p.leftMargin : bot;

        p.setMargins(lef, top, rig, bot);
        v.requestLayout();
    }
}

public LayoutParams getParams() {
    return params;
}

public void setParams(LayoutParams params) {
    this.params = params;
}

/**
 * Text Size
 */
public void textSize(TextView tv, int size) {
    tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
}

public void textSize(View view, int size) {
    try {
        ((TextView) view).setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
    } catch (Exception e) {
        Log.e("", e.toString());
    }
}

public void textSizeR(TextView view, int size) {
    view.setTextSize(TypedValue.COMPLEX_UNIT_PX, RH(size));
}

public void textSizeR(View view, int size) {
    try {
        ((TextView) view).setTextSize(TypedValue.COMPLEX_UNIT_PX, RH(size));
    } catch (Exception e) {
        Log.e("", e.toString());
    }
}

/**
 * Animation & TypeFace
 */
public Animation getAnimation(int id) {
    return AnimationUtils.loadAnimation(mContext, id);
}

public Typeface getTypeface(String typefacePath) {
    return Typeface.createFromAsset(mContext.getAssets(), typefacePath);
}

// public void setSize(View v,int w,int h){
// float hrate = MyActivity.Height/1920f;
// size(v, (int) (w*hrate),(int)(h*hrate));
// }
  }

This is example

in onCreate()

// TODO Auto-generated method stub
    SetViewSizeByPixel setsize = new SetViewSizeByPixel(HighScoreActivity.this);
    DisplayMetrics metris = setsize.getMetrics(HighScoreActivity.this);

    // Main_Menu
    setsize.size(layout_header, metris.widthPixels * 1024 / 1024, metris.heightPixels * 60 / 600);
    layout_header.setPadding(metris.widthPixels * 10 / 1024, metris.heightPixels * 10 / 600,
            metris.widthPixels * 10 / 1024, metris.heightPixels * 10 / 600);
    setsize.textSize(txt_top, metris.widthPixels * 15 / 600);
    setsize.textSize(txt_time, metris.widthPixels * 15 / 600);
    setsize.textSize(txt_main_menu, metris.widthPixels * 15 / 600);
    setsize.size(txt_top, metris.widthPixels * 200 / 1024, metris.heightPixels * 40 / 600);
    setsize.size(txt_time, metris.widthPixels * 600 / 1024, metris.heightPixels * 40 / 600);
    setsize.size(txt_main_menu, metris.widthPixels * 204 / 1024, metris.heightPixels * 40 / 600);

Note:

  • You just take screen device test. And use photoshop to show ficture.

    metris.widthPixels * 600 / 1024

    600 is 600px, desired width is measured with the image in Photoshop
    1024 is 1024px, the width of the device screen test

Community
  • 1
  • 1
Nguyễn Trung Hiếu
  • 2,004
  • 1
  • 10
  • 22