Solution 1 :
The error is in your layout.
You can check the doc of the DrawerLayout
:
To use a
DrawerLayout
, position your primary content view as the first child with width and height of match_parent and no<layout_gravity>
Change your layout:
<androidx.drawerlayout.widget.DrawerLayout
.....
tools_openDrawer="start">
<!-- NavigationView -->
<com.google.android.material.navigation.NavigationView
../>
<!-- main content view -->
<LinearLayout...>
</LinearLayout>
</androidx.drawerlayout.widget.DrawerLayout>
To:
<androidx.drawerlayout.widget.DrawerLayout
.....
tools_openDrawer="start">
<!-- main content view -->
<LinearLayout...>
</LinearLayout>
<!-- NavigationView -->
<com.google.android.material.navigation.NavigationView
../>
</androidx.drawerlayout.widget.DrawerLayout>
Solution 2 :
activity_main
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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"
tools_context=".MainActivity">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android_layout_width="match_parent"
android_layout_height="match_parent"
tools_context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.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" />
</com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<com.google.android.material.navigation.NavigationView
android_id="@+id/nav_view"
android_layout_width="wrap_content"
android_layout_height="match_parent"
android_layout_gravity="start"
android_fitsSystemWindows="true"
app_menu="@menu/setting_menu" />
</androidx.drawerlayout.widget.DrawerLayout>
MainActivity
class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
val toggle = ActionBarDrawerToggle(
this,
drawer_layout,
toolbar,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close
)
drawer_layout.addDrawerListener(toggle)
toggle.syncState()
nav_view.setNavigationItemSelectedListener(this)
}
override fun onNavigationItemSelected(item: MenuItem): Boolean {
Log.d("abc",item.itemId.toString())
drawer_layout.closeDrawer(GravityCompat.START)
return true
}}
Solution 3 :
Try create a listner then passing to setNavigationItemSelectedlistener instead of “this”.
Like following:
private NavigationView.OnNavigationItemSelectedListener navListener = new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch(item.getItemId()) {
case R.id.nav_slideshow:
appBarTV.setText("Slideshow Page");
Toast.makeText(this, "Slideshow", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_manage:
appBarTV.setText("Tools Page");
Toast.makeText(this, "Tools", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_share:
appBarTV.setText("Share Page");
Toast.makeText(this, "Share", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_send:
appBarTV.setText("Send");
Toast.makeText(this, "Send", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
}
then to you call:
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(navListener)
Problem :
I try to add onNavigationItemSelected
to DrawerLayout
but it does not work. I can see the options but when I click option the DrawerLayout
is closing and nothing happened.
I don’t know what is a problem and for a long time, I searched for a solution and could not find it.
xml code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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_orientation="vertical"
>
<androidx.drawerlayout.widget.DrawerLayout
android_id="@+id/drawer_layout"
android_layout_width="match_parent"
android_layout_height="match_parent"
android_fitsSystemWindows="true"
tools_openDrawer="start">
<com.google.android.material.navigation.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_main"
app_menu="@menu/activity_main_drawer" />
<LinearLayout
android_layout_width="match_parent"
android_layout_height="match_parent"
android_orientation="vertical">
<include layout="@layout/app_bar_main" />
<com.google.android.material.tabs.TabLayout
android_id="@+id/tablayout"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_background="@color/colorPrimary"
app_tabSelectedTextColor="@android:color/white"
app_tabTextColor="@android:color/black">
<com.google.android.material.tabs.TabItem
android_id="@+id/tabChats"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_text="Table1" />
<com.google.android.material.tabs.TabItem
android_id="@+id/tabStatus"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_text="Table2" />
</com.google.android.material.tabs.TabLayout>
<androidx.viewpager.widget.ViewPager
android_id="@+id/viewPager"
android_layout_width="match_parent"
android_layout_height="wrap_content" />
</LinearLayout>
</androidx.drawerlayout.widget.DrawerLayout>
</RelativeLayout>
and java code
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawer;
private TextView appBarTV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
appBarTV = findViewById(R.id.appbar_text_view);
ImageButton menuRight = findViewById(R.id.leftRight);
menuRight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
drawer.openDrawer(GravityCompat.START);
}
}
});
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
drawer.closeDrawers();
int id = item.getItemId();
if (id == R.id.nav_slideshow) {
appBarTV.setText("Slideshow Page");
Toast.makeText(this, "Slideshow", Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_manage) {
appBarTV.setText("Tools Page");
Toast.makeText(this, "Tools", Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_share) {
appBarTV.setText("Share Page");
Toast.makeText(this, "Share", Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_send) {
appBarTV.setText("Send");
Toast.makeText(this, "Send", Toast.LENGTH_SHORT).show();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
and here is the full source code.
Comments
Comment posted by MMG
What is the error?
Comment posted by marwan
Nothing happens when I click
Comment posted by marwan
Thank you brother .. yes it was a problem in arranging the items.. I use also…. navigationView.bringToFront();
Comment posted by marwan
my code is java and you are delete table layout I need use It with table
Comment posted by MMG
No matter, have my code as correct code and change it as you want @marwan
Comment posted by marwan
my brother .. how I can do it it’s different about my code