I have a custom map marker that I would like to scale based on the text of the marker. I want to retrieve the width of this marker. The issue I am facing is that when the app is first launched, the width value is 0. I am getting the width too early. But I am unable to get ViewTreeObserver to actually work. What am I doing wrong, is it possible I am not referencing the correct view?
// custom marker
mCustomMarkerView = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.custom_map_marker, null);
tvCustomMarkerTitle = (FontedTextView) mCustomMarkerView.findViewById(R.id.tv_marker_title);
ivCustomMarker = (ImageView) mCustomMarkerView.findViewById(R.id.iv_marker_bg);
rlCustomMarkerWrapper = (RelativeLayout) mCustomMarkerView.findViewById(R.id.rl_custom_marker_wrapper);
rlCustomMarkerWrapper.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// do some work
}
});
The XML looks like the following
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/rl_custom_marker_wrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!-- custom marker image -->
<ImageView
android:id="@+id/iv_marker_bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/custom_marker"/>
<!-- custom marker title -->
<TextView
android:id="@+id/tv_marker_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="@dimen/font_size_14"/>
</RelativeLayout>
</FrameLayout>
Thank you in advanced!