Why don't you try to get rid of all cases of if else and do something in common? Since you need to put different pixel values โโdepending on the screen size, you can use dp instead.
You can get px from dp depending on the screen density of the device and use it in LayoutParams .
public static float convertDpToPixel(float dp, Context context){ Resources resources = context.getResources(); DisplayMetrics metrics = resources.getDisplayMetrics(); float px = dp * ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
So you will create param as follows:
zoomBarParams = new LinearLayout.LayoutParams(convertDpToPixel(DP_VALUE, context), LayoutParams.FILL_PARENT);
where DP_VALUE will remain constant on all devices.
I hope this can solve your problem.
source share