MY activity_home_drawer.xml
activity_home_drawer.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">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_home"
android:icon="#drawable/ic_menu_camera"
android:title="#string/menu_home" />
<item
android:id="#+id/nav_gallery"
android:icon="#drawable/ic_menu_gallery"
android:title="#string/menu_gallery" />
<item
android:id="#+id/nav_slideshow"
android:icon="#drawable/ic_menu_slideshow"
android:title="#string/menu_slideshow" />
<item
android:id="#+id/nav_tools"
android:icon="#drawable/ic_menu_manage"
android:title="#string/menu_tools" />
</group>
<item android:title="Communicate">
<menu>
<item
android:id="#+id/nav_share"
android:icon="#drawable/ic_menu_share"
android:title="#string/menu_share" />
<item
android:id="#+id/nav_send"
android:icon="#drawable/ic_menu_send"
android:title="#string/menu_send" />
</menu>
</item>
</menu>
I was following a tutorial in YouTube about how to create an App Navigation drawer for my android app, while following the procedures, that occurred (see photo above), I'm new to android programming so, can anybody please tell me what's the root cause of this problem and how to fix it? Thanks.
remove this line
tools:showIn="navigation_view"
If we look into the official documentation, it clearly states that the tools:showIn is,
Intended for: Any root <View> in a layout that's referred to by an
<include>
Basically, tools:showIn lets Android Studio's layout editor to know where the View that uses it should be placed.
From the above explanation we can figure out that tools:showIn,
should be used by a Root <View>
the passing layout should refer this view by an <include>
Now, if you look into your source code, you will observe that,
You are using tools:showIn in your menu which is not it is intended for
You did not <include> the activity_home_drawer.xml in your navigation_view layout. Though you will not be able to do so, as it's not a <View>.
For the above mention reason, Android Studio is not able to preview the menu. The workaround to fix this issue is to remove the tools:showIn attribute and the tools namespace as well.
Remove
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view"
After refactoring your activity_home_drawer.xml menu should look like,
<?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_home"
android:icon="#drawable/ic_menu_camera"
android:title="#string/menu_home" />
<item
android:id="#+id/nav_gallery"
android:icon="#drawable/ic_menu_gallery"
android:title="#string/menu_gallery" />
<item
android:id="#+id/nav_slideshow"
android:icon="#drawable/ic_menu_slideshow"
android:title="#string/menu_slideshow" />
<item
android:id="#+id/nav_tools"
android:icon="#drawable/ic_menu_manage"
android:title="#string/menu_tools" />
</group>
<item android:title="Communicate">
<menu>
<item
android:id="#+id/nav_share"
android:icon="#drawable/ic_menu_share"
android:title="#string/menu_share" />
<item
android:id="#+id/nav_send"
android:icon="#drawable/ic_menu_send"
android:title="#string/menu_send" />
</menu>
</item>
</menu>
Related
So as I researched the only way to prevent your android app to accept system current set fonts is to use "dp" instead of "sp" as explained here. So it did worked but not really. Since menu layout for drawer-view is a little different from normal layouts and there is not even attribute textSize i do not really know how to do that.
Here is the code
Drawer-view
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group
android:id="#+id/activities"
android:checkableBehavior="single">
<item
android:id="#+id/start"
android:icon="#drawable/ic_date_range_blue_24dp"
android:title="#string/Start" />
<item
android:id="#+id/newReview"
android:icon="#drawable/ic_add_circle_blue_24dp"
android:title="#string/newReview" />
<item
android:id="#+id/myReviews"
android:icon="#drawable/ic_date_range_blue_24dp"
android:title="#string/myReviews" />
<item
android:id="#+id/profile"
android:icon="#drawable/ic_account_circle_blue_24dp"
android:title="#string/profile" />
<item
android:id="#+id/messages"
android:icon="#drawable/ic_message_blue_24dp"
android:title="#string/messages" />
<item
android:id="#+id/feedback"
android:icon="#drawable/ic_star_half_green_24dp"
android:title="#string/feedback" />
</group>
<group android:id="#+id/exit_group">
<item
android:id="#+id/exit"
android:icon="#drawable/ic_exit_to_app_blue_24dp"
android:title="Изход">
</item>
</group>
<group android:id="#+id/application">
<item
android:id="#+id/about"
android:title="#string/about" />
<item
android:id="#+id/terms"
android:title="#string/terms" />
</group>
<group android:id="#+id/rights">
<item
android:id="#+id/right"
android:title="#string/rights" />
</group>
</menu>
As you can see drawer-view font-size is much bigger than the normal-layout
this code inside the styles.xml
<style name="menu_text_style" parent="#android:style/TextAppearance.DeviceDefault.Widget.ActionBar.Menu">
<item name="android:textSize">16dp</item>
<item name="android:textColor">#0D2142</item>
<item name="android:textAllCaps">false</item>
</style>
and add on navigationView
app:itemTextAppearance="#style/menu_text_style"
I have the following menu ressource file (generated with androids Navigation Drawer Activity) and customized by me:
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_home"
android:icon="#drawable/ic_home_black_24dp"/>
<item
android:id="#+id/nav_users"
android:icon="#drawable/ic_menu_camera" />
<item
android:id="#+id/nav_following"
android:icon="#drawable/ic_people_black_24dp" />
</group>
<item android:title="Foo">
<menu>
<item
android:id="#+id/nav_logout"
android:icon="#drawable/ic_exit_to_app_black_24dp" />
<item
android:id="#+id/nav_licences"
android:icon="#drawable/ic_assignment_black_24dp" />
<item
android:id="#+id/nav_about"
android:icon="#drawable/ic_info_black_24dp" />
</menu>
</item>
Setting the first level items works as expected:
navigationView.setCheckedItem(R.id.nav_home);
But setting a child menu item as checked, doesn't do anything.
navigationView.setCheckedItem(R.id.nav_about);
Any idea?
The problem was the generated layout by Android Studio. To check child items, I had to adjust the layout like this:
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_home"
android:icon="#drawable/ic_home_black_24dp"/>
<item
android:id="#+id/nav_users"
android:icon="#drawable/ic_menu_camera" />
<item
android:id="#+id/nav_following"
android:icon="#drawable/ic_people_black_24dp" />
</group>
<item android:title="Foo">
<menu>
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_logout"
android:icon="#drawable/ic_exit_to_app_black_24dp" />
<item
android:id="#+id/nav_licences"
android:icon="#drawable/ic_assignment_black_24dp" />
<item
android:id="#+id/nav_about"
android:icon="#drawable/ic_info_black_24dp" />
</group>
</menu>
</item>
Its because they are not under group tag , and they dont have property of android:checkableBehavior="single" .
Modify you code as
<item android:title="Foo">
<menu>
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_logout"
android:icon="#drawable/ic_exit_to_app_black_24dp" />
<item
android:id="#+id/nav_licences"
android:icon="#drawable/ic_assignment_black_24dp" />
<item
android:id="#+id/nav_about"
android:icon="#drawable/ic_info_black_24dp" />
</group>
</menu>
Hope this helps.
If you are trying to select an Item of the Navigation drawer in the onCreate Method or anywhere else in your application, you can use the code below:
navigationView.getMenu().getItem(0).setChecked(true);
The getItem(int index) method gets the MenuItem then you can call the setChecked(true);on that MenuItem, all you are left to do is to find out which element index does the default have, and replace the 0 with that index.
You can select(highlight) the item by calling
onNavigationItemSelected(navigationView.getMenu().getItem(0));
You can refer to this as well :
NavigationView with DrawerLayout setCheckedItem not working
Also you can add the menu as:
<group
android:checkableBehavior="single">
<item
android:id="#+id/nav_home"
android:icon="#drawable/ic_home_black_24dp"/>
<item
android:id="#+id/nav_users"
android:icon="#drawable/ic_menu_camera" />
<item
android:id="#+id/nav_following"
android:icon="#drawable/ic_people_black_24dp" />
</group>
<group
android:checkableBehavior="single">
<item
android:id="#+id/nav_logout"
android:icon="#drawable/ic_exit_to_app_black_24dp" />
<item
android:id="#+id/nav_licences"
android:icon="#drawable/ic_assignment_black_24dp" />
<item
android:id="#+id/nav_about"
android:icon="#drawable/ic_info_black_24dp" />
</group>
Try this solution...
Use two line code, one is for checked Item of navigation drawer and second is for selection of item of sub-menu.
navigationView.setCheckedItem(R.id.nav_about);
onNavigationItemSelected(navigationView.getMenu().getItem(3).getSubMenu().getItem(2));
If you are creating your menu programatically you will have to do something similar to #Abdul Kawee's answer
Menu.setGroupCheckable(...)
I want to change default Images of Navigation Drawer in Android.When I'm changing it's not working any one help me please.Thanks stack over flow.how can I do this help me please.
<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>
I hope you need some pre defined vector drawables as one of your comments was pointing to it.
Try
Right Click drawable folder
New -> Vector Asset
Enable Material Icon RadioButton (enabled by default)
Click Choose button
Select one image. change Resource name if required & click Next -> Finish.
Now a new vector drawable will be created in drawable folder & you can refer it using #drawable.
Here is my .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_home"
android:icon="#drawable/ic_menu_home"
android:title="section1"
android:checked="true"/>
<item
android:id="#+id/nav_add_event"
android:icon="#drawable/ic_menu_addevent"
android:title="section2" />
<item
android:id="#+id/nav_managenews"
android:icon="#drawable/ic_menu_manage_news"
android:title="section3" />
<item
android:id="#+id/nav_leaderboard"
android:icon="#drawable/ic_menu_leadre_board"
android:title="section3"/>
<item
android:id="#+id/nav_mypoints"
android:icon="#drawable/ic_menu_mypoint"
android:title="section4" />
<item
android:id="#+id/nav_myprofile"
android:icon="#drawable/ic_menu_my_profile"
android:title="section5" />
<item
android:id="#+id/nav_change_password"
android:icon="#drawable/ic_change_password"
android:title="section6"/>
</group>
</menu>
Im trying to add a switch (checkbox as second option) to the navigation drawer. The "slide in menu". The default one you'll get when creating a new project with navigation drawer.
I've tried on a fresh new project so I dont mess up my 'real' project.
I tried this from SO
But without any luck. Cant seem to find anything else worth mentioning..
Im trying to add the switch at the last menuItem. 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_camera"
android:icon="#drawable/ic_menu_camera"
android:title="Import"
android:checkable="true"/>
<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" />
<item
android:id="#+id/myswitch"
android:title=""
android:actionLayout="#layout/ttt"
/>
</menu>
</item>
</menu>
ttt.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Switch
android:id="#+id/ss"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</RelativeLayout>
The last item "id/myswitch" doens't show at all.
The MainActivity.java is 100% default. Thats why I dont post it.
Instead of:
<item
android:id="#+id/myswitch"
android:title=""
android:actionLayout="#layout/ttt"
/>
write:
<item
android:id="#+id/myswitch"
android:title=""
app:actionLayout="#layout/ttt"
/>
Change android:actionLayout to app:actionLayout.
In my app i have a nvigation view with some menu items. I want to align some of them to the center of the view. How can i achieve this correctly?
This is what i want:
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>
Thanks in advance.
As far as I know this is not possible in easy way.
You could find or navigate through views hierarchy to find views or textviews you want to center.
But i think NavigationView is not designed to align things other than left or right