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"
Related
First, I will explain to you what I want to do. Then, I will show you what I've already done. Finally, I will ask for your help about what I will implement.
My aim
My aim is to have a Toolbar like the Playstore:
When you click on the burger menu, it displays the menu.
When you click on the input, it displays changes the icon and it displays keyboard:
What I have for now
I followed the documentation (https://developer.android.com/training/appbar/ + https://developer.android.com/guide/topics/search/search-dialog + https://developer.android.com/training/search/setup). Thus, I have:
res/menu/action_bar_menu.xml containing my SearchView widget
<?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/searchView"
android:title="#string/search_title"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always" />
</menu>
A Toolbar in my fragment's layout:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorRoyalRed"
android:theme="#style/ToolbarStyle"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
The style ToolbarStyle (see above):
<style name="ToolbarStyle">
<item name="android:background">#color/colorRoyalRed</item>
<item name="android:textColorPrimary">#color/colorRichYellow</item>
<item name="actionButtonStyle">#style/ToolbarActionButton</item>
</style>
<style name="ToolbarActionButton">
<item name="android:padding">0dp</item>
<item name="android:paddingStart">0dp</item>
<item name="android:paddingEnd">0dp</item>
</style>
In the fragment class, I make use of the toolbar:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
Toolbar toolbar = view.findViewById(R.id.toolbar);
toolbar.setTitle("");
AppCompatActivity app_compat_activity = (AppCompatActivity) getActivity();
Objects.requireNonNull(app_compat_activity).setSupportActionBar(toolbar);
app_compat_activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
return view;
}
In the fragment class, I make use of the SearchView:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater menu_inflater) {
menu_inflater.inflate(R.menu.action_bar_menu, menu);
super.onCreateOptionsMenu(menu,menu_inflater);
AppCompatActivity app_compat_activity = (AppCompatActivity) getActivity();
SearchView search_view = (SearchView) menu.findItem(R.id.searchView).getActionView();
search_view.setIconifiedByDefault(false);
SearchManager search_manager = (SearchManager) Objects.requireNonNull(app_compat_activity).getSystemService(Context.SEARCH_SERVICE);
search_view.setSearchableInfo(Objects.requireNonNull(search_manager).getSearchableInfo(app_compat_activity.getComponentName()));
}
Well, there are other implementations (modifications concerning the manifest, etc.) but they are not important (note all the required changements to implement a simple search bar in the toolbar... more than 5 steps...).
What I would want to do next (help please!)
I don't want to use anything other than the menu items structure (thus I don't want to actually add the SearchView in the fragment's layout).
How could I change the SearchView's icon? For now it's a magnifying glass. It must be a burger menu. I tried to modify res/menu/action_bar_menu.xml by indicating a android:icon= to the SearchView but it didn't work. I also tried to use <item name="searchIcon">#drawable/ic_baseline_menu_24px</item> in the style ToolbarActionButton but it didn't work. Here the aim is to replace the glass by a burger menu icon.
How could I handle the click on the SearchView's burger menu icon (once it will be set)? Indeed, I will call my DrawerLayout::openDrawer function to open my menu.
How could I handle the click on the SearchView's content? Indeed, I will replace the burger menu by the left arrow (and, if clicked, keyboard will disapear and the burger menu will be displayed).
Am I using the wrong things?
I just followed Google's documentation. But it seems so complicated to me to realize that I may be on the wrong path? Should I stop using the menu items structure?
You may try SearchEditTextLayout available under this Google project -
https://android.googlesource.com/platform/packages/apps/Dialer/
These libraries may also help you to achieve your desired results.
SearchView and FloatingSearchView
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.
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.
If I define the following items for my action bar:
res/menu/action_menu.xml :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="label"/>
<item android:title="label1"/>
<item android:title="label2"/>
<item android:title="label3"/>
<item android:title="label4"/>
</menu>
In my Activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_menu, menu);
return true;
}
Is there anyway to allow me define certain items move to action overflow part ? and how to do it?
P.S. Action overflow part is the right-most part of action bar which hide certain items like a popup menu.
It's the other way round. You need to explicitly tell the menu which ones you want in the ActionBar and which not by setting the appropriate flags
E.g.
<item android:id="#+id/refresh"
android:title="#string/refresh"
android:icon="#drawable/reload_button"
android:showAsAction="always"/>
Here android:showAsAction tells how to handle it. Options are
always
ifRoom
never
withText
You can or options together with the pipe symbol as "always|withText"
See the android docs for action bar for more documentation.
To add something to Heiko's answer about the "overflow menu" on the action bar, this only happens if you have items set as ifRoom and there is no room for them to be displayed. On the overflow menu they only appear with a title and no icon.
On Android 4.0, the overflow menu ("3 dot spinner") is only shown on devices that don't have the physical "menu" button. You can test this on an ADV setting the option Hardware Back/Home keys option to "no".