I'm having some trouble using custom layout as a menu item in a navigation drawer. When I use app:actionLayout just a title is shown.
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="start">
<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:background="#color/colorPrimary"
android:fitsSystemWindows="true"
app:headerLayout="#layout/drawer_menu_header"
app:itemIconTint="#color/colorGray"
app:itemTextColor="#color/colorGray"
app:menu="#menu/drawer_menu">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
drawer_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:showIn="navigation_view">
<group android:id="#+id/menu_grp1" android:checkableBehavior="single">
<item
android:id="#+id/menu_add_event"
android:icon="#drawable/menu_add"
android:title="#string/menu_add_event"/>
<item
android:id="#+id/menu_add_service"
android:icon="#drawable/menu_add"
android:title="#string/menu_add_service"/>
</group>
<group android:id="#+id/menu_grp2" android:checkableBehavior="single">
<item
android:id="#+id/menu_home"
android:icon="#drawable/menu_home"
android:title="#string/menu_home"/>
<item
android:id="#+id/menu_event"
android:icon="#drawable/menu_event"
android:title="#string/menu_event"/>
<item
android:id="#+id/menu_interesting"
android:icon="#drawable/menu_interesting"
android:title="#string/menu_interesting"/>
<item
android:id="#+id/menu_service"
android:icon="#drawable/menu_service"
android:title="#string/menu_service"/>
<item
android:id="#+id/menu_about"
android:icon="#drawable/menu_about"
android:title="#string/menu_about" />
<item
android:id="#+id/menu_advertising"
android:icon="#drawable/menu_advertising"
android:title="#string/menu_advertising" />
</group>
<group android:id="#+id/menu_grp3" android:checkableBehavior="single">
<item
android:id="#+id/menu_news"
android:icon="#drawable/menu_news"
android:title="#string/menu_news"/>
<item
android:id="#+id/menu_favorite"
android:icon="#drawable/menu_favorite"
android:title="#string/menu_favorite" />
<item
android:id="#+id/menu_visited"
android:icon="#drawable/menu_visited"
android:title="#string/menu_visited" />
<item
android:id="#+id/menu_logout"
android:icon="#drawable/menu_logout"
android:title="#string/menu_logout" />
</group>
<group android:id="#+id/menu_grp4" android:checkableBehavior="single">
<item
android:id="#+id/menu_social"
android:title="title"
app:actionLayout="#layout/drawer_menu_footer"
app:showAsAction="always"
android:visible="true"/>
</group>
</menu>
drawer_menu_footer.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:id="#+id/menu_facebook"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/menu_social_buttons_margin"
app:srcCompat="#drawable/menu_facebook" />
<ImageView
android:id="#+id/menu_instagram"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/menu_social_buttons_margin"
app:srcCompat="#drawable/menu_instagram" />
<ImageView
android:id="#+id/menu_p"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/menu_pinterest" />
</LinearLayout>
I solved my problem by replacing app:srcCompat with android:background in drawer_menu_footer.xml
Related
<?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"
tools:context=".MainActivity"
android:id="#+id/fragment_container">
<com.ismaeldivita.chipnavigation.ChipNavigationBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:id="#+id/bottom_nav_menu"
app:cnb_textColor ="#color/white"
app:cnb_unselectedColor="#color/black"`enter code here`
android:layout_marginBottom="5dp"
app:cnb_menuResource="#menu/bottom_navigation_menu_chef"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
How I can set the text color of the selected item using this library on any other way?
In menu item add app:cnb_textColor="#color/home_label"
As i see.. You can set the selected color changing the cnb_textColor in the bottom_navigation_menu_chef.
I will get you an example:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/home"
android:icon="#drawable/ic_home"
android:title="Home"
app:cnb_textColor="#android:color/holo_red_dark"/>
<item
android:id="#+id/activity"
android:icon="#drawable/ic_activity"
android:title="Activity"
app:cnb_textColor="#android:color/holo_green_dark"/>
<item
android:id="#+id/favorites"
android:icon="#drawable/ic_heart"
android:title="Favorites"
app:cnb_textColor="#android:color/black"/>
<item
android:id="#+id/settings"
android:icon="#drawable/ic_settings"
android:title="Settings"
app:cnb_textColor="#android:color/holo_blue_light" />
</menu>
curently done: menu working fine with selector color change.
i want: to add a border on selected menu item. please check image for result i want.
attched: 1. activitymain 2. selector 3. menu_nevigation
can i get border on selected item.
activityMain.xml
<?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"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Home"
android:textSize="50sp"
android:textStyle="bold"
android:layout_centerInParent="true" />
<View
android:layout_width="match_parent"
android:layout_height="3dp"
android:layout_above="#id/bottom_navigation"
android:background="#drawable/shadow"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/bottom_navigation"
app:itemBackground="#color/colorcustom"
app:itemTextColor="#drawable/selector"
app:itemIconTint="#drawable/selector"
app:menu="#menu/menu_navigation"
app:labelVisibilityMode="labeled"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="true"
android:color="#333333"
/>
<item
android:state_selected="false"
android:color="#a6a6a6"/>
</selector>
menu_navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/home"
android:title="Home"
android:icon="#drawable/ic_home"/>
<item
android:id="#+id/about"
android:title="About"
android:icon="#drawable/ic_about"/>
<item
android:id="#+id/test"
android:title="Test"
android:icon="#drawable/ic_test"/>
<item
android:id="#+id/term"
android:title="Term"
android:icon="#drawable/ic_term"/>
<item
android:id="#+id/setting"
android:title="Setting"
android:icon="#drawable/ic_setting"/>
</menu>
How do you underline menu items in navigation drawer?
expectation:
reality:
#menu/activity_navigation_drawer:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_open_account"
android:title="#string/na_navigate_open_account" />
<item
android:id="#+id/nav_login"
android:title="#string/na_navigate_login" />
<item
android:id="#+id/nav_stock_signals"
android:title="#string/na_navigate_stock_signals" />
...
</group>
navigation drawer activity layout:
<?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_navigation"
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"
android:background="#color/primary_asphalt_dark"
app:headerLayout="#layout/nav_header_navigation"
app:menu="#menu/activity_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
Tell me if some more code needed.
Each group ends with a line separator. Consequently, if each item in your menu has its own group you will achieve the desired graphical output:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:id="#+id/nav_open_account_group"
android:checkableBehavior="single">
<item
android:id="#+id/nav_open_account"
android:title="#string/na_navigate_open_account" />
</group>
<group android:id="#+id/nav_login_group"
android:checkableBehavior="single">
<item
android:id="#+id/nav_login"
android:title="#string/na_navigate_login" />
</group>
<group android:id="#+id/nav_stock_signals_group"
android:checkableBehavior="single">
<item
android:id="#+id/nav_stock_signals"
android:title="#string/na_navigate_stock_signals" />
</group>
...
</menu>
Note that the different ids for each group are required for this to work.
Press twice Shift
Open file design_navigation_menu_item.xml from com.android.support:design-26.1.0
Copy the file design_navigation_menu_item.xml into \app\src\main\res\layout to override it
Add android:paddingStart="#dimen/design_navigation_icon_padding" to CheckedTextView, make the file looks like this:
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<CheckedTextView
android:id="#+id/design_menu_item_text"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:drawablePadding="#dimen/design_navigation_icon_padding"
android:paddingStart="#dimen/design_navigation_icon_padding"
android:gravity="center_vertical|start"
android:maxLines="1"
android:textAppearance="#style/TextAppearance.AppCompat.Body2"/>
<ViewStub
android:id="#+id/design_menu_item_action_area_stub"
android:inflatedId="#+id/design_menu_item_action_area"
android:layout="#layout/design_menu_item_action_area"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
</merge>
In your activity_main.xml add to your NavigationView app:itemBackground="#drawable/drawer_item_background_selector", it should be like this:
<android.support.design.widget.NavigationView
...
app:itemBackground="#drawable/drawer_item_background_selector"
.../>
Create drawable/drawer_item_background_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/drawer_item_background_selected" android:state_pressed="true" />
<item android:drawable="#color/drawer_item_background_selected" android:state_checked="true" />
<item android:drawable="#color/drawer_item_background_selected" android:state_focused="true" />
<item android:drawable="#color/drawer_item_background_selected" android:state_activated="true" />
<item android:drawable="#drawable/drawer_item_background_default" />
</selector>
Create drawable/drawer_item_background_default.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/drawer_transparent" />
<stroke
android:width="1dp"
android:color="#color/appGrayLight" />
<padding
android:bottom="1dp"
android:left="-2dp"
android:right="-2dp"
android:top="-2dp" />
</shape>
In my app i want to change the navigation view menu background, but i dont know how to do this. All posts i have watched explains how to change each menu item background, but all i need is to change the entire background of the menu.
This is what i want to change, the entire white background:
activity_main_drawer.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item android:id="#+id/nav_camara" android:icon="#android:drawable/ic_menu_camera"
android:title="Import" />
<item android:id="#+id/nav_gallery" android:icon="#android:drawable/ic_menu_gallery"
android:title="Gallery" />
<item android:id="#+id/nav_slideshow" android:icon="#android:drawable/ic_menu_slideshow"
android:title="Slideshow" />
<item android:id="#+id/nav_manage" android:icon="#android:drawable/ic_menu_manage"
android:title="Tools" />
</group>
<item android:title="Communicate">
<menu>
<item android:id="#+id/nav_share" android:icon="#android:drawable/ic_menu_share"
android:title="Share" />
<item android:id="#+id/nav_send" android:icon="#android:drawable/ic_menu_send"
android:title="Send" />
</menu>
</item>
</menu>
Is there anyway to change the white background from the activity_main_drawer layout?
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="start">
<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" />
</android.support.v4.widget.DrawerLayout>
Thanks in advance.
You can set background to your NavigationView to change its color like below (set to app_white ) & for header set any color as a background of drawer_header_layout.
<android.support.design.widget.NavigationView
android:id="#+id/adcl_navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/app_white"
app:headerLayout="#layout/drawer_header_layout"
app:itemIconTint="#color/app_secondary_text"
app:itemTextColor="#color/app_secondary_text"
app:menu="#menu/menu_drawer"
app:theme="#style/ThemeOverlay.AppCompat.Light" />
just add android:background="#color/yourColor" to the <android.support.design.widget.NavigationView.
I'm designing a native navigation drawer in Android Studio.
I can't see the drawer in my preview because it is sitting left of the activity, out of range of the preview.
For now I'm using a testlayout.xml file to see my changes, but a lot of times I forget to copy paste them into the right activity.
Is there a way to preview the drawer layout?
If you want to be able to preview the drawer layout / navigation bar
If you are following the tutorial at https://developer.android.com/training/implementing-navigation/nav-drawer.html
You can put your listview for the nav drawer in a different xml file in the /layout folder, then include it in the drawer layout xml. Means you can edit it in the drawer_layout file and be able to see it properly
layout/drawer_layout
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/left_drawer"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#color/nav_drawer_background_color"/>
Then in the navbar
<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">
</FrameLayout>
<!-- The navigation drawer -->
<include
android:id="#+id/listView"
layout="#layout/drawer_layout"/>
</android.support.v4.widget.DrawerLayout>
To see NavigationView preview add tools namespace to your drawer layout:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
then add attribute tools:openDrawer equal to gravity of layout_gravity value of your Navigation view, for example
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:openDrawer="left">
<android.support.design.widget.NavigationView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left"
app:headerLayout="#layout/navigation_header_main"
app:itemTextAppearance="#style/NavigationItemStyleLight"
app:menu="#menu/navigation_main_menu"/>
</android.support.v4.widget.DrawerLayout>
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_camera"
android:icon="#drawable/ic_menu_camera"
android:title="Import" />
<item
android:id="#+id/nav_gallery"
android:icon="#drawable/ic_menu_gallery"
android:title="Gallery" />
<item
android:id="#+id/nav_slideshow"
android:icon="#drawable/ic_menu_slideshow"
android:title="Slideshow" />
<item
android:id="#+id/nav_manage"
android:icon="#drawable/ic_menu_manage"
android:title="Tools" />
</group>
<item android:title="Communicate">
<menu>
<item
android:id="#+id/nav_share"
android:icon="#drawable/ic_menu_share"
android:title="Share" />
<item
android:id="#+id/nav_send"
android:icon="#drawable/ic_menu_send"
android:title="Send" />
</menu>
</item>
Just Add
tools:showIn="navigation_view"
You can use NavigationView to get its preview on AndroidStudio, Its much better and easier to implement then the old NavigationDrawer.
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="start">
<include
layout="#layout/prof_app_bar"
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:itemTextColor="#color/text_head_color"
app:itemIconTint="#null"
app:headerLayout="#layout/nav_header_main2"
app:menu="#menu/professional_menu" />
</android.support.v4.widget.DrawerLayout>
Define the menu in navigationmenu.xml place it in menu resource folder:-
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single"
android:id="#+id/top_menu">
<item
android:id="#+id/pnav_setting"
android:icon="#drawable/accountsettings"
android:title="#string/paccount_settingss" />
<item
android:id="#+id/pnav_expertise"
android:icon="#drawable/professional"
android:title="#string/pexpertise" />
<item
android:id="#+id/pnav_comu"
android:icon="#drawable/communication"
android:title="#string/pcomunication" />
<item
android:id="#+id/pnav_change_pass"
android:icon="#drawable/changepasswd"
android:title="#string/pchange_pass" />
<item
android:id="#+id/pnav_change_lang"
android:icon="#drawable/changelang"
android:title="#string/pchange_lang"
android:visible="true"/>
</group>
<group android:checkableBehavior="single"
android:id="#+id/middle_menu">
<item
android:id="#+id/pnav_about_us"
android:icon="#drawable/aboutus"
android:title="#string/about_us" />
<item
android:id="#+id/pnav_feedback"
android:icon="#drawable/feedback"
android:title="#string/feedback"
android:visible="false"/>
<item
android:id="#+id/nav_privacy_policy"
android:icon="#drawable/privacy"
android:title="#string/privacy_policy" />
<item
android:id="#+id/pnav_faq"
android:icon="#drawable/faq"
android:visible="false"
android:title="#string/faq" />
<item
android:id="#+id/pnav_contact_us"
android:icon="#drawable/contactus"
android:title="#string/contact_us" />
</group>
<group android:checkableBehavior="single"
android:id="#+id/bottom_menu">
<item
android:id="#+id/pnav_sign_out"
android:icon="#drawable/signout"
android:title="#string/sign_out" />
</group>
</menu>
And, for getting click events use this:-
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(yourItemclickListener);