I am attempting to display icon with search bar in Android nav bar but it just keeps displaying just the icon. Here is my xml code
<item android:id="#+id/action_login"
android:title="Login"
android:icon="#android:drawable/ic_lock_lock"
app:showAsAction="always|withText" />
here the xml full code
<?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/action_login"
android:title="Login"
android:icon="#android:drawable/ic_lock_lock"
app:showAsAction="always|withText" />
</menu>
what could be wrong
Your code worked properly in Android 5.0 and above.
But in android 4.2 display only icon, not text!!
So if you worked on below 5.0 then use actionLayout.
Refer this answer:
How to get text on an ActionBar Icon?
Hope this will help you
Change this -:
android:icon="#android:drawable/ic_lock_lock"
To-:
android:icon="#drawable/ic_lock_lock"
or
android:icon="#mipmap/ic_lock_lock"
I have resolved this problem by following code:
1- Create menu in menu folder
<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:context=".MainActivity">
<item
android:id="#+id/action_sign"
android:title="Sing In"
app:showAsAction="always|withText"
app:actionLayout="#layout/menu_sign" />
2- app:actionLayout is the key word, Then create layout named menu_sign and put textView in it. the icon will displayed by drawableLeft
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:drawableLeft="#mipmap/ic_user_sign"
android:gravity="center"
android:orientation="vertical"
android:text="#string/text"
android:drawablePadding="#dimen/margin_5"
android:paddingLeft="#dimen/margin_10"
android:paddingRight="#dimen/margin_10"
android:textColor="#android:color/white" />
3- Add listener for your menu
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_sign, menu);
signItem = menu.findItem(R.id.action_sign);
signItem.getActionView().setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onOptionsItemSelected(signItem);
}
});
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'm creating an Android Action Bar using menu directory. While I was creating a new bar, I wanted to customize this action bar, so I added action-layout in one of items in xml file under menu directory. here is what I did
menu_main.xml(under menu directory)
<?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:orderInCategory="101"
android:id="#+id/menu_refresh"
android:title="refresh"
android:icon="#drawable/menu_refresh"
app:showAsAction="always"/>
<item
android:orderInCategory="102"
android:id="#+id/menu_search"
android:title="search"
app:showAsAction="always"
android:actionLayout="#layout/search_layout"/>
<item
android:orderInCategory="103"
android:id="#+id/menu_setting"
android:title="setting"
android:icon="#drawable/menu_settings"
app:showAsAction="always"/>
</menu>
in second item, i added actionLayout and search_layout.xml is just simeple LinearLayout
with TextView and EditText.
search_layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="search"
android:textSize="16dp"
android:textColor="#ffad8745"/>
<EditText
android:id="#+id/editText"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:background="#ffad8745"/>
</LinearLayout>
My expectation was that the first and third item show icons whereas second item shows me a layout that I defined, but here's what i got: only the title that I defined in menu_main.xml.
I eventually made an action bar that I wanted, but I still don`t quiet understand why the way initially tried does't work. please help me out. thank you
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 need to add master on/off (switch) as a sub menu in settings. I used separate layout xml and add it to menu.xml as android:actionLayout. But instead of switch it display a blank line. The switch is not displaying there. This is the code I used.
main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu"
android:orderInCategory="1"
android:showAsAction="never"
android:title="Menu"
/>
<item
android:id="#+id/myswtich"
android:showAsAction="never"
android:actionLayout="#layout/switch_layout"/>
</menu>
switch_layout.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/switchForActionBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</RelativeLayout>
Here if I changed as android:showAsAction="always" in menu.xml that switch displays in action bar. But what I want is to display it in sub menu. What is wrong with my code?
Action Layouts only exist on the Action Bar (i.e., items that are shown on the Action Bar such as via showAsAction="ifRoom" (assuming there is room) or showAsAction="always"). If can utilize checkable Menu Items for submenus that need to be toggleable.
I have an app, which have toggle button in action menu item, though i'm using Actionbar Sherlock, I don't know, how to place the toggle button in the action menu item. I don't want to place as a custom layout in action bar, but i want to place it as a Menu item. If anyone find solution, Please help me out.
Purpose, If I change the state of toggle button, it will sort the person based on ALphabets and again in Date of Birth.
Thanks in Advance!
Just add it like a normal Menu Button, check its state with a boolean variable, and you can change the icon and title when changing the sortmode
boolean birthSort=false;
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_toggle:
if(birthSort){
//change your view and sort it by Alphabet
item.setIcon(icon1)
item.setTitle(title1)
birthSort=false;
}else{
//change your view and sort it by Date of Birth
item.setIcon(icon2)
item.setTitle(title2)
birthSort=true;
}
return true;
}
return super.onOptionsItemSelected(item);
}
Don't forget to add it in xml like any other menu button and configure android:showAsAction if you want to show it in overflow or outside of it.
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_toogle"
android:showAsAction="ifRoom"
android:title="Share"
/>
</menu>
Other approach would be to use a custom layout for your ActionBar:
Basically you define a layout that contains your Toggle:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ToggleButton
android:id="#+id/actionbar_service_toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="Logging On"
android:textOff="Logging Off" />
</RelativeLayout>
ALTERNATIVE 1:
Then in your Activity or Fragment container you do:
ActionBar actionBar = getSupportActionBar();
actionBar.setCustomView(R.layout.actionbar_top);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_CUSTOM);
...
ToggleButton button = (ToggleButton) findViewById(R.id.actionbar_service_toggle);
Notice that you are having a real ToggleButton and you are handling it in code as a real object ToggleButton, which has lots of advantages compared to having you re-implement your own toggle (theme, reliability, views hierarchy, native support...).
Source code here.
ALTERNATIVE 2:
Another way to do it is embed your custom view into a regular menu view:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/myswitch"
android:title=""
android:showAsAction="always"
android:actionLayout="#layout/actionbar_service_toggle" />
</menu>
If like me the actionLayout isn't working for you, try app:actionLayout="#layout/actionbar_service_toggle" instead of android:actionLayout="#layout/actionbar_service_toggle" as well as app:showAsAction="always" instead of android:showAsAction="always"
This is because if you use appCompat the android namespace won't be used.
So here's the final version:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ToggleButton
android:id="#+id/actionbar_service_toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="Logging On"
android:textOff="Logging Off" />
</RelativeLayout>
and
<?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/myswitch"
android:title=""
app:showAsAction="always"
app:actionLayout="#layout/actionbar_service_toggle" />
</menu>
create xml file in menu:
menu.xml
<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="si.ziga.switchinsideab.MainActivity">
<item android:id="#+id/switchId" android:title="" android:showasaction="always" android:actionlayout="#layout/switch_layout">
<item android:id="#+id/action_settings" android:orderincategory="100" android:title="#string/action_settings" app:showasaction="never">
</item></item></menu>
And then navigate to your layout folder make a new xml file
and name it switch_layout.xml
Here's the code:
switch_layout.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/switchAB" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:layout_centervertical="true">
</switch></relativelayout>
In your MainActivity class copy and paste this code:
MainActivity.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
switchAB = (Switch)menu.findItem(R.id.switchId)
.getActionView().findViewById(R.id.switchAB);7
Or follow this link about this stuff
Here somthing easy
<?xml version="1.0" encoding="utf-8"?>
<item
android:title="#string/mode"
android:id="#+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:switchMinWidth="56dp"
android:layout_marginTop="120dp"
app:showAsAction="ifRoom"
app:actionViewClass="androidx.appcompat.widget.SwitchCompat"
android:checked="false"/>