I am new to android. I would like to implement a navigation drawer consisting of a list of items, which when clicked opens a new activity. Basically a navigation drawer across all the activities. When I select an item from the navigation drawer that particular activity opens.navigation drawer code is implemented by taking empty activity. I would like to implement navigation drawer functionality in all activities that activities are taken as empty activity in that activities already have some functionality along with this functionality the navigation drawer functionality also works. Please help me.
This is activity_header File
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer"
tools:context=".MainActivity">
<android.support.design.widget.NavigationView
app:headerLayout="@layout/header"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@color/white"
app:menu="@menu/drawermenu"
android:layout_gravity="start"
android:id="@+id/navigationView"
>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
This is My Main Activity
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="2"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:layout_marginTop="30dp"/>
This is Header Activity java code
public class HeaderActivity extends AppCompatActivity {
protected DrawerLayout myDrawerLayout;
protected ActionBarDrawerToggle toggle;
protected NavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_header);
myDrawerLayout=(DrawerLayout)findViewById(R.id.drawer);
navigationView=(NavigationView) findViewById(R.id.navigationView);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
Log.e("log_cat", "Error Response" + "Error");
switch (menuItem.getItemId())
{
case R.id.dashboard:
Toast.makeText(HeaderActivity.this,"Dashboard Clicked",Toast.LENGTH_LONG).show();
break;
case R.id.devotional:
Toast.makeText(HeaderActivity.this,"devotional Clicked",Toast.LENGTH_LONG).show();
break;
case R.id.inspiration:
Toast.makeText(HeaderActivity.this,"inspiration Clicked",Toast.LENGTH_LONG).show();
break;
case R.id.happy:
Toast.makeText(HeaderActivity.this,"happy Clicked",Toast.LENGTH_LONG).show();
break;
case R.id.sad:
Toast.makeText(HeaderActivity.this,"sad Clicked",Toast.LENGTH_LONG).show();
break;
case R.id.love:
Toast.makeText(HeaderActivity.this,"love Clicked",Toast.LENGTH_LONG).show();
break;
}
return false;
}
});
toggle = new ActionBarDrawerToggle(
this,
myDrawerLayout,
R.string.open,
R.string.close
) {
public void onDrawerClosed(View view) {
//Snackbar.make(view, R.string.drawer_close, Snackbar.LENGTH_SHORT).show();
}
public void onDrawerOpened(View drawerView) {
//Snackbar.make(drawerView, R.string.drawer_open, Snackbar.LENGTH_SHORT).show();
}
};
myDrawerLayout.addDrawerListener(toggle);
toggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// gridview.setAdapter(i);
}
@Override
public boolean onOptionsItemSelected (MenuItem item) {
if (toggle.onOptionsItemSelected (item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
This is My main activity java code
public class MainActivity extends HeaderActivity {
GridView grid;
public static String[] osNameList = {
"Devotional",
"Inspirational",
"Happy",
"Sad",
"Love"
} ;
public static int[] osImages = {
R.drawable.devotional,
R.drawable.inspirational,
R.drawable.happy,
R.drawable.sad,
R.drawable.love
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getLayoutInflater().inflate(R.layout.activity_main, myDrawerLayout);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this, osNameList, osImages));
}
}
In this code setNavigationItemSelectedListener is not working in mainactivity but setOnClickListener on Grid is working. When we click on any navigation menu element it move to that activity.