I am trying to find the position of widgets but somehow it always returns 0
. I have followed most suggestions but all of them still returns 0
. I finally ended up with this, but still it returns a 0
. I have read somewhere that the reason it returns a 0
is because I am calling for the position before onCreate()
has finished parsing the layout specified in the accompanying .xml
file.
Where then should I put the call to the positions then?
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fuelCalculationConstraintLayout"
tools:context=".FuelCalculation">
<ImageView
android:id="@+id/imageViewIconHome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/icon_home" />
<TextView
android:id="@+id/textViewSettingValues"
android:text="Settings:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/imageViewIconHome"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:height="30dp"
android:width="100dp"/>
<TextView
android:id="@+id/textViewScratchPad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scratch Pad"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.fuelcalculation3
import android.content.Context
import android.content.Intent
import android.graphics.Point
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class FuelCalculation : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_fuel_calculation)
val updateTextViewSettingValues = findViewById<TextView>(R.id.textViewSettingValues)
val updateTextViewScratchPad = findViewById<TextView>(R.id.textViewScratchPad)
val imageIconHomeClick = findViewById<ImageView>(R.id.imageViewIconHome)
imageIconHomeClick.setOnClickListener(){
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
}
fun View.getLocationOnScreen(): Point {
val location = IntArray(2)
this.getLocationOnScreen(location)
return Point(location[0], location[1])
}
val location = updateTextViewSettingValues.getLocationOnScreen()
val absX = location.x
val absY = location.y
val getTextViewSettingsRootView = updateTextViewSettingValues.rootView
val getTextViewSettingsValueWidth = updateTextViewSettingValues.width
val getTextViewSettingsValueTop = updateTextViewSettingValues.top
val getTextViewSettingsValueLeft = updateTextViewSettingValues.left
val getTextViewSettingsValueRight = updateTextViewSettingValues.right
val getTextViewSettingsValueMeasuredWidth = updateTextViewSettingValues.measuredWidth
val getTextViewSettingsValueA = updateTextViewSettingValues.measuredWidth
updateTextViewScratchPad.text = "Top=$getTextViewSettingsValueTop" // returns 0
updateTextViewScratchPad.text = "$absX" // returns 0
}
}