I have 4 fragments which have different Actionbar (some items) but they have the same dropdown navigation. I've tried to covered some items but the spinner has been moved to the left. I need it on the right.
screen
My code in one of the fragment:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.findItem(R.id.action_gps).setVisible(false);
menu.findItem(R.id.action_filter).setVisible(false);
}
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/spinner"
android:layout_gravity="left"
app:actionViewClass="android.widget.Spinner"
app:showAsAction="always" />
<item
android:id="#+id/action_search"
android:icon="#drawable/m_glass"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always" />
<item
android:id="#+id/action_filter"
android:icon="#drawable/funnel"
app:showAsAction="always|collapseActionView" />
<item
android:id="#+id/action_gps"
android:icon="#drawable/gps"
app:showAsAction="always|collapseActionView" />
Related
Action items always appear in overlay. Here is the code:
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=".MainActivity">
<item android:id="#+id/action_create_order"
android:title="#string/action_create_order"
android:icon="#drawable/ic_event_black_18dp"
android:orderInCategory="1"
app:showAsAction="always" />
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
and in the main activity:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
Shouldn't the first item appear on the action bar because of "app:showAsAction="always" ?
Use this inside your item ,to display in Action bar
app:showAsAction="always"
When you dont want to display item in Action bar
app:showAsAction="never"
Try this piece of code , instead of your creating Option Menu
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
If it is for fragments
custom namespace will prevent showAsAction from showing. so
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/action_create_order"
android:title="#string/action_create_order"
android:icon="#drawable/ic_event_black_18dp"
android:orderInCategory="1"
android:showAsAction="always"
android:icon="#android:drawable/ic_menu_add" />
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
android:showAsAction="never" />
</menu>
I want to use android v7.toolbar in my project with fragments. So i did something like below in my main activity xml. Because i dont want to add all fragments xml to android.support.v7.widget.Toolbar
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:id="#+id/toolBar" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragment_container">
</FrameLayout>
</LinearLayout>
i am adding fragments with programmatically. And in some fragments i have to change right side of the toolbar. For example in some fragments i have to use just 1 right menu icon and another fragments 2. How can i achieve it?
Also is there a way to change icon resource which is in right side of the toolbar from fragments?
Thanks,
You can create menu.xml and put all your menu items in it. Set all the items visibility to false. This hides everything.
Then in your fragment's onCreate set setHasOptionsMenu(true), this will allow you to override onCreateOptionsMenu(Menu m, MenuInflater inflater).
In this method you can do menu.findItem(id.of.item).setVisible(true/false).
Examples:
menu.xml:
<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="com.sample.app.MainActivity">
<item
android:id="#+id/action_refresh"
android:icon="#drawable/ic_action_refresh"
android:orderInCategory="101"
app:showAsAction="ifRoom"
android:title="#string/action_refresh"
android:visible="false"/>
<item
android:id="#+id/action_edit_account"
android:icon="#drawable/ic_action_edit"
android:orderInCategory="102"
app:showAsAction="never"
android:title="#string/action_edit_account"
android:visible="false"/>
<item
android:id="#+id/action_enable_offline_token"
android:orderInCategory="105"
app:showAsAction="never"
android:title="#string/action_enable_offline_token"
android:visible="false"/>
<item
android:id="#+id/action_disable_offline_token"
android:orderInCategory="105"
app:showAsAction="never"
android:title="#string/action_disable_offline_token"
android:visible="false"/>
<item
android:id="#+id/action_save"
android:icon="#drawable/ic_action_save"
android:orderInCategory="106"
app:showAsAction="ifRoom"
android:title="#string/action_save"
android:visible="false"/>
</menu>
Fragment:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
super.onCreateOptionsMenu(menu, inflater);
menu.findItem(R.id.action_refresh).setVisible(false);
menu.findItem(R.id.action_save).setVisible(true);
}
To change icons you can just get the menu and do a findItem and setIcon.
How can i display Icons in Actions bar, here is my 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/search_icon"
android:icon="#drawable/ic_action_search"
android:showAsAction="always"
android:orderInCategory="0"
android:title="Search" >
</item>
</menu>
and
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater mif = getMenuInflater();
mif.inflate(R.menu.custom_action_bar,menu);
return super.onCreateOptionsMenu(menu);
}
Thanks for Help !
If using app compat use app:showAsAction
<item android:id="#+id/search_icon"
android:icon="#drawable/ic_action_search"
app:showAsAction="always"
android:orderInCategory="100"
android:title="Search" >
</item>
In Manifest.xml File
<activity android:name="your activity name"
android:theme="#style/Theme.Holo.Light"></activity>
The below layout not displaying search icon only the another icon is displaying and the search comes as the drop down menu of other icon.I want it as separate icon in the action bar
<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.example.diveintojava1.MainActivity" >
<item android:id="#+id/search"
android:title="search"
android:icon="#drawable/search1"
android:actionViewClass="android.support.v7.widget.SearchView"
android:showAsAction="collapseActionView|ifRoom"
/>
<item android:id="#+id/file"
android:title="drop"
android:icon="#drawable/moremenu" >
<menu>
<item android:id="#+id/create_new"
android:title="java" />
<item android:id="#+id/open"
android:title="rate us" />
</menu>
</item>
</menu>
And below is the java code of implementing menu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
MenuItem searchItem = menu.findItem(R.id.search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
return true;
}
Try this, see more here : http://developer.android.com/guide/topics/ui/actionbar.html and here http://sourceoncloud.wordpress.com/2013/07/26/actionbar-compact/
<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"
xmlns:appcompact="http://schemas.android.com/apk/res-auto"
tools:context="com.example.diveintojava1.MainActivity" >
<item android:id="#+id/search"
android:title="search"
android:icon="#drawable/search1"
android:actionViewClass="android.support.v7.widget.SearchView"
appcompact:showAsAction="collapseActionView|ifRoom"
/>
<item android:id="#+id/file"
android:title="drop"
android:icon="#drawable/moremenu" >
<menu>
<item android:id="#+id/create_new"
android:title="java" />
<item android:id="#+id/open"
android:title="rate us" />
</menu>
</item>
</menu>
android:showAsAction="collapseActionView|ifRoom" Change ifRoom to always
Change this:
android:showAsAction="collapseActionView|ifRoom"
into this:
android:showAsAction="always"
Code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.start_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
start_activity.actions.xml:
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="always"
/>
<item
android:id="#+id/action_collection"
android:title="#string/action_collection"
android:icon="#drawable/ic_action_collection"
android:showAsAction="always"
/>
My menu appears only after clicking settings button. It's not visibile on launch. How to change it ?
If You have extend the activity to android.app.Activity; then the menu xml you have should work.
If your using support library android.support.v7.app.ActionBarActivity then to display the action menus in action bar, change your menu xml like below..
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
custom:showAsAction="always"/>
<item
android:id="#+id/action_collection"
android:title="#string/action_collection"
android:icon="#drawable/ic_action_collection"
custom:showAsAction="always"/>
</menu>