2

The bottom property of below textView scales wrong, the TextView is on a different height on every Android device: See attached pictures.

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.bars_layout);

    RelativeLayout relativeLayout = (RelativeLayout)findViewById(R.id.bar_holder);

    BarView view = new BarView(getApplicationContext());
    int width = (int) getApplicationContext().getResources().getDimension(R.dimen.bar_width_compare);
    int height = 200;
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width, height);
    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
    int left = (int) getApplicationContext().getResources().getDimension(R.dimen.bar_margin_left_right);
    int right = 0;
    int bottom = (int) getApplicationContext().getResources().getDimension(R.dimen.graph_margin_bar_compare_bottom);
    params.setMargins(left, 0, right, bottom);
    view.setBackgroundColor(getApplicationContext().getResources().getColor(R.color.bar_dark_blue));
    view.setLayoutParams(params);
    relativeLayout.addView(view);

    TextView textView = new TextView(getApplicationContext());
    textView.setText("   20  ");
    textView.setTextSize(20);
    textView.setTextColor(getApplicationContext().getResources().getColor(R.color.black_text));
    width = (int) getApplicationContext().getResources().getDimension(R.dimen.bar_width);
    height = 100;

    bottom = (int) getApplicationContext().getResources().getDimension(R.dimen.graph_margin_bar_bottom);
    int offset = getApplicationContext().getResources().getDimensionPixelOffset(R.dimen.graph_margin_bar_bottom);
    int pxSize =  getApplicationContext().getResources().getDimensionPixelSize(R.dimen.graph_margin_bar_bottom);



    RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(width, height);
    params2.setMargins(0, 0, 0, bottom);
    params2.addRule(Gravity.CENTER_HORIZONTAL);
    params2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    params2.addRule(RelativeLayout.ALIGN_LEFT, view.getId());
    textView.setLayoutParams(params2);
    relativeLayout.addView(textView);
    }
}

bars_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_marginTop="40dp" >


<RelativeLayout
    android:id="@+id/bar_holder"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_marginTop="20dp"
    android:layout_marginBottom="10dp" >


</RelativeLayout>
</LinearLayout>

dimens.xml

<resources>
    <dimen name="graph_margin_bar_bottom">40dp</dimen>
    <dimen name="bar_width_compare">25dp</dimen>
    <dimen name="bar_margin_left_right">10dp</dimen>
    <dimen name="graph_margin_bar_compare_bottom">50dp</dimen>
    <dimen name="bar_width">50dp</dimen>
</resources>

Here are 2 examples of phones, but it looks different on every phone...

Samsung Galaxy Tab 3 Lite (density1.0)

enter image description here

Samsung Galaxy S4 (density3.0)

enter image description here

Jim Clermonts
  • 1,694
  • 8
  • 39
  • 94
  • 2
    getDimension() should scale correctly. I think the problem you're seeing comes from the hardcoded values for height and textsize. You really ought to use getDimension(), getDimensionPixelSize() and getDimensionPixelOffset() consistently. Any particular reason why you don't code this in xml? Much easier to trouble-shoot these sorts of issues. – 323go Feb 13 '16 at 18:36
  • @323go I'll try out today. There are lots of different dynamic screens so I chose to build up the view programmatically at runtime. I have the same problem with XML though. – Jim Clermonts Feb 14 '16 at 08:03
  • @323go I've added offset (getDimensionPixelOffset) and pxSize (getDimensionPixelSize) to the code, but these values don't solve the problem. for 1 DIP they return both 50 and for 3 DIP they return both 120. So they're just bottom * 3 – Jim Clermonts Feb 14 '16 at 08:16
  • you might consider programmatically choosing between half a dozen hardcoded layouts for each screen and having a default layout for the ones that dont fit your pre established dimensions. – tony gil Feb 14 '16 at 11:36
  • @tonygil that doesn't solve the problem. The problem is: TextView scales on different resolutions on different heights. – Jim Clermonts Feb 14 '16 at 13:33
  • do you fixed this? – Swaminathan V Apr 07 '16 at 08:21
  • Dear @RaguSwaminathan no. The problem persists. I'm thinking about consulting an expert because I've wrote numerous functions per device to solve the problem but there must be a better way. – Jim Clermonts Apr 09 '16 at 19:16
  • can you attach images showing layout bounds? – stallianz Apr 11 '16 at 19:23

3 Answers3

3

Android groups all actual screen sizes into four generalized sizes: small, normal, large, and extra-large.

Create three floder for "layout" for normal,small screen layout-large for large screen layout-xlarge for extra-large screen

Add same bars_layout.xml into all floder and increase size as per screen size(For example if your keep size in layout floder 10 dp then for layout-large increase that size to 30 dp and for layout-xlarge make 60dp)

For dimens.xml small and normal screen add it to "values" floder For large screen add to values-large floder For extra-large add to values-xlarge

megha jagdale
  • 396
  • 2
  • 4
1

setMargins(int,int,int,int) is using pixels, not DIP, so on every device same size in pixels looks different, you need to convert DIP into pixels and then setMargins() with that value

Wackaloon
  • 2,285
  • 1
  • 16
  • 33
0

multiply text height with densityDpi and textsize with scaledDensity values. So it will support different resolutions.

DisplayMetrics dm = context.getResources().getDisplayMetrics();
int densityDpi = dm.densityDpi;
float spi = dm.scaledDensity;
textheight = 100 * densityDpi;
textSize = 20 * spi;
ugur
  • 3,604
  • 3
  • 26
  • 57