2

According to this video

If I want create some element, which must looks the same on devices with different dpi (Phone, Tablet, TV).

As shown on video (at 4:52), I must specify dimensions of this element in dp.

But I got this (Redmi Note vs Redmi 1s (android 4.2.2 on both)):

(I use IntelliJ Idea 14)

Here is my code from activity:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical"
              tools:context=".MainActivity">

    <TextView

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        android:textSize="60sp"/>

    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="#ff080808">

    </LinearLayout>

    <Button
        android:id="@+id/button"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:text="New Button"/>

</LinearLayout>

Questions:

Why line height is not match on phones?

Why button is smaller on smaller screen?

Emil Lundberg
  • 7,268
  • 6
  • 37
  • 53
Avoxur
  • 31
  • 2
  • Possible duplicate of [What is the difference between "px", "dp", "dip" and "sp" on Android?](http://stackoverflow.com/questions/2025282/what-is-the-difference-between-px-dp-dip-and-sp-on-android) – Shaishav Jogani Aug 26 '16 at 08:23

1 Answers1

0
  • px is one pixel.
  • sp is scale-independent pixels.
  • dip is Density-independent pixels.

You would use

sp for font sizes

dip for enter code hereeverything else.

dip==dp

enter image description here

dp

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. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Note: The compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp".

enter image description here

Refer this question

Community
  • 1
  • 1
Shaishav Jogani
  • 2,111
  • 3
  • 23
  • 33