2

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?

Phantômaxx
  • 36,442
  • 21
  • 78
  • 108
Heinzlmaen
  • 482
  • 3
  • 14

1 Answers1

3

Official documentation to MapView here explicitly says

Note: You are advised not to add children to this view.

This means that MapView custom inner views are supported only partially, or not supported at all. I would not recommend you to use something that is badly supported since it may produce weird bugs as the one you have. Especially in MapView with its complicated touch listener and extensive inner UI - you can easily break something(for example zoom or camera swiping with your ScrollView)

Frankly I'm not sure why are you doing this - you can easily place all the views you need in ordinary FrameLayout and everything will work fine(except maybe ScrollView swipe gesture processor may disrupt MapView gestures)

So it would be wiser do to it like this

<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <android.company.com.library.mapbox.MyMapView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/mapView"/>

        <FrameLayout
            android:id="@+id/wrapper_layout"
            android:layout_width="50dp"
            android:layout_height="250dp"
            android:layout_gravity="right"
            android:layout_marginTop="100dp">

            <ScrollView
                android:id="@+id/level_scroll"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <LinearLayout
                    android:id="@+id/level_layout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">
                </LinearLayout>

            </ScrollView>

        </FrameLayout>

</FrameLayout>

This way you will be able to leverage all the possibilities of MapView and FrameLayout separately. Don't forget to handle lifecycle changes for the MapView in your fragment/activity as it is told here.

You may also notice that I wrapped ScrollView with FrameLayout and gave this wrapper layout wight and height, while ScrollView has match_parent. I've done that because ScrollView with defined height and width can be a real pain in the arse and is quite bug prone, thus wrapping it in some other layout is recommended workaround for such purposes.

While it is not the exact solution to your problem, I hope this answer will help you somehow.

Pavlo Ostasha
  • 8,161
  • 6
  • 23