My navigation drawer are showing icon in a filter not real color
how t remove this filter
i am new please help me step by step.
Thank you!
Screenshot:
This issue is duplicated.
But I add answer for explanation.
Your layout file is like this:
(activity_main.xml)
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
...
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/navigation_item"/>
</android.support.v4.widget.DrawerLayout>
Your Activity is like this: (MainActivity.java)
Call navigationView.setItemIconTintList.
public class MainActivity extends AppCompatActivity {
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.setItemIconTintList(null);
...
}
...
}
Related
I add Badge on a button like this way.
I want to add Badge on hamburger navigation menu icon with this way but I don't know how.
I found a few solutions but none use BadgeDrawable.
This is my code but the result is not what I want :
activity_main :
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawerLayout_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layoutDirection="rtl">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</FrameLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigationView_main"
android:layout_width="250dp"
android:layout_height="match_parent"
/>
</androidx.drawerlayout.widget.DrawerLayout>
MainActivity :
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NavigationView navigationView = findViewById(R.id.navigationView_main);
DrawerLayout drawerLayout = findViewById(R.id.drawerLayout_main);
Toolbar toolbar = findViewById(R.id.toolbar_main);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this , drawerLayout, toolbar , 0b0, 0);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
navigationView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
BadgeDrawable badgeDrawable = BadgeDrawable.create(MainActivity.this);
badgeDrawable.setVerticalOffset(25);
badgeDrawable.setHorizontalOffset(15);
BadgeUtils.attachBadgeDrawable(badgeDrawable, navigationView, null);
navigationView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
}
}
Result :
I want like this :
My question : Can I add Badge on hamburger navigation menu icon with BadgeDrawable ?
Implement DrawerArrowDrawable class and override the draw(canvas: Canvas) method. For detailed example guide visit this link
https://android.pcsalt.com/add-badge-count-to-navigation-drawer-hamburger-icon-android/
I'm working on an app but I have some troubles with drawer layout. I always getting the error message like I mentioned in the title but I have no such Gravity with LEFT value. All I have are gravities with END values and that's all.
Here is my Java code:
public class HomeActivity extends AppCompatActivity {
private ActionBarDrawerToggle toggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar mainToolBar =findViewById(R.id.main_page_toolbar);
DrawerLayout mainDrawerLayout = findViewById(R.id.main_page_drawer_layout);
NavigationView mainNavigationView = findViewById(R.id.main_page_navigation_view);
setSupportActionBar(mainToolBar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setHomeButtonEnabled(true);
toggle = new ActionBarDrawerToggle(this,mainDrawerLayout,mainToolBar,R.string.openDrawer,R.string.closeDrawer);
mainDrawerLayout.addDrawerListener(toggle);
toggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
toggle.onConfigurationChanged(newConfig);
}
}
And here is my XML code:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="end"
tools:context=".Activities.HomeActivity"
android:id="#+id/main_page_drawer_layout"
android:fitsSystemWindows="true">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="#layout/home_main_page"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
app:headerLayout="#layout/home_side_menu_header_layout"
app:menu="#menu/home_side_menu"
android:layoutDirection="rtl"
android:id="#+id/main_page_navigation_view"/>
You need to handle navigation click on Toolbar like below:
mainToolBar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mainDrawerLayout.isDrawerOpen(GravityCompat.END))
mainDrawerLayout.closeDrawer(GravityCompat.END);
else
mainDrawerLayout.openDrawer(GravityCompat.END);
}
});
Also don't forgot to close drawer whenever needed like below:
mainDrawerLayout.closeDrawer(GravityCompat.END)
I have an app that displays the default activity as:
When clicked the hamburger icon opens left main navigation drawer as:
And when clicked the FILTER button opens another drawer from the right as:
I know how the left navigation drawer is displayed (all java codes and layouts)
What is the code for displaying the drawer that is opening from the right ?
and also i wanna know, how the on click listener is set up even if the FILTER button is outside the toolbar ?
Can anyone help?
Just change the value of tools:openDrawer to end (i.e tools:openDrawer="end"), also add android:layout_gravity="end" in <android.support.design.widget.NavigationView>. Here's the solution.
activity_main.xml
<?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="end">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
//Add this piece of code
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(drawer.isDrawerOpen(Gravity.RIGHT)){
drawer.closeDrawer(Gravity.RIGHT);
}else{
drawer.openDrawer(Gravity.RIGHT);
}
}
});
}
Check out this -http://updateunlimited.blogspot.in/2015/12/double-sided-nav-drawers.html
it will take less time
also check how it open from right -https://stackoverflow.com/a/19358114/4741746 and https://stackoverflow.com/a/32155976/4741746 and https://stackoverflow.com/a/17156831/4741746
Best of luck
I have implemented a navigationview in my app , which was automatically created by AndroidStudio. I picked up the NavigationDrawer Activity when I created a new project and the menu items seem ok , but nothing happend when I click on any of the menu item .
Below is my onNavigationItemSelected() method:
#Override
public boolean onNavigationItemSelected(MenuItem item) {
Toast.makeText(MainActivity.this,"onNavigationItemSelected",Toast.LENGTH_LONG).show();
// Handle navigation view item clicks here.
item.setChecked(true);
int id = item.getItemId();
if (id == R.id.medicalRecord) {
Toast.makeText(MainActivity.this,id,Toast.LENGTH_LONG).show();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
And my onCreate method , I create the NavigationView in it
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear_layout);
if (babyImage.exists()) {
Drawable drawable = Drawable.createFromPath(Environment.getExternalStorageDirectory() + "/babycare/temp.jpg");
linearLayout.setBackground(drawable);
}
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerLayout.openDrawer(Gravity.LEFT);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
Of course , I implemented the NavigationView.OnNavigationItemSelectedListener for my class .
I am wondering whether this NavigationView should be implement in Material Design theme or not . Now the theme used in my app is #style/AppTheme
Please , someone help me out , thank you
I solved my question , just simply change the order in my activity_main.xml .
I have a LinearLayout viewgroup and NavigationView in my DrawerLayout viewgroup , at first the NavigationView is the first in my viewgroup and now I change the order , the first one is the LinearLayout and the second is NavigationView , and it work as it suppose to be . Dear!!!
But can someone tell me why it happend ? Does it matter the view order in a viewgroup regardless the display sequence .
I too had this issue and I finally figured out what was wrong, I initially created a sample project with navigation drawer and the main activity xml was as below,
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<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_main"
app:menu="#menu/activity_main_drawer" />
But in the actual app I made I did a mistake like this,
<?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">
<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_main"
app:menu="#menu/activity_main_drawer" />
<LinearLayout...../>
</android.support.v4.widget.DrawerLayout>
I had a LinearLayout below the NavigationView which was the issue.
Then I did this, everything started to work fine.
<?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.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_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
Key was to put the NavigationView at last. Weird that UI was not disturbed but callback was.
The Navigation Drawer help say this:
The main content view (the FrameLayout above) must be the first child in the DrawerLayout because the XML order implies z-ordering and the drawer must be on top of the content.
regards to this example:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
So this is the explanation, what is kind of annoying is if you have the wrong layout order as a result of an auto-generated template. I have faced this kind of "problems" with Android Studio a few times and is so fustrating.
I've just had this problem but none of the answers help me. But finally I solved.
In my case, the listener wasn't called because I used in my activity:
NavigationUI.setupWithNavController(navigationView, navController);
That overrides my OnNavigationItemSelectedListener, so I removed that line and I've setted my listener as the following:
final NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(new
NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
FragmentManager fm = getSupportFragmentManager();
fm.popBackStackImmediate();
boolean handled = NavigationUI.onNavDestinationSelected(item, navController);
if (handled) {
ViewParent parent = navigationView.getParent();
if (parent instanceof DrawerLayout) {
((DrawerLayout) parent).closeDrawer(navigationView);
}
}
return handled;
}
});
Here's a piece of NavigationUI that override my listener:
public static void setupWithNavController(#NonNull final NavigationView navigationView,
#NonNull final NavController navController) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
boolean handled = onNavDestinationSelected(item, navController);
//******* CUTTED ***************
return handled;
}
});
}
I hope it can helps.
The Navigation Drawer help say this:
The main content view (the FrameLayout above) must be the first child in the DrawerLayout because the XML order implies z-ordering and the drawer must be on top of the content.
regards to this example:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
What is kind of annoying is if you have the wrong layout order as a result of an auto-generated template. I have faced this kind of "problems" with Android Studio a few times and is so fustrating.
you should change the place of tags. shouldn't do NavigationView first. it works.
So i have tried to Add a drawer layout to my project. first it didnt work, and then i open a new project for test it didnt work as well.
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
nothing is showing up, not a an icon nor the drawer itself.
thank you for helping.
Only layout is not enough to implementing working DrawerLayout. You have to additional prepare your activity.
Please analyse code below:
Activity
//You have to use ActionBarActivity from support library
public class ActivityWithSliderDrawer extends ActionBarActivity{
//you need this object to synchronise state with your activity
ActionBarDrawerToggle mActionBarDrawerToggle;
DrawerLayout mDrawerLayout;
#Override
public void onCreate(Bundle savedInstanceState ) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
//here you create ActionBarDrawerToggle object
mActionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.string.menu_is_open, R.string.menu_is_close);
//you set it for Drawer layout
mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);
//if you want to display indicator
// you have to call two methods below
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
// If you ever will use toolbar, you have to call additionally
//setSupportActionBar(toolbarObject)
//but before these first two methods
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
//you have to call syncState() method on ActionBarDrawerToggle object
// here onPostCreate(..) method
super.onPostCreate(savedInstanceState);
mActionBarDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
//you have to call onConfigurationChanged(...) method on ActionBarDrawerToggle object
// here onConfigurationChanged(..) method
mActionBarDrawerToggle.onConfigurationChanged(newConfig);
}
}
activity_test.xml - layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<!-- The navigation drawer -->
<ListView
android:id="#+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#android:color/white"/>
</android.support.v4.widget.DrawerLayout>
Declaration in manifest
<activity
android:name="your.package.ActivityWithSliderDrawer"
//here is important part of implentation- you have to use Theme.AppCompat
//or use toolbar
android:theme="#style/Theme.AppCompat"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>