I have a simple task:
Create a menu with few items which opens on a toggle action bar button.
My activity_main is:
<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"
tools:context="com.example.mt.gmtelandroid.MainActivity"
android:id="@+id/drawer_layout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="@menu/navigation_menu"
android:layout_gravity="start"
app:headerLayout="@layout/navigation_header"
>
</android.support.design.widget.NavigationView>
and navigation menu
<menu xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/meni1" >
<item android:id="@+id/navhome"
android:title="Home"
android:icon="@mipmap/ic_home_black_24dp"
>
</item>
<item android:id="@+id/navtasks" android:title="Zadaci"
android:icon="@mipmap/ic_drive_eta_black_24dp"
></item>
<item android:id="@+id/navlogout" android:title="Odjava"
android:icon="@mipmap/ic_close_black_24dp">
</item>
<item android:id="@+id/myname" android:title="Moj Nalog"
android:icon="@mipmap/ic_account_circle_black_24dp">
</item>
Main activity is
public class MainActivity extends AppCompatActivity
{
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mtogle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
mtogle = new ActionBarDrawerToggle(this, mDrawerLayout,R.string.open_menu, R.string.close_menu);
mDrawerLayout.addDrawerListener(mtogle);
mtogle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mtogle.onOptionsItemSelected(item))
{
return true;
}
return super.onOptionsItemSelected(item);
}
}
I don't know how to add a listener onMenuItemClick so I can handle what happens when a user clicks on a menu item. I have tried adding some code to onOptionsItemSelected method, but when I was debuging it I found out that this method is only called on toggle buton click, not on menu item click!?
You have to implement the callbacks for NavigationView.OnNavigationItemSelectedListener
in Activity
as below,
public class HomeActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener
than handle click event of NavigationView
like below code
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
// Handle navigation view item clicks here.
int id = item.getItemId();
Intent i;
if (id == R.id.navhome) {
// perform your action here
} else if (id == R.id.navtasks) {
// perform your action here
} else if (id == R.id.navlogout) {
// perform your action here
}else if (id == R.id.myname) {
// perform your action here
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}