I need to know the bottom Y of a TextView when its size changed (either by text change or width change by screen rotation) to set the padding of a Google Maps view. Below is NOT the real code but a simplified sample code just to demonstrate the problem.
The TextView is inside another layout so getting its top returns 0. But of course, that TextView's top in the top layout is not 0. I have tried the code below, but I got zeros.
loc[0] = 0, loc1 = 0
top = 0, height = 86
Also, I am not sure if getLocationInWindow
is right. I mean, if what I am thinking is correct, that may include the AppBar? But that the AppBar is outside the top layout (for example, RelativeLayout
in my main_activity.xml), so I think I should get the location in the top layout, not in the window. What is the correct way to get the location in the top layout?
class MainActivity : AppCompatActivity(),
{
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var loc = IntArray(2);
tvAny.getLocationInWindow(loc);
tvAny.addOnLayoutChangeListener { v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom ->
val height = bottom - top;
Log.d("so", "loc[0] = " + loc[0] +", loc[1] = " + loc[1])
Log.d("so", "top = " + top +", height = " +height)
}
}
}
And activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:layout_margin="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvAny"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#55FF0000"
android:text="I don't want any damn vegetables."/>
</FrameLayout>
</RelativeLayout>