I want to put this little green line in the menu item that is active ... already has to create a shape and a custom style but no success ...
My activity_main_drawer.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">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_ordem"
android:checked="true"
android:title="Home"/>
<item
android:id="#+id/nav_auto_infracao"
android:title="Payment Profile" />
<item
android:id="#+id/nav_apreensao_deposito"
android:title="Payment history" />
<item
android:id="#+id/nav_doacao"
android:title="My card" />
<item
android:id="#+id/nav_sincronia"
android:title="Frinds" />
</group>
</menu>
Try to use View to draw line.
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#android:color/black" />
One way to solve this is you can highlight the item by calling the following method to detect when to change the visibility and position the view:
onNavigationItemSelected(navigationView.getMenu().getItem(0));
Since you've mentioned you already have a shape and a custom style, put the view (here in this code below, selectedItemBottomView) inside the NavigationView in activity_main or the XML layout file where you've used the activity_main_drawer like this:
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">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<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/activity_main_drawer" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:id="#+id/selectedItemBottomView"
android:layout_width="match_parent"
android:layout_height="10dp"
android:background="#color/green" />
</LinearLayout>
</RelativeLayout>
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
On selected, change the visibility or position as per your requirement:
selectedItemBottomView.setVisibility(View.VISIBLE);
Hope this helps.
Related
How to access a switch in a menu using ViewBinding ?
menu/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:checkableBehavior="single">
<item
android:id="#+id/nav_read"
android:title="#string/read"
android:icon="#drawable/ic_read"
/>
<item
android:id="#+id/nav_write"
android:icon="#drawable/ic_write"
android:title="#string/write"
/>
<item
android:id="#+id/nav_theme"
android:icon="#drawable/ic_theme"
android:title="Dark Theme"
app:actionLayout="#layout/theme_switch"
/>
</group>
<item android:title="#string/about">
<menu>
<item
android:id="#+id/nav_source_code"
android:icon="#drawable/ic_source_code"
android:title="#string/source_code"/>
</menu>
</item>
</menu>
theme_switch.xml:
<androidx.appcompat.widget.SwitchCompat xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toggleSwitch"
android:layout_width="fill_parent"
android:layout_height="match_parent" />
activity_home.xml :
<?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=".HomeActivity"
android:id="#+id/drawer_layout"
android:fitsSystemWindows="true"
tools:openDrawer="start"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
android:id="#+id/toolBar"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
/>
<FrameLayout
android:id="#+id/fl_home"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:id="#+id/nav_view"
app:headerLayout="#layout/nav_header"
app:menu="#menu/drawer_menu"
/>
</androidx.drawerlayout.widget.DrawerLayout>
These are my layout files.
In brief, I've created a DrawerLayout inside which I've implemented my menu using NavigationView. One of the menu item is a switch and I want to add a .setOnCheckedChangeListener to this switch.
I've tried accessing the switch like this inside the onCreate() function of my activity:
val switch = binding.navView.menu.findItem(R.id.nav_theme) as SwitchCompat
switch.setOnCheckedChangeListener { _, isChecked ->
}
However, it is throwing an exception that MenuItem cannot be cast into a SwitchCompat.
I don't see any otherway to access this switch.
If you want a switch on your item, use actionViewClass to define the component.
And if a not mistaken you can remove app:actionLayout="#layout/theme_switch"
app:actionViewClass="androidx.appcompat.widget.SwitchCompat"
I'm breaking my head wis an error.
Here is the code:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".ScanningChoice">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBarLayout3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.appbar.MaterialToolbar
android:id="#+id/app_bar"
style="#style/Widget.MaterialComponents.Toolbar.Primary"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/background_color"
app:navigationIcon="#drawable/ic_baseline_add_box_24"
app:title="Scanare"
app:titleTextColor="#FFFFFF"
app:menu="#menu/scan_specific_item"
/>
</com.google.android.material.appbar.AppBarLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/butonScanare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="64dp"
android:contentDescription="#string/scanare"
app:srcCompat="#drawable/ic_baseline_add_a_photo_24" />
<ListView
android:id="#+id/table_main"
android:layout_marginTop="56dp"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<?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/search"
android:icon="#drawable/ic_baseline_search_24"
android:title="#string/search"
android:contentDescription="#string/details_about_object"
app:showAsAction="always" />
</menu>
The issue seems to be with the app:menu="#menu/scan_specific_item"
When I remove it it looks like this:
but it lacks the search icon I desire to use.
I've been stuck with this problem for about 5 hours.
Any clue what's wrong?
I want to include a Menu with 2-3 fixed items, that will never change and below the menu I want to include a ListView that I can manage dynamically.
I want to do something like the following picture.
When I start the application, I always get the following error:
Process: com.pigmalionstudios.todolist, PID: 7055
java.lang.RuntimeException: Unable to start activity ComponentInfo{...}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class menu
The corresponding error line in MainActivity.java is:
setContentView(R.layout.activity_main);
Here's the activity_main.xml 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="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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="#layout/nav_header_main" />
<include layout="#menu/drawer_menu"/>
<ListView
android:id="#+id/list_slidermenu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:dividerHeight="1dp"
android:textColor="#424242"/>
</LinearLayout>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
And the 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"
tools:showIn="navigation_view"
android:id="#+id/activity_main_drawer"
android:layout_width="match_parent"
android:layout_height="#dimen/nav_header_height"
android:background="#drawable/side_nav_bar"
android:orientation="vertical">
<group android:checkableBehavior="single">
<item
android:id="#+id/add"
android:icon="#drawable/add"
android:title="#string/add" />
<item
android:id="#+id/ToDo"
android:icon="#drawable/pending"
android:title="To Dos" />
<item
android:id="#+id/settings"
android:icon="#drawable/settings"
android:title="Settings" />
</group>
</menu>
You do not really have to get a Menu in your navigation drawer for the desired layout. Just simply add a header view along with your ListView in the navigation drawer.
In my case, I just had to create a custom header for the ListView, then added the header along with the ListView in the navigation drawer. The process might look something like this.
LinearLayout nDrawerListHeader = (LinearLayout) inflater.inflate(R.layout.header_navigation_drawer, container, false);
mDrawerListView.addHeaderView(nDrawerListHeader);
The header_navigation_drawer.xml looked something like this.
<?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="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/navigation_header_color_white">
<!-- Some other layouts to get the precise view -->
</RelativeLayout>
</LinearLayout>
Then implement the action click by finding the right view inside the header view like this.
Button menuButton = nDrawerListHeader.findViewById(R.id.menu_button_1);
menuButton.setOnClickListener(...);
Hope that helps!
I have a Navigation Drawer that contains several items.
I added a button on the bottom of the Navigation Drawer.
The problem is that in small size screens the button covers the last item in the Drawer.
How can I add some space under the last item so the button won't cover anything?
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" >
<Button
android:id="#+id/remove_ads_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#color/red"
android:text="#string/remove_ads"
android:textColor="#color/white"
android:textSize="15sp" />
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
activity_main_drawer.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="none">
<item
android:id="#+id/action_contact_us"
android:icon="#drawable/ic_contact_us"
android:title="#string/action_contact_us" />
<item
android:id="#+id/action_share"
android:icon="#drawable/ic_share_24dp"
android:title="#string/action_share" />
<item
android:id="#+id/action_rate_us"
android:icon="#drawable/ic_rate_us_24dp"
android:title="#string/action_rate_us" />
<item
android:id="#+id/action_follow_us"
android:icon="#drawable/ic_follow_us_24dp"
android:title="#string/action_follow_us" />
<item
android:id="#+id/action_help"
android:icon="#drawable/ic_help_name"
android:title="#string/action_help" />
<item
android:id="#+id/action_about_us"
android:icon="#drawable/ic_info_24dp"
android:title="#string/action_about_us" />
<item
android:id="#+id/action_settings"
android:icon="#drawable/ic_settings_24dp"
android:title="#string/action_settings" />
</group>
</menu>
I came up with an idea how to do it and it worked for me.
What I needed to do is to add one more Item to the Drawer and then disable it.
The code is:
<item
android:title=""
android:enabled="false" >
</item>
Very simple and easy!!!
May be you can try something like this
<android.support.v4.widget.DrawerLayout 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"
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="#dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/remove_ads_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/red"
android:text="#string/remove_ads"
android:textColor="#color/white"
android:textSize="15sp" />
<ScrollView
android:id="#+id/my_scroll_view"
android:layout_above="#id/remove_ads_button"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="#dimen/padding_10dp">
<include layout="#layout/nav_header_main"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/action_contact_us"
android:textSize="#dimen/text_30sp"
android:drawableRight="#drawable/ic_contact_us"
android:drawablePadding="8dp"/>
<!--another menu items-->
</LinearLayout>
</ScrollView>
</RelativeLayout>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
I have a drawer layout with a navigation view and this is populated with menu items, i want to include an image at the bottom of the drawer below the menu items where there is blank space but having difficulty achieving this, i have tried to add imageview but it places this behind the drawer and not where i was hoping. Another idea i had was to use a footer like the header but this would not work for me! Thank you
<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
app:theme="#style/NavigationDrawerStyle"
android:id="#+id/nav_view"
android:layout_width="400dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:itemTextColor="#FFE300"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer"
android:background="#drawable/side_nav_bar"/>
</android.support.v4.widget.DrawerLayout>
layout
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/first_one"
android:icon="#drawable/ic_menu_book"
android:title="To Begin" />
<item
android:id="#+id/second_one"
android:icon="#drawable/nav"
android:title="Continue" />
<item
android:id="#+id/more_stories"
android:icon="#drawable/navtwo"
android:title="More Stories" />
<item
android:id="#+id/nav_manage"
android:icon="#drawable/rsz_win"
android:title="Help" />
<item
android:id="#+id/newone"
android:icon="#drawable/rsz_win"
android:title="add something here" />
</group>
<item android:title="Communicate">
<menu>
<item
android:id="#+id/nav_share"
android:icon="#drawable/shareicon"
android:title="Share"/>
<item
android:id="#+id/nav_send"
android:icon="#drawable/send"
android:title="Send" />
</menu>
</item>
</menu>
Try this
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="start"
>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="400dp"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
app:itemTextColor="#FFE300"
app:menu="#menu/test_menu"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#mipmap/ic_launcher"/>
</LinearLayout>
2 solutions
1. Add your ImageView in your layout as visibility = gone and change it to visible when needed .
See below :
<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
app:theme="#style/NavigationDrawerStyle"
android:id="#+id/nav_view"
android:layout_width="400dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:itemTextColor="#FFE300"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer"
android:background="#drawable/side_nav_bar"/>
<ImageView
android:id="#+id/ivLogo"
android:layout_width="100dp"
android:layout_marginTop="30dp"
android:visibility="gone"
android:layout_height="wrap_content"
android:src="#drawable/YOUR_IMAGE" />
</android.support.v4.widget.DrawerLayout>
in your java code :
Imageview ivLogo = (Imageview) findViewById(R.id.ivLogo);
ivLogo.setVisibility(View.VISIBLE);
2. Add the ImageView dynamically from your java code .
DrawerLayout drawer_layout = (DrawerLayout) findViewById(R.id.drawer_layout);
Imageview ivLogo = new Imageview(this);
// set your image, position, size...
drawer_layout.addview(ivLogo); // add the view
Hope it helps !