Andrei
Andrei

Reputation: 77

Extending a base activity makes my other activity unusable

I wanted to have an navigation drawer that would be present in multiple activities, thus I created a BaseDrawerActivity class which looks something like this

public class BaseDrawerActivity extends AppCompatActivity {
    protected RelativeLayout fullLayout;
    protected FrameLayout frameLayout;

    @Override
    public void setContentView(int layoutResID) {

        fullLayout = (RelativeLayout) getLayoutInflater().inflate(R.layout.drawer_n_activity, null);
        frameLayout = fullLayout.findViewById(R.id.drawer_frame);

        getLayoutInflater().inflate(layoutResID, frameLayout, true);

        super.setContentView(fullLayout);
        //...    

    }
//...
//(other navigation drawer stuff)
//...

And one of my activities that want to contain that Navigation Drawer looks something like

public class ExerciseActivity extends BaseDrawerActivity {
//...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exercise);


        //...
    }
    //...

My navigator drawer is present in the above activity but its content is unclickable/unusable. It's like the things from BaseDrawerActivity is on top of it.

Is it anything I'm missing

I know this can be done with fragments too, but I'm not interested in that.

EDIT: I was asked for .xml files.

activity_exercise.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:layout_editor_absoluteY="81dp">

    <ListView
        android:id="@+id/lvItems"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

And this is the one for BaseDrawerActivity

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

    <FrameLayout
        android:id="@+id/drawer_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <ListView
            android:id="@+id/navList"
            android:layout_width="200dp"
            android:layout_height="match_parent"
            android:layout_gravity="left|start"
            android:background="#ffeeeeee" />

    </android.support.v4.widget.DrawerLayout>
</RelativeLayout>

Upvotes: 2

Views: 110

Answers (1)

cincy_anddeveloper
cincy_anddeveloper

Reputation: 1152

This isn't the correct usage of the Navigation Drawer pattern. Take a look at link https://developer.android.com/training/implementing-navigation/nav-drawer.html.

What is happening, is that your have your layout file composed wrong. The DrawerLayout expects, to children one, that will behave as the sliding drawer, and one that will act as the main content that gets displayed when the drawer is closed. If your layout code, you don't have a second child to act as the main content inside of the DrawerLayout. Instead, you've created a FrameLayout and added it as a sibling to DrawerLayout (incorrect). Since you assigned the ListView inside of DrawerLayout to be the drawer i.e. apply a gravity, and no main content, the DrawerLayout draws nothing for where the main content would go, essentially leaving a huge window, that covers the entire width and height the DrawerLayout reaches. Due to how Android's view system work, everything is drawing bottom up, so previous children View's are drawn before the top most view in a Layout containe r(remember how I said the FrameLayout was a sibling to DrawerLayout). So, this means that your FrameLayout content is being drawn behind the DrawerLayout but since there is no main content to be drawn by DrawerLayout, you see what was drawn below it, thus you see UI from the file 'activity_exercise.xml'. Note, even though the DrawerLayout isn't drawing anything, it is still intercepts click events for where it's content should be drawn at.

This is more along the lines of what you want:

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

    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

      <FrameLayout
           android:id="@+id/drawer_frame"
           android:layout_width="match_parent"
           android:layout_height="match_parent" />

        <ListView
            android:id="@+id/navList"
            android:layout_width="200dp"
            android:layout_height="match_parent"
            android:layout_gravity="left|start"
            android:background="#ffeeeeee" />

    </android.support.v4.widget.DrawerLayout>
</RelativeLayout>

Upvotes: 1

Related Questions