-1

I have an ImageView with height set to 120dp. When I get the view.height it equalls to 360, why won't it be equal to 120?

I'm not sure of the code won't be of help but:

    <ImageView
        android:id="@+id/profilePic1"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:scaleType="centerCrop"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:clickable="true"
        app:srcCompat="@drawable/profile_pic1"/>

Activity:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    val layoutInflater: LayoutInflater = LayoutInflater.from(this)
    val view: View = layoutInflater.inflate(R.layout.activity_edit_profile, null)

    view.viewTreeObserver.addOnGlobalLayoutListener(object: ViewTreeObserver.OnGlobalLayoutListener {
        override fun onGlobalLayout() {
            // remove listener
            view.viewTreeObserver.removeOnGlobalLayoutListener(this)

            // insert coordinates (coor) of each image to imagePosition variables
            Log.d("TAG", profilePic1.height.toString()) // RETURNS 360

        }
    })
sir-haver
  • 3,096
  • 7
  • 41
  • 85

5 Answers5

2

Because in xml file you have given size in DP and In java that parameter returns values in Pixel , Please check this for more understanding about DP and PIxel

https://developer.android.com/training/multiscreen/screendensities

36x36 (0.75x) for low-density (ldpi)
48x48 (1.0x baseline) for medium-density (mdpi)
72x72 (1.5x) for high-density (hdpi)
96x96 (2.0x) for extra-high-density (xhdpi)
144x144 (3.0x) for extra-extra-high-density (xxhdpi)
192x192 (4.0x) for extra-extra-extra-high-density (xxxhdpi)
Avinash tiwari
  • 269
  • 1
  • 11
2

This is just because you are using XXHDPI device, where 120dp is being converted to 360PX.

Must read What is the difference between "px", "dip", "dp" and "sp"?


You should read Image resolution for mdpi, hdpi, xhdpi and xxhdpi to understand how DP and pixels work together. And official documentation can be found here


If I explain it in short, in MDPI 1dp is almost equals to 1PX. So if you check your code on devices of MDPI resolution you will get 120PX output. But as I added a URL above for other resolutions it follows a rule of conversion from dp to PX. It would give you 180px in HDPI, 240px in XHDPI, 360px in XXHDPI, 480px in XXXHDPI devices as output.

Shadow Droid
  • 1,696
  • 1
  • 12
  • 26
Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
1

In java you get the height in px instead of dp. You have to convert it to dp to get correct data:

float px = profilePic1.height;
float dp = px / context.getResources().getDisplayMetrics().density;

// here dp should be 120
Md. Asaduzzaman
  • 14,963
  • 2
  • 34
  • 46
0

There is a library called sdp and ssp for making dimensions responsive.

Niraj
  • 903
  • 8
  • 23
0

Because you have given height in dp(density pixel) while in the code when you retrieve it , it is retrieved in pixels only. You have to convert it to dp.

intellignt_idiot
  • 1,962
  • 2
  • 16
  • 23