0

I am creating a BaseActivity.java class to contain a navigation view and toolbar.

BaseActivity.java

public class BaseActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{

    private Toolbar toolbar;
    private DrawerLayout drawerLayout;
    private ActionBarDrawerToggle actionBarDrawerToggle;
    private NavigationView navigationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        //OTHER CODES TO SET UP NAVIGATION VIEW...


    }
}

I am creating a activity_base.xml file that contains DrawerLayout

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:orientation="vertical"
        tools:context="com.example.sumit.movie.MovieActivity">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

        </android.support.design.widget.AppBarLayout>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/fragment_container">   ***MY PLACEHOLDER FRAGMENT***

        </FrameLayout>


    </LinearLayout>



    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_movie"
        app:menu="@menu/activity_movie_drawer" />

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

As you can see, I have a fragment_container inside this file, which I thought I would use to plug the views from another activites. For every activity, I would create a corresponding fragment and plug that fragment into this fragment_container.

But then what would I do with the xml file of the activity itself ?

I have read other answers on how to do this.
They say to create a BaseActivity.java class and extend this class to create other activities.
But do we have to create a xml file for this activity too, which would create the navigation view and the toolbar which would remain consistent across all activities ?

And if i don't create activity_base.xml, and merely create the class, Won't I have to write REDUNDENT codes for setting the navigation drawer and toolbar in every activity's xml file ?

Please help me on how this is done. I am new to android.

Community
  • 1
  • 1
Sumit Jha
  • 2,095
  • 2
  • 21
  • 36

3 Answers3

1

For this use case you might not need to have an activity xml for every page you can just have an activity class that extends BaseActivity that customises the activity / toolbar / nav draw and then you can pull common functions up to the base activity. so the BaseActivity does the setup in it on create then customisation happen per activity class.

siliconeagle
  • 7,379
  • 3
  • 29
  • 41
1

I faced the similar problem. But, in my case there was custom NavigationDrawer and ActivityLayout used databinding. The solution is to use the same viewModel for NavigationDrawer and create layout for each Activity. Create abstract BaseDrawerActivity, which has abstract method for getting layout resource Id. Every derived Activity will specify its own resource.

Alex Shutov
  • 3,217
  • 2
  • 13
  • 11
1

Just create one activity which will going to extend NavigationDrawerActivity and setup all functionalities to that activity like onclick listener setting textviews and all.. now If you want a activity to have Navigation Drawer You just Need to extend that activity which extended The NavigationDrawerActivity and instead of using setContentView instead You have to use

   addview() 

with the inflated View.. Thats all..

Hope it helps!!!

Mr.Popular
  • 845
  • 1
  • 8
  • 15