1

I'm making an Activity with a Image View in their XML file, I'm accessing to all the View using Kotlinx extension as:

import kotlinx.android.synthetic.main.activity_registrar_usuario.*

I need to have access to the ImageView heigh and with from the code behind, but it is giving me 0 inside the onCreate() method, but I can have these number later in a method apart (I need them in onCreate to set some variables).

The strange thing is that I can set ImageView properties without problem (tested with set color filter tho change ImageView color).

So How can I use kotlinx extension to get the ImageView height and width?

This is my code (I'm using a Toast to show the height and with easily, at this moment it always gives me 0).

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_registrar_usuario)


    setSupportActionBar(toolbar)
    val ab = supportActionBar
    ab!!.setDisplayHomeAsUpEnabled(true)

    targetW = 500
    targetH = 500

    iconoUsuarioIV.setColorFilter(Color.RED)

    Toast.makeText(this@RegistrarUsuarioActivity,"Datos del ImageView: " + iconoUsuarioIV.height.toString() + "  " + iconoUsuarioIV.width.toString(), Toast.LENGTH_SHORT).show()

}

and their respective XML:

            <ImageView
                android:id="@+id/iconoUsuarioIV"
                android:layout_width="300dp"
                android:layout_height="300dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="16dp"
                android:layout_marginEnd="8dp"
                android:src="@drawable/ic_user"

                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
Eduardo Corona
  • 1,262
  • 4
  • 20
  • 31
  • See this SO page: [When are views initialized in lifecycle?](https://stackoverflow.com/questions/49931394/android-kotlin-when-are-views-initialized-in-lifecycle) – Sanlok Lee Feb 03 '19 at 22:52

0 Answers0