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.
Related
Hi
I'm using Navgation Drawer.
I want to change from text to text and icon at overflow menu items.
So i used 'android:icon' and 'android:title' in menu.xml. But Anything is not displayed.
To be exact, I could only see text.
and
I used android:actionLayout="#layout/overflow_menu_item". In overflow_menu_title, Text and Icon are defined.
But It dosen't work, too
I don't no why it dosen't work.
How to set Text and icon in overflow menu item?
I searched a lot but it didn't help.
plz help me...
Here is Code
overflow_menu.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=".MovieList">
<item
android:id="#+id/menu_movie"
android:orderInCategory="150"
android:icon="#drawable/ic_search_black_24dp"
android:title="setting1"
android:actionLayout="#layout/overflow_menu_title"
app:showAsAction="never" />
<item
android:id="#+id/menu_gallery"
android:orderInCategory="150"
android:title="setting2"
android:icon="#drawable/ic_search_black_24dp"
app:showAsAction="never" />
<item
android:id="#+id/settings3"
android:orderInCategory="150"
android:icon="#drawable/ic_search_black_24dp"
android:title="setting2"
app:showAsAction="never" />
</menu>
overflow_menu_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center_vertical"
android:padding="2dp"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimaryDark">
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:src="#drawable/ic_15"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MOVIE"
android:textSize="24dp"/>
</LinearLayout>
You can't create a custom layout for menu item using android:actionLayout for items that are not on the action bar.
In other meaning, you can't set android:actionLayout for non-action items that are in the overflow menu items.
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>
I want to put a switch button into MenuItem with showAsAction="never" but it's not working. Here is my layout
<?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.trietnhm.multipleactivities.MainActivity"
>
<item
android:id="#+id/action_about"
android:orderInCategory="0"
app:showAsAction="never"
android:title="About"/>
<item
android:id="#+id/action_change_theme"
android:orderInCategory="3"
app:actionLayout="#layout/switcher"
app:showAsAction="never"
android:title="" />
<item
android:id="#+id/action_change_delimeter"
android:orderInCategory="2"
app:showAsAction="never"
android:title="Change Delimeter"/>
</menu>
This my Switch button
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v7.widget.SwitchCompat
android:id="#+id/switchForActionBar"
android:layout_width="70dp"
android:layout_height="50dp"
android:scaleX="1.3"
android:scaleY="1.3"
android:switchMinWidth="45dp"
android:paddingTop="6dp" />
</RelativeLayout>
When I run the app and it's not show anything. Here the image https://drive.google.com/file/d/0B5zsq6J-qyY5bF8tYnBKVy11OHM/view?usp=sharing
Any help I would be appreciated. Thanks all.
android:showAsAction
never :
Never place this item in the app bar. Instead, list the item in the
app bar's overflow menu.
Of course, It won't show up. Perhaps you should use other options to show, always or ifRoom.
This line code
android:orientation="vertical"
is used with LinearLayout not with RelativeLayout
why are you using app:showAsAction="Never"?? for that you can also use app:showAsAction="ifRoom" or app:showAsAction="ifRoom|collapseActionView"
Despite there being enough space in the action bar , only the icon is being displayed, even when I am using
app:showAsAction= "always|withText"
Full code:
<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.ishaan.admin.notify.Profile">
<item android:id="#+id/action_logout"
android:title="Logout"
android:icon="#drawable/logout_icon"
app:showAsAction= "always|withText" />
</menu>
It is known thing on the Android 4.0 and discussed ealier
Your can find solution here:
android 4.0, text on the action bar NEVER shows
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/toolbar_right_button"
android:orderInCategory="1"
android:title="Text"
app:actionLayout="#layout/menu_item_1"
app:showAsAction="ifRoom|withText"/>
</menu>
and in Layout create Layout File menu_item_1.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/logout_button"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableRight="#drawable/ic_toolbar_locked"
android:gravity="center_vertical"
android:text="Text"
android:textColor="#color/PrimaryColour"/>
</RelativeLayout>
I have simple requirement to display some text in the actionBar near the menu. How can I acheive it?
What it looks like now?
What I need?
Try this ActionBar menu sample:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourApp="http://schemas.android.com/apk/res-auto"
>
... ...
<item
android:id="#+id/item_text"
yourApp:showAsAction="always"
yourApp:actionLayout="#layout/action_text"
/>
</menu>
Then you need a layout action_text for your TextView:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical"
>
... ...
<TextView android:id="#+id/lb_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/your_text"
android:textSize="10sp"
android:textColor="#color/white"
/>
</RelativeLayout>
Hope this help!
<item
android:id="#+id/action_refresh"
android:icon="#android:drawable/ic_input_add"
android:title="Options"
app:showAsAction="withText|always" />
<item
android:id="#+id/action_options_unused"
app:showAsAction="withText|always"
android:title="Options"
/>
Don't set a android:icon if you don't want an icon appearing at the left side of your textual item. Also make sure this <item> is the last one in your menu's XML file.