How to make navigation menu item look like button? - android

I have side menu in my app that contain regular menu items, but menu_button_enable_push has to look like button. Tried to use app:actionLayout, but it has no effect. Any ideas how can it be done?
menu.xml
<menu>
<item
android:id="#+id/menu_home"
android:icon="#drawable/ic_home"
android:checked="true"
android:title="Домой" />
<item
android:id="#+id/menu_settings"
android:icon="#drawable/ic_cog"
android:title="Настройки" />
<item
android:id="#+id/menu_info"
android:icon="#drawable/ic_question"
android:title="Информация" />
<item
android:id="#+id/menu_button_enable_push"
android:title="#string/button_enable_push"
app:actionLayout="#layout/menu_button"/>
</menu>
menu_button.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/activatedBackgroundIndicator"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="#fff" />

Related

Icons are not visible for menu in android studio

I use navigation drawer activity in my code. For certain menu, icons are visible. But for the rest it appears as a shaded square/circle. I tried clean/rebuild/invalidate cache and restart options, still no solution. Any help please...
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/fullstackdeveloper"
android:icon="#mipmap/ic_launcher_stack"
android:title="#string/home_label" />
<item
android:id="#+id/resourceperson"
android:icon="#mipmap/ic_launcher_person"
android:title="#string/about_us_label" />
<item
android:id="#+id/collegeworkshops"
android:icon="#mipmap/ic_launcher_workshop"
android:title="#string/portfolio_label" />
<item
android:id="#+id/tutorials"
android:icon="#mipmap/ic_launcher_tutorial"
android:title="#string/contacts_label" />
<item
android:id="#+id/tutorialvideos"
android:icon="#mipmap/ic_launcher_video"
android:title="#string/tutvideos_label" />
<item
android:id="#+id/scripts"
android:icon="#mipmap/ic_launcher_script"
android:title="#string/scripts_label" />
<item
android:id="#+id/androidapps"
android:icon="#mipmap/ic_launcher_android"
android:title="#string/androidapps_label" />
<item
android:id="#+id/analytics"
android:icon="#mipmap/ic_launcher_analytics"
android:title="#string/analytics_label" />
<item
android:id="#+id/python"
android:icon="#mipmap/pythonlogo"
android:title="#string/python_label" />
<item
android:id="#+id/rprogramming"
android:icon="#mipmap/rprogramming"
android:title="#string/rprogramming_label" />
<item
android:id="#+id/machinelearning"
android:icon="#mipmap/ic_launcher_machine"
android:title="#string/machinelearning_label" />
<item
android:id="#+id/booking"
android:icon="#mipmap/contacts"
android:title="#string/booking_label" />
</group>
Make sure you are following material design stander while creating icon for menu.
If you follow then it will show icons otherwise not.
You can confirm this by setting following icon
android:icon="#android:drawable/ic_menu_search"
You will see this icon in your drawer.
Above suggestion is 1 way to show icon in drawer other way is what #jeetparmar has suggest in above answer you can follow that also.
Try this way :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:tools="http://schemas.android.com/tools"
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/Home"
app:actionLayout="#layout/home_item"
tools:ignore="MenuTitle" />
</group>
==========home_item============
<RelativeLayout
android:layout_width="match_parent"
android:layout_centerInParent="true"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/home"
android:layout_width="wrap_content"
android:src="#mipmap/home"
android:layout_centerVertical="true"
android:layout_height="wrap_content">
</ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_centerVertical="true"
android:text="#string/HOME"
android:textColor="#color/colorblack"
android:layout_toRightOf="#+id/home"
android:layout_marginLeft="#dimen/_8sdp"
android:layout_height="wrap_content">
</TextView>
<ImageView
android:id="#+id/right"
android:alpha="0.3"
android:layout_width="#dimen/_20sdp"
android:src="#mipmap/right_arrow"
android:layout_margin="#dimen/_8sdp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:padding="#dimen/_4sdp"
android:layout_height="#dimen/_20sdp">
</ImageView>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_below="#+id/right"
android:layout_marginLeft="#dimen/_22sdp"
android:background="#c0c0c0"/>
</RelativeLayout>

How to increase the menuitem width in android while using Actionbar

I have a problem to increase the width of MenuItem in Action bar, it take the fixed width. Below is screen shot,
and want to achieve the below one,
Below is the code:
search_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:background="#drawable/search_view_background"
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content">
<TextView android:layout_width="0dp"
android:padding="10dp"
android:layout_weight="1"
android:text="Search Product"
android:textColor="#color/grey"
android:gravity="center_vertical"
android:drawableEnd="#drawable/ic_search"
android:drawableRight="#drawable/ic_search"
android:drawablePadding="20dp"
android:textSize="#dimen/text_size_14sp"
android:layout_height="match_parent"/>
</LinearLayout>
home_menu.xml
<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:onClick="onClickSearchButton"
android:orderInCategory="1"
android:title="#string/Search"
app:actionLayout="#layout/search_layout"
app:showAsAction="always"/>
<item
android:id="#+id/action_cart"
android:icon="#drawable/ic_menu_cart"
android:orderInCategory="2"
android:title="#string/action_cart"
app:showAsAction="always"/>
<item
android:id="#+id/overflow"
android:icon="#drawable/ic_menu_notifications"
android:orderInCategory="3"
android:title="#string/overFlowMenu"
android:visible="false"
app:showAsAction="always"/>
Please give your suggestion, how to fix this problem.
Thanks
Did it with a Hack.
I know it's not the best or even good solution but I did it with an hack.
searchItem.getActionView().findViewById(R.id.text).setMinimumWidth(width);
You can pass a particular width to the view so that it will take at-least that much space.

setting background and text color to topmost item in hamburger android

I want to set a black background with a white textview to the topmost item of the hamburger menu.It tried dynamically but setting Menu Item class does not have any propert setBackgroundDrawable etc.This is my menu and I want to set the background to navigation_item_1.pls help me
<?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:id="#+id/grp1" android:checkableBehavior="single" >
<item
android:id="#+id/navigation_item_1"
android:checked="true"
android:icon="#drawable/ic_home"
android:title="get i5 Pro" />
</group>
<group android:checkableBehavior="single">
<item
android:id="#+id/home"
android:icon="#drawable/ic_home"
android:title="Home"/>
<item
android:id="#+id/activate_pro"
android:icon="#drawable/ic_pro"
android:title="Active PRO"/>
<item
android:id="#+id/my_music"
android:icon="#drawable/ic_music_list"
android:title="My Music"/>
<item
android:id="#+id/nav_browse"
android:icon="#drawable/ic_brows_black"
android:title="Browse and Discover" />
<item
android:id="#+id/radio_stations"
android:icon="#drawable/ic_radio_station"
android:title="Radio Stations"/>
<item
android:id="#+id/new_releases"
android:icon="#drawable/ic_new_release"
android:title="New Releases"/>
<item
android:id="#+id/my_profile"
android:icon="#drawable/ic_profile"
android:title="My Profile" />
<item
android:id="#+id/music_lang"
android:icon="#drawable/ic_checked"
android:title="Music Languages" />
<item
android:id="#+id/settings"
android:icon="#drawable/ic_settings"
android:title="Settings"/>
<item
android:id="#+id/nav_login"
android:icon="#drawable/ic_logout"
android:title="Log in " />
<item
android:id="#+id/nav_log_out"
android:icon="#drawable/ic_logout"
android:title="Log out " />
</group>
</menu>
In xml file having navigation view add following code inside navigation view as :
<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:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:orientation="horizontal"
android:layout_marginTop="330dp">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:src="#drawable/ic_home"/>
<TextView
android:id="#+id/mSwitch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:textColor="#ffffff"
android:text="get i5 Pro" />
</LinearLayout>
</android.support.design.widget.NavigationView>
Where you have to give margin top of linear layout according to your header height.. e.g. If your header height is 320 dp then you need to increase is by 10 dp or more according to your requirement.

Android Relativelayout click background behaviour

So i have a RecyclerView and it is populated by my custom layout (code below). I have made a background drawable and set it on the root of my custom layout so that when user clicks the item, the background colour changes. There is also an options button (3 dots) on every item's layout, which is also clickable and also needs to change the background colour when clicked.
The problem is, when i set my drawable as background for the options menu the whole layout doesn't change colour when clicked (screens below)
When i set the background on options menu as "?android:attr/selectableItemBackground", everything works fine.
Code for my layout:
<RelativeLayout xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:background="#drawable/item_click"
android:layout_marginBottom="2dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:paddingLeft="16dp"
android:id="#+id/item_icon"
android:src="#drawable/ic_folder" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="72dp"
android:layout_centerVertical="true">
<TextView
android:id="#+id/item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#333333"
android:text="Caption"
android:textSize="13sp" />
<TextView
android:id="#+id/item_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#333333"
android:text="Description"
android:textSize="13sp" />
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:foregroundGravity="center_vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:id="#+id/item_menu"
android:src="#drawable/ic_ellipsis_v"
android:background="#drawable/item_click" />
</RelativeLayout>
Code for the drawable:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="#color/primary_100" />
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="#color/primary_100" />
<item
android:drawable="#android:color/white" />
</selector>
Item selected when the background of the options menu is set to "?android:attr/selectableItemBackground" (I want the whole row to change colour to blue):
Item selected when the background of the options menu is set to my drawable:
Change the default state of your selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="#color/primary_100" />
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="#color/primary_100" />
<item
android:drawable="#android:color/transparent" />
</selector>

How to implement the navigation drawer in android with sub item menu [duplicate]

This question already has answers here:
How to open sub menu after click on menu-item in Navigation drawer?
(3 answers)
Closed 7 years ago.
I am new in android and in my project I want to add the navigation drawer. I am selecting Navigation Drawer activity and create project in eclipse please help me how to develop navigation drawer below
Here is the image of navigation drawer for item and sub item menu
Use NavigationView
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/nav_header_main_navigation"
app:itemBackground="#drawable/nav_view_item_background"
app:itemTextColor="#color/nav_item_text_color"
app:menu="#menu/activity_main_navigation_drawer" />
activity_main_navigation_drawer code
<?xml version="1.0" encoding="utf-8"?>
<group android:checkableBehavior="single" android:id="#+id/root_group">
<item
android:id="#+id/item_home"
android:checked="true"
android:title="Home" />
<item android:title="You" android:id="#+id/you_parent_item">
<menu android:id="#+id/you_menu">
<group android:checkableBehavior="single" android:id="#+id/you_group">
<item
android:id="#+id/item_you_summary"
android:title="Summary Detail" />
<item
android:id="#+id/item_calendar"
android:title="Calendar" />
<item
android:id="#+id/item_request_timeout"
android:title="Request TimeOut" />
<item
android:id="#+id/item_cancel_requests"
android:title="Cancel Requests" />
<item
android:id="#+id/item_approval_responses"
android:title="Approval Responses" />
<item
android:id="#+id/item_transaction"
android:title="Transactions" />
<item
android:id="#+id/item_approval_request"
android:title="Approval Requests" />
<item
android:id="#+id/item_delegate"
android:title="Delegate" />
</group>
</menu >
</item>
<item android:title="Your People" android:id="#+id/your_parent_item">
<menu android:id="#+id/your_menu">
<group android:checkableBehavior="single" android:id="#+id/your_group">
<item
android:id="#+id/your_item_sumary"
android:title="View Summary" />
<item
android:id="#+id/item_record_timeout"
android:title="Record TimeOut" />
</group>
</menu>
</item>
<item android:title="Settings" android:id="#+id/setting_parent_item">
<menu android:id="#+id/setting_menu">
<group android:checkableBehavior="single" android:id="#+id/setting_group">
<item
android:id="#+id/item_log_out"
android:title="Log Out" />
</group>
</menu>
</item>
</group>
HeaderLayout if you want sent then
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="#dimen/nav_header_height"
android:background="#color/cws_orange"
android:gravity="bottom"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:theme="#style/Theme.AppTheme.AppBarOverlay">
<ImageView
android:id="#+id/imgAvatar"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="#android:drawable/sym_def_app_icon" />
<TextView
android:id="#+id/txtUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:text="Android Studio"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="#+id/txtUserEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android.studio#android.com" />

Categories

Resources