Is it possible to get the SearchView in the ActionBar by just having the Toolbar object? .
I know that you can get it through the onCreateOptionsMenu(), but in this case I am just trying to see if via a toolbar.findViewById I can get it
Yes.It possible to show the SearchView.For this you need to add menu feature such as :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
default:
return super.onOptionsItemSelected(menuItem);
}
}
You need to implement the below code into menu_main menu(it must be stay into menu folder).
<?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/search"
android:icon="#drawable/abc_ic_search_api_mtrl_alpha"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always|collapseActionView"/></menu>
Hope it can help to you thanks.
Related
I have a requirement that action Overflow menu icon should show in fragment, but not activity.
Code:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.filter:
openFilterDialog();
return true;
default:
break;
}
return false;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_mail_list, menu);
return true;
}
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="com.dwisehealthcare.pmstablet.consult.activity.ConsultActivity">
<item
android:id="#+id/filter"
android:title="Filter"
android:icon="#drawable/ic_baseline_filter_list_24"
app:showAsAction="always" />
</menu>
I searched, but everyone is talking about menu item, not action icon.
Try this link may be it will help you, with the help of this you can add menu anywhere you want.Just add an ImageView to that fragment and when you click that imageView you create a PopUpMenu
I want to add an action to the action bar, but it appears in my action bar as a drop down list.
How can I add button to the action bar?
My code is:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="Add"
showAsAction="ifRoom"/>
</menu>
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
If you are using android.app.Activity simply change showAsAction="always" to "android:showAsAction="always".
If you are using android.support.v7.app.Activity change the code as follows:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="Add"
app:showAsAction="ifRoom"/>
</menu>
You just simply add the one more item into menu and inflate that menu into your activity ....
like below you will add your button and access that button into your activity...
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/refresh"
android:icon="#android:drawable/stat_notify_sync"
showAsAction="ifRoom"/>
<item
android:id="#+id/action_bar_button_cancel"
android:focusableInTouchMode="true"
android:icon="#android:drawable/ic_menu_close_clear_cancel"
showAsAction="ifRoom"/>
</menu>
Inflating the menu like below.....
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
Accessing method in action bar item is....
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.refresh:
//logic
return true;
case R.id.action_bar_button_cancel:
//logic
return true;
default:
return super.onOptionsItemSelected(item);
}
}
that's if you have any doubt comment
I create base activity with actionbar menu functional and extended another activities from it.
public class BaseActivity extends SherlockFragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
...
return true;
}
}
Now I want to add some buttons to the actionbar in some activities. How can i add elements to actionbar and use element from base activity?
You can do that as simple as in BaseActivity just don't forget to call super.onCreateOptionsMenu().
main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_help"
android:icon="#drawable/ic_action_help"
android:title="#string/action_help" />
</menu>
home.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_new"
android:icon="#drawable/ic_action_new"
android:title="#string/action_new" />
</menu>
In BaseActivity you are using base menu config main.xml.
public class BaseActivity extends SherlockFragmentActivity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
In child activity you are using another config from home.xml only with additional menu items, no duplicates. But don't forget to call super.onCreateOptionsMenu(menu) with the same menu instance to add elements from BaseActivity (parent menu items would be added after child menu items if you call super's method after inflate, and before otherwise).
public class HomeActivity extends BaseActivity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.home, menu);
return super.onCreateOptionsMenu(menu);
}
}
You can specify your actions for the actionbar in the main.xml
example :
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"/>
<item android:id="#+id/action_compose"
android:icon="#drawable/ic_action_compose"
android:title="#string/action_compose" />
And call from your subclass
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
Make different xml files containing the buttons you need specific to those Activity. Say for Activity1 you have two buttons and for Activity2 you have one, then you will create 2 xml files like below.
action_activity1.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"/>
<item android:id="#+id/action_compose"
android:icon="#drawable/ic_action_compose"
android:title="#string/action_compose" />
</menu>
action_activity2.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"/>
</menu>
Then in the onCreateOptionsMenu(Menu menu) method inflate the desired xml file. Like,
Activity1:
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.action_activity1, menu);
return true;
}
Activity2:
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.action_activity2, menu);
return true;
}
Please be care full with the syntax (as I used appcompat actionbar). :)
This is what I use. Hope you find it helpful. And, I will be happy to see an easier way than this. :)
I have options menu item in my application. Requirement was to add a toggle button to a menu item. Is this possible?
As of this writing there are 3 options.
1) Use app:actionViewClass. Example:
<?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:title="Switch!"
app:actionViewClass="android.widget.Switch"
app:showAsAction="always" />
</menu>
2) You can use a custom layout in a menu item to add toggle button. Example:
Create a layout with Switch (alternatively, you may also use ToggleButton), res/layout/menu_switch.xml:
<?xml version="1.0" encoding="utf-8"?>
<Switch xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:padding="64dp" />
And use that layout in menu item:
<?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:title="#string/switch_button_title"
app:actionLayout="#layout/menu_switch"
app:showAsAction="always" />
</menu>
3) You need to set android:checkable property of the menu to true and control its checked state in runtime. Example:
Menu:
<item
android:id="#+id/checkable_menu"
android:checkable="true"
android:title="#string/checkable" />
Activity:
private boolean isChecked = false;
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem checkable = menu.findItem(R.id.checkable_menu);
checkable.setChecked(isChecked);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.checkable_menu:
isChecked = !item.isChecked();
item.setChecked(isChecked);
return true;
default:
return false;
}
}
Hope this helps.
I had a couple of issues when using a actionViewClass="android.widget.Switch in a menu item. It does actually show a switch on the ToolBar, although for me:
The onOptionsItemSelected() doesn't actually get called when I toggle
the switch.
Using setChecked() on the MenuItem doesn't toggle its state.
Upon debugging and removing the actionViewClass="android.widget.Switch, the onOptionsItemSelected() gets called again.
Not sure what was going on; Maybe that I am using a custom ActionBar that I set using setSupportActionBar(), and using OptionsMenu callbacks within a fragment by enabling it with setHasOptionsMenu(true).
I get this solved by inflating the switch itself, and set OnCheckedChangeListener on it
<item
android:id="#+id/my_switch"
android:title=""
app:actionViewClass="androidx.appcompat.widget.SwitchCompat"
app:showAsAction="always" />
And in Fragment
#Override
public void onCreateOptionsMenu(#NonNull Menu menu, #NonNull MenuInflater inflater) {
inflater.inflate(R.menu.my_menu, menu);
MenuItem menuItem = menu.findItem(R.id.my_switch);
SwitchCompat mySwitch = (SwitchCompat) menuItem.getActionView();
mySwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
// Do something when `isChecked` is true or false
});
}
And to toggle the switch programmatically, call setChecked() on the Switch, not on the MenuItem.
use app:actionViewClass
<item android:id="#+id/id"
android:title="#string/string"
app:actionViewClass="android.widget.ToggleButton"
android:orderInCategory="80"
app:showAsAction="always" />
public boolean onPrepareOptionsMenu(final Menu menu) {
if(super.mMapView.isTraffic())
menu.findItem(MENU_TRAFFIC_ID).setIcon(R.drawable.traffic_off_48);
else
menu.findItem(MENU_TRAFFIC_ID).setIcon(R.drawable.traffic_on_48);
return super.onPrepareOptionsMenu(menu);
}
Do you mean you want to add a toggle button as one of the elements/items appearing in the options menu or add a button to a list item from the menu?
Then you can do it with a custom layout(use a ListView within if you want) and inflating it in the
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
and you can save the values each time the button is toggles.
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.btnToggleValue:
// save it here
return true;
case R.id.btnSecond:
...
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I have this code to create the menu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.tip_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MNU_PREV:
animateTextViewsPrev();
break;
case MNU_NEXT:
animateTextViewsNext();
break;
}
return true;
}
And the XML:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/prev_tip" android:title="#string/prevTip"></item>
<item android:id="#+id/next_tip" android:title="#string/nextTip"></item>
</menu>
In a smartphone with Android 2.1 the menu is visible but in other mobile whit 4.1.1 is invisible.
Somebody now how to solve it?
What is you target Android, good to know, in android 4.0 them has redesign the menu layout.
I think you is missing super.onCreateOptionsMenu(menu); in the call onCreateOptionsMenu
In my code I has,
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
I was dealing with the same problem.. read some queries and documentation.. Hope this might help you.
Here's my XML file for a menu..
<item
android:id="#+id/action_send_feedback"
android:orderInCategory="100"
android:showAsAction="always"
android:title="#string/action_send_feedback"/>
<item
android:id="#+id/action_share_app"
android:orderInCategory="100"
android:showAsAction="ifRoom"
android:title="#string/action_share_app"
android:icon="#drawable/ic_action_share" />
<item
android:id="#+id/action_rate_app"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_rate_app"/>
JAVA Code goes here..
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
For android phones which have option button (at the bottom of the phone) the menu item which are showAsAction="never" comes when the button is pressed.. or else they will be shown normally on the action bar options menu..
Ref: http://developer.android.com/guide/topics/ui/menus.html#options-menu
You can simply change the "targetSdkVersion" to 10 in manifest file
It needs the ID in the java! :)