In AppCompat library showasaction dosent work - android

ActionBar show only icon. How to get icon and text?
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/item1"
android:title="#string/add"
app:showAsAction="always|withText"
android:icon="#android:drawable/ic_input_add"
>
</item>
</menu>
My xml menu file.
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_add, menu);
return super.onCreateOptionsMenu(menu);
}
My code.

always|withText' will work if there is sufficient room, otherwise it will only place icon. You can test it on your phone with rotation.
if you want to show always text with icon follow the below step for work around
In Style.xml make sure parent is "Theme.AppCompat.Light.NoActionBar"
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>
Now Include Toolbar like in which layout you want to add menu action.
toolbar_with_add.xml
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:title="#string/app_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/darker_gray"
android:minHeight="?attr/actionBarSize">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:drawableRight="#android:drawable/ic_menu_add"
android:gravity="center"
android:text="Add" />
</android.support.v7.widget.Toolbar>
Make sure you include support library like fro toolbar like below in your build.gradle file.
compile 'com.android.support:appcompat-v7:23.0.1'
You can also change the drawable prosition by changing the "android:drawableRight" to "android:drawableLeft", "android:drawableBottom" ,"android:drawableTop"

Related

How to add an extra menu to android toolbar in Kotlin

The question is clear! I need to add another menu beside of the main menu in toolbar. I have created the status.xml menu file like this:
<?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/online"
android:orderInCategory="10"
android:title="#string/action_settings"
app:showAsAction="never" />
<item
android:id="#+id/busy"
android:orderInCategory="20"
android:title="#string/action_settings"
app:showAsAction="never" />
<item
android:id="#+id/invisible"
android:orderInCategory="30"
android:title="#string/action_settings"
app:showAsAction="never" />
</menu>
and added a clickable TextView to toolbar like this:
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
... />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.appcompat.widget.Toolbar>
Now I need to add it separately to the toolbar, but in code below:
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.main, menu)
menuInflater.inflate(R.menu.status, menu)
return true
}
It add all of items to main menu. Any help will be appreciated.
If this menus have to be separate that appear depending on some kind of condition you can use onPrepareOptionsMenu.
Check this question and developer guide on menus
Android: Multiple Option Menus in one Activity
https://developer.android.com/guide/topics/ui/menus

attempting to display icon with text android ActionBar

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);
}
});

v7 Toolbar OptionsMenu Item to far right - how to add margin

I am using the android.support.v7.widget.Toolbar in my activity.
I added it into my layout for the activity as following:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/add_contact_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#color/colorPrimary"
android:elevation="6dp"
android:title="#string/add_contact_toolbar_title"
android:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
</RelativeLayout>
I inflate the options menu in onCreateOptionsMenu of the activity and I
inflate my 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/add_contact_optionmenu_save"
android:title="#string/add_contact_optionmenu_save"
app:showAsAction="always"/>
</menu>
But unfortunately, the item appears way too far right and is too small:
How can I make it to appear like in all other apps? Bigger, and with more
right margin?
Cheers!
Well to move the MenuItem to a bit left from the right edge, you need to set paddingRight attribute to the android.support.v7.widget.Toolbar.
android:paddingRight="20dp"
To change the size, add the following style <item> to your App Theme.
<item name="actionMenuTextAppearance">#style/ActionBar.MenuTextStyle</item>
<item name="android:actionMenuTextAppearance">#style/ActionBar.MenuTextStyle</item>
<style name="ActionBar.MenuTextStyle"
parent="android:style/TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textSize">48dp</item>
</style>
All this being said, a better design pattern is simply to go ahead and follow the Material Design guidelines. I would suggest you have an icon for save rather than the text.

How to create a button in ActionBar with icon and text?

I want to create a button in ActionBar with text and icon. Something like on the screenshot below.
I've tried this code. But it doesn't work. It displays only icon without text.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/create"
android:title="#string/create"
android:icon="#drawable/ic_apply"
android:showAsAction="always|withText" />
</menu>
Read up on Toolbar
Since Toolbar is basically a ViewGroup, you can add child views. An example :
<Toolbar
android:id = "#+id/myToolbar"
android:layout_height = "wrap_content"
android:layout_width = "match_parent"
android:minHeight = "?attr/actionBarSize"
android:background = "?attr/colorPrimary" >
<Button
android:layout_height = "wrap_content"
android:layout_width = "match_parent"
android:text = "My button"
android:layout_gravity = "right" />
</Toolbar>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="ifRoom" />
</menu>
If you mentioned android:showAsAction="ifRoom"
it is considered as Action bar button.
Inflating Menu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
Handling Click event:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Assuming you are using AppCompat:
Your question maybe similar to: https://stackoverflow.com/a/18262141/950427
Make sure you bringing in the correct namespace: xmlns:yourapp="http://schemas.android.com/apk/res-auto".
Here is the example from the docs:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
yourapp:showAsAction="ifRoom" />
...
</menu>
Explanation from the docs:
Using XML attributes from the support library Notice that the
showAsAction attribute above uses a custom namespace defined in the
tag. This is necessary when using any XML attributes defined by
the support library, because these attributes do not exist in the
Android framework on older devices. So you must use your own namespace
as a prefix for all attributes defined by the support library.
Documentation: http://developer.android.com/guide/topics/ui/actionbar.html#ActionItems
You can create different xml layout for actionbar and in that use buttons or text.
Example:
actionbar.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Enter Text"
android:textColor="#ffffff"
android:textStyle="bold"
android:textSize="#dimen/actionbar_title"/>
</LinearLayout>
use actionbar.xml in java file:
DisplayMetrics metrics = getResources().getDisplayMetrics();
ActionBar actionbar = getActionBar();
Drawable d=getResources().getDrawable(R.drawable.action2);
getActionBar().setBackgroundDrawable(d);
getActionBar().setIcon(android.R.color.transparent);
TextView title;
getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getActionBar().setCustomView(R.layout.actiobar);
title= (TextView)findViewById(R.id.title);
title.setText("BUSINESS");

Is it possible to Implement Toggle Button in Action Menu Item using Actionbar sherlock in android

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"/>

Categories

Resources