0

I have extended the TextView and added support for borders, the thing is when I am drawing a border I need to put padding on the bordered side, so that the text would move.

I set my widths of borders in pixels, and it draws them accordingly, but on my TF201 tablet when I setPadding on the TextView, out of some reason it multiplies the padding width by 3x in pixels even though the setpadding documentation says it is defined explicitly in pixels.

EDIT:

Even though the answer I have selected is not what was causing my issue, it is a valid answer. The real answer to my question is actually a duplicate from this. Problem was that I have added a value to my padding each time setPadding was called. And it does get called three times on a page that has scrolling to it.

Community
  • 1
  • 1
Bojan Jovanovic
  • 1,439
  • 2
  • 19
  • 26

2 Answers2

2

It might be a issue of pixel density. Its true that setpadding docs asks to set the padding in pixels but are you setting it in px, sp or dp ? If you read Supporting Different Densities document it says and I quote:

Different screens have different pixel densities,so the same number of pixels may correspond to different physical sizes on different devices.

So, when you specify spacing between two views, use dp rather than px:

<Button android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/clickme"
    android:layout_marginTop="20dp" />

When specifying text size, always use sp:

<TextView android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:textSize="20sp" />

Also, based on your comments:

drawRect unit issues android andDraw Rectangle which change size w.r.t different android screen size question might help.

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
  • I am using the draw method to draw pixels onto the view, example: `canvas.drawRect(new Rect(0, 0, getWidth(), border.getWidth()), paint); ` and it really draws a rectangle with a 100px width, and when I call setPadding(100,0,0,0) on the view, it sets my padding 3x on my tablet, while on my cellphone it displays x1. – Bojan Jovanovic Jul 23 '13 at 01:44
  • See if [drawRect unit issues android](http://stackoverflow.com/questions/6922074/drawrect-unit-issues-android) and[Draw Rectangle which change size w.r.t different android screen size](http://stackoverflow.com/questions/6533368/draw-rectangle-which-change-size-w-r-t-different-android-screen-size) question helps. – Shobhit Puri Jul 23 '13 at 01:55
0

While the method may only accept pixel values, that sadly doesn't save you from needing to take screen densities into account. Instead, you need to determine your values in terms of DP and then programmatically calculate the pixel equivalents at runtime. Fortunately, there are some built-in methods to help you out. This can be done with the following code:

/// Converts 14 dip into its equivalent px

int dimensionInDp = 14;    

Resources r = getResources();
float dimensionInPixels = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dimensionInDp, r.getDisplayMetrics());

Although the result is a float, you can easily cast it to an int for use in your setPadding(...) method.

(Referencing: Converting pixels to dp)

Community
  • 1
  • 1
Brian
  • 1,436
  • 8
  • 18