Underline menu item in navigation drawer list - android

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>

Related

Why the texts inside the navigation drawer are not the same?

Can I know why the texts inside the navigation drawer are not the same?
<?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:context=".activities.MainActivity"
tools:openDrawer="start">
...
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/menu_navigation_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>
----------
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Other">
<menu>
<item
android:icon="#drawable/ic_star"
android:title="Rate the app" />
<item
android:icon="#drawable/ic_share"
android:title="Share the app" />
<item
android:icon="#drawable/ic_shield"
android:title="Privacy policy" />
<item
android:icon="#drawable/ic_document"
android:title="Terms of services" />
</menu>
</item>
</menu>
Notice some items have bold texts and some items have regular texts.
Why is that happening?
I'm using Material Design 3.
The solution
<style name="styleName" parent="TextAppearance.Material3.LabelLarge">
<item name="fontFamily">#font/font_regular</item>
</style>
Then add this inside NavigationView
app:itemTextAppearance="#style/styleName"

how to add top border?

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>

actionLayout not show in navigation drawer

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

How do I set my menu as active?

I have this menu:
Which I created like so:
menu_navigation.xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.ZZZZ.global.YYYY.XXXX">
<item
android:id="#+id/nearbyButton"
android:title="Nearby"
android:onClick="showNearbyPressed"
android:state_selected="true"
/>
<item
android:id="#+id/browseAtoZButton"
android:title="Browse A to Z"
android:onClick="showAtoZPressed"
android:state_selected="true"
/>
</menu>
And I place this in the activity XML like so:
<android.support.design.widget.BottomNavigationView
android:id="#+id/Navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#Ccc"
app:menu="#menu/menu_navigation"
/>
The problem is that if I click on "Browse A to Z", then the "Nearby" remains highlighted. " Browse A to Z" stays the same. What I was expecting was that the browse button will get the highlighted blue/purple color, and Nearby will become grey. But this does not happen.
How do I change this?
Is it perhaps a font thing and I have to manually change the font/color when you click on it?
I've tried:
navigation.performClick();
And:
navigation.setSelectedItemId(R.id.nearbyButton);
Any ideas as to what I am missing here?
First, remove android:state_selected="true" from your menu items. Update your menu_navigation.xml as below:
<?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"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.ZZZZ.global.YYYY.XXXX">
<item
android:id="#+id/nearbyButton"
android:title="Nearby"
android:onClick="showNearbyPressed" />
<item
android:id="#+id/browseAtoZButton"
android:title="Browse A to Z"
android:onClick="showAtoZPressed" />
</menu>
Handling items state
You need to provide a ColorStateList resource, and set this resource as background for the app:itemIconTint and app:itemTextColor attributes:
Update BottomNavigationView as below:
<android.support.design.widget.BottomNavigationView
android:id="#+id/Navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#Ccc"
app:menu="#menu/menu_navigation"
app:itemIconTint="#drawable/item_bg"
app:itemTextColor="#drawable/item_bg" />
Here is the item_bg.xml:
<!-- item_bg.xml -->
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/blue" android:state_checked="true" />
<item android:color="#color/white" />
</selector>
Hope this will help~

Resx Translation does not work for menues

I use the MvvmCross Resx Localization plugin. This works fine for all the layouts I have. But all the menues are untranslated. I have this xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto">
<group android:id="#+id/nav_items">
<item
android:id="#+id/nav_accounts"
android:icon="#drawable/ic_account_balance"
local:MvxLang="Text AccountsLabel" />
<item
android:id="#+id/nav_statistics"
android:icon="#drawable/ic_show_chart"
local:MvxLang="Text StatisticsLabel" />
</group>
<group android:id="#+id/nav_footer">
<item
android:id="#+id/nav_categories"
android:icon="#drawable/ic_label_outline"
local:MvxLang="Text CategoriesLabel" />
<item
android:id="#+id/nav_backup"
android:icon="#drawable/ic_backup"
local:MvxLang="Text BackupLabel" />
<item
android:id="#+id/nav_settings"
android:icon="#drawable/ic_settings"
local:MvxLang="Text BackupLabel" />
<item
android:id="#+id/nav_about"
android:icon="#drawable/ic_info"
local:MvxLang="Text AboutLabel" />
</group>
</menu>
And I include it here:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.NavigationView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="#+id/navigation_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:theme="#style/AppTheme.Menu"
local:headerLayout="#layout/fragment_navigation_header"
local:menu="#menu/navigation_drawer" />
Also I have the viewmodel with the TextSource definition:
public IMvxLanguageBinder TextSource => new MvxLanguageBinder("", GetType().Name);
I made a constructor and resolved the label I want. For example "AboutLabel". This worked without any issues.
Is there a fix that the menu works as well ?

Categories

Resources