When using ActionbarCompat as Actionbar BackPort I am having the problem that action-icons do not show up - same code/res works with actionbarsherlock.
Am I doing something wrong or is this not yet supported? I am also missing the whole Menu/MenuItem getSupportMenuInflater() part that ABS has in ActionBar compat - can anyone shed some light on this?
This question was already answered in Actionbar not shown with AppCompat.
Add the following namespace to the "menu" item in your xml file
xmlns:compat="http://schemas.android.com/apk/res-auto"
Then change the "showAsAction" attribute to use the new namespace
compat:showAsAction="ifRoom"
Here's a full example with one item in the menu, with the changes on lines 2 and 6 (from Actionbar not shown with AppCompat)
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:compat="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_whatever"
android:icon="#drawable/ic_action_whatever"
android:title="#string/whatever"
compat:showAsAction="ifRoom" />
</menu>
When using the new ActionBarActivity, you no longer need getSupportMenuInflator. Your code should look like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
When calling invalidateOptionsMenu with ActionBarActivity you do need to use the new support version:
supportInvalidateOptionsMenu();
Those are the only two main differences between ActionBarSherlock and the new ActionBarActivity that I have found.
Related
I use this library (NavigationMaterialDrawer) for implementing drawer and managing accounts but when i add some action buttons to it,the action buttons only show in overflow menu even if considering always for showAsAction
as you see i click on overflow menu and i see this. but i want to see search icon in action bar
this is my code
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.test_menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
return super.onCreateOptionsMenu(menu);
}
init method:
public void init(Bundle savedInstanceState) {
View view = LayoutInflater.from(this).inflate(R.layout.custom_drawer,null);
setDrawerHeaderCustom(view);
this.addSection(newSection("Section 1", new FragmentIndex()));
this.addSection(newSection("Section 2",new FragmentIndex()));
this.addSection(newSection("Section 3", R.drawable.ic_mic_white_24dp,new FragmentButton()).setSectionColor(Color.parseColor("#9c27b0")));
this.addSection(newSection("Section",R.drawable.ic_hotel_grey600_24dp,new FragmentButton()).setSectionColor(Color.parseColor("#03a9f4")));
this.addBottomSection(newSection("Bottom Section",R.drawable.ic_settings_black_24dp,new Intent(this,Settings.class)));
getSupportActionBar().setDisplayShowTitleEnabled(false);
setTitle("TestTitle");
}
and this is my test_menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto"
xmlns:MaterialNavigationDrawer="http://schemas.android.com/tools">
<item android:id="#+id/action_search"
android:title="Search"
android:icon="#android:drawable/ic_search_category_default"
MaterialNavigationDrawer:showAsAction="always" />
</menu>
I import library to android studio because i guess for solving this issue i have to rewrite some class or xml but i don't know where. is it right?
UPDATE
I use sample of this library(Source) for test.
maybe it because of my theme!
I read this queston(Action bar buttons showing only in overflow (not extending ActionBarActivity)) and delete theme but the sample code not working without theme.it force closed on a line of library. i move my code to another activity of sample(account light) with another theme and i have the same issue. then i delete theme from this but it force closed too :
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.AppCompatDelegateImplBase.onCreate(AppCompatDelegateImplBase.java:124)
at android.support.v7.app.AppCompatDelegateImplV7.onCreate(AppCompatDelegateImplV7.java:146)
at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:59)
at it.neokree.materialnavigationdrawer.MaterialNavigationDrawer.onCreate(MaterialNavigationDrawer.java:361)
i want to use theme in future!so i can't delete theme entirely. wich element on theme must changed or deleted?
As i already mentioned in question problem is with theme!
in MaterialNavigationModule>res>values>themes.xml change parent of styles to
parent="Theme.AppCompat.NoActionBar
instead of
parent="Theme.AppCompat.Light.NoActionBar"
I want my refresh item to appear on the top bar. I am extending ListActivity and using the theme android:Theme.Holo.Light.DarkActionBar. The menu is created using:
<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="eu.pcas_project.client.android.pa.services.ServiceList">
<item
android:id="#+id/refresh_all_services"
android:title="#string/refresh_all_services"
android:icon="#drawable/ic_menu_refresh"
app:showAsAction="always" />
</menu>
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_service_list, menu);
return super.onCreateOptionsMenu(menu);
}
If I change app:showAsAction="always" on the menu XML to android:showAsAction="always", this works as I want it to—icon on top bar—but then I get an error saying Should use android:showAsAction when not using the appcompat library. Can it be fixed?
Targeting API 19.
It depends, if you hav a physical button for menu it doesn't appear. but you can try this:
<item
android:id="#+id/refresh_all_services"
android:icon="#drawable/ic_menu_refresh"
android:title="#string/refresh_all_services"
app:showAsAction="withText|ifRoom"
app:actionProviderClass="android.support.v7.widget.ActionProvider"
/>
As said in the question, android:showAsAction="always" works but generates an error.
While the project was not using the appcompat library directly, I discovered that a library imported by said project had com.android.support:appcompat-v7:XXX.YYY.ZZZ as a dependency even though it was not needed. This was added by Android Studio. Once the line was removed and the project rebuilt the error disappeared and the refresh icon appeared on the top right corner instead of on the menu.
In the new update Google has released a new API support library, that supports the ActionBar in API level 7+.
I used ActionBarSherlock until this update and I wrote the code to load the menu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main, menu);
return true;
}
and the menu file:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/item_menu_ok" android:icon="#drawable/ic_action_ok"
android:title="#string/ok" android:showAsAction="always"></item>
<item android:id="#+id/item_menu_cancel" android:icon="#drawable/ic_action_cancel"
android:title="#string/cancel" android:showAsAction="always"></item>
</menu>
To set up the menu buttons on the action bar. This code worked perfectly with ActionBarSherlock. But when I changed the action bar to the new support library, the buttons are not shown in the action bar. Even if they are set as android:showAsAction="always". And when I debug the code, the function menu.getSize() return 2, and that is correct, but no buttons are shown..
Why are the buttons not shown in the new support library?
Try pressing the MENU button on your device or emulator, and see if they appear in the overflow.
If they do, then the problem is that your <menu> XML needs to change. Menu XML that works with ActionBarSherlock and the native API Level 11+ action bar will not work with the AppCompat action bar backport.
Your menu XML would need to look like this:
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto"
>
<item android:id="#+id/item_menu_ok" android:icon="#drawable/ic_action_ok"
android:title="#string/ok" yourapp:showAsAction="always"></item>
<item android:id="#+id/item_menu_cancel" android:icon="#drawable/ic_action_cancel"
android:title="#string/cancel" yourapp:showAsAction="always"></item>
</menu>
And you would need to use the same yourapp prefix for anything else related to the action bar (e.g., yourapp:actionLayout).
You can see this covered in the action bar documentation.
I'd like to add a little to the answer.
If you want to see both text and an icon, please use withText in showAsAction
I've just tested it; when I used always or ifRoom without withText, I only saw an icon.
I have the following menu layout for my ActionBar:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/itemSearch"
android:icon="#drawable/actionbar_icon_search"
android:showAsAction="ifRoom|collapseActionView"
android:actionViewClass="android.widget.SearchView"
android:title="Search"/>
</menu>
And here's the setup code:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.actionbar_default, menu);
SearchView searchView=(SearchView) menu.findItem(R.id.itemSearch).getActionView();
int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
View searchEditText = searchView.findViewById(searchPlateId);
((TextView) searchEditText).setTextColor(Color.WHITE);
searchView.setOnCloseListener(new OnCloseListener() {...});
searchView.setOnQueryTextListener(new OnQueryTextListener() {...});
}
Everything is ok, except for one thing: on my Asus tablet (TF-201, Android 3.2.1) graphics are blurred:
If I remove android:actionViewClass="android.widget.SearchView", everything looks normal:
This problem is not reproduced on 4.1.2 emulator. I tried leaving only menu inflation code in my onCreateOptionsMenu() but that didn't help.
How do I fix this?
So I beat up the solution, however, it declares right compatibility only for API 14 and above, if not using ActionBarSherlock library.
First solution (API 14+)
Important is to be connected to Themed Context of application, otherwise you get lowest possible design for SearchView (blurry icons)
Creating SearchView programmatically
FRAGMENT
SearchView searchView =
new SearchView(getActivity().getActionBar().getThemedContext());
ACTIVITY
SearchView searchView = new SearchView(getActionBar().getThemedContext());
Second solution (ABS compatibility, API 8+)
ACTION BAR SHERLOCK FRAGMENT
SearchView searchView =
new SearchView(getSherlockActivity().getSupportActionBar().getThemedContext());
ACTION BAR SHERLOCK ACTIVITY
SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
Third solution (API 11+)
In some cases, setting SearchView.setBackgroundColor(int color) makes the icon appear less blurry, try Color.WHITE or Color.BLACK
Note that this changes background color for MenuItem whether collapsed or not, and eg. by using Theme Holo.Light.DarkActionBar you need to use right color, according to your ActionBar style.
I experienced same problem but other answer did not resolve my problem. I am extending my activity class from AppCompatActivity. My searchview icon was looking blurry on Android 4.* and it was looking good on Android 5.* without any problem.
I changed my xml to this:
<item android:id="#+id/action_search"
android:icon="#drawable/ic_search_white_24dp"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView"
android:iconifiedByDefault="true" />
And imported
import android.support.v7.widget.SearchView;
Instead of import android.widget.SearchView;
And problem fixed.
I'm currently working on a mp3 library on Android. Thing is, there are 4 tab, representing Songs, Albums, Artists and Playlists. My main activity creates a tabspec for each tab and add them to a tabhost.
The problem is, I can't use the ActionBar. I tried the tutorial from Android Developpers and it works fine, however when I try to apply it on my project it doesn't work. I can't see the actionbar and if I try a getActionBar() in my activity it returns null.
As in the tutorial I put this code in the activity :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.actionbar_menu, menu);
return true;
}
And I created this menu :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_save"
android:title="Hello !"
android:showAsAction="ifRoom" />
</menu>
I suppose there are more things to do in order to display the actionbar, yet I can't find an accurate on the Web since now ... Does this have something to do with the xml from my main activity ?
Thanks for your attention
For me I added this to my manifest:
android:theme="#android:style/Theme.Holo.Light.DarkActionBar" >
Hopefully that saves some time from having to look it up.
The action bar is only available from Android 3.0 and above, and you need to set your application theme to Holo.
Besides setting the sdk version, for instance:
<uses-sdk android:minSdkVersion="11" android:targetSdkVersion="15"/>
You also have to make the application theme use Holo.
make android:minSdkVersion="14" it works for me.