Sunday, 29 September 2019

How to access sub elements of a custom FrameLayout in a library project?

I am extending a "MapView" that itself extends FrameLayout. But I can't access the ui elements I added: level_layout and *level_scroll. I have no trouble adding the code directly to my MainActivity, but I can't get it to work in a library.

<?xml version="1.0" encoding="utf-8"?>
<android.company.com.library.mapbox.MyMapView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/mapView">

        <ScrollView
            android:id="@+id/level_scroll"
            android:layout_width="50dp"
            android:layout_height="250dp"
            android:layout_gravity="right"
            android:layout_marginTop="100dp"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/level_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"></LinearLayout>
        </ScrollView>
</android.company.com.library.mapbox.MyMapView>

In MyMapView.java I am getting a value from:

int level = R.id.level_layout;

But linear is null when trying to get the LinearLayout

LinearLayout linear = (LinearLayout) findViewById(R.id.level_layout);

I tried Adding

@Override
protected void onFinishInflate() {
    super.onFinishInflate();
}

(I try to access my ui elements after this is called)

Calling the following in different places (e.g. constructors)

LayoutInflater inflater = (LayoutInflater)this.getContext().getSystemService
        (Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.nameOfTheXmlFile, null);

I don't have multiple layout files with the same name.

I can't use setContentView(...) since this is a View and not an Activity?



from How to access sub elements of a custom FrameLayout in a library project?

No comments:

Post a Comment