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.