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.
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
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"
I need to add master on/off buttons in to sub menus in my action bar menus and small description under the title.Also I need to add heading for sub menus. I don't have idea how to do it. Do I have to use separate layout for each sub menu or else is there any way to fix using current menu.xml file?
This is what I want to do :
This is my menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/action_settings"
android:showAsAction="always"
android:title="#string/action_settings"
android:icon="#drawable/settings">
<menu >
<item
android:id="#+id/title"
android:title="HEADING"/>
<item
android:id="#+id/theme"
android:title="theme"
android:showAsAction="never"
android:orderInCategory="1"
android:icon="#drawable/ic_action2"
android:actionLayout="#layout/switch_layout"/>
<item
android:id="#+id/volunm"
android:title="Volumn"
android:orderInCategory="2"
android:showAsAction="never"
android:icon="#drawable/ic_action1"/>
</menu>
</item>
</menu>
This is my 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/switchForsetting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="on/off" />
</RelativeLayout>
But I'm not getting Switch button.
Use Action provider for this. It will be easier. here
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"/>