4

When adding a view in an axml file, it is possible to simply specify the size and the units of the view's attribute, for example:

<TextView
    android:TextSize = "10sp"
    android:layout_marginTop = "10dp" />

As said in this answer, there are specific units for specific purposes.

My main question is, when applying a size programmatically (by code) in a dynamic way, what are the units applied for the size?

For example, when declaring a TextSize like this:

TextView tv = new TextView();
tv.TextSize = 10;

What are the units applied for the text size? sp? dp? px?

And most importantly, how can I change them to fit my needs?

Daniel Reyhanian
  • 579
  • 4
  • 26
  • Have you read the [doc](https://developer.android.com/reference/android/widget/TextView#setTextSize(float))? This method changes size using `sp`, You can use other overload for specific units. – Pawel Feb 06 '19 at 12:01
  • @Pawel It was just an example. What about other attributes? How it works? – Daniel Reyhanian Feb 06 '19 at 12:04
  • I think most (especially layout parameters) take raw pixel size as an argument, but You should always see the doc. – Pawel Feb 06 '19 at 12:08
  • @Pawel So I'd probably want to change it in order to run the program smoothly on other devices, right? – Daniel Reyhanian Feb 06 '19 at 12:27

2 Answers2

4

Hi @Daniel if u programmatically generate textview as following code

TextView tv = new TextView();
tv.setTextSize(10); // Sets text in sp (Scaled Pixel).

And if you want to set text size with other unit so you can achieved by following way.

TextView tv = new TextView();
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, 10); // Sets text in px (Pixel).
tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 10); // Sets text in dip (Device Independent Pixels).
tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10); // Sets text in sp (Scaled Pixel).
tv.setTextSize(TypedValue.COMPLEX_UNIT_PT, 10); // Sets text in pt (Points).
tv.setTextSize(TypedValue.COMPLEX_UNIT_IN, 10); // Sets text in in (inches).
tv.setTextSize(TypedValue.COMPLEX_UNIT_MM, 10); // Sets text in mm (millimeters).

By default Android uses "sp" for text size and "px" for view size.

For other View sizes we can set in px(pixels) but if you want customize the unit you can use custom methods

/**
     * Converts dip to px.
     *
     * @param context -  Context of calling class.
     * @param dip     - Value in dip to convert.
     * @return - Converted px value.
     */
    public static int convertDipToPixels(Context context, int dip) {
        if (context == null)
            return 0;
        Resources resources = context.getResources();
        float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, resources.getDisplayMetrics());
        return (int) px;
    }

From above method you can convert YOUR_DESIRED_UNIT in pixels and then set to view. You can replace

TypedValue.COMPLEX_UNIT_DIP

with above unit as per you use case. You can also use it vice-versa for px to dip but we cant assign to custom unit to view so that's why i am using it like this.

I hope i explained well this.

Arpit bandil
  • 204
  • 2
  • 9
  • Thanks, you definitely did, but the TextSize was just an example. How it works for other View attributes? And the method for setting the units is for java, yes? How can I do it in C#? – Daniel Reyhanian Feb 06 '19 at 12:22
  • I edited my question as per you comment and i don't about C#. :) – Arpit bandil Feb 06 '19 at 12:36
  • Why would I want to convert dp to px? I thought that it's not recommended to use px since the scale changes from each device. – Daniel Reyhanian Feb 06 '19 at 12:38
  • Yes you are right it's not recommended to use px in android but for set property like margins, padding, height and width programmatically so we have assign in pixels because there is no inbuilt method to convert into specific unit at runtime. So we will convert required unit into pixels and assigns to it. Remember we can only assign unit in XML but not in JAVA. – Arpit bandil Feb 07 '19 at 04:01
2

First:

I think you should avoid from set size programmatically as much as possible.

Second:

px Pixels : corresponds to actual pixels on the screen.

dp or dip Density-independent Pixels- : an abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi screen, so one dp is one pixel on a 160 dpi screen

sp Scale-independent Pixels- : this is like the dp unit, but it is also scaled by the user's font size preference

And in your third question , i think :

for example :

for a edittext you should not use constant for width like this :

  <TextView
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:text="@string/banklist_firstselectbank"
        style="@style/TextAppearanceHeadline2"
        android:gravity="center"/>

I think its better to use margin start and margin end like this :

 <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:text="@string/banklist_firstselectbank"
        style="@style/TextAppearanceHeadline2"
        android:layout_marginEnd="50dp"
        android:layout_marginStart="50dp"
        android:gravity="center"
        />

And use as much as possible fields like : gravity and other instead of constant number.

milad salimi
  • 1,580
  • 2
  • 12
  • 31