i have a photo sharing apps which display bottom menu in all layout i have create one but is display when i press menu key i want menu which display when any activity start
so how can i create general menu for all layout and no need to press menu key for display bottom menu bar android
thanks
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_bookmark"
android:icon="#drawable/icon_bookmark"
android:title="Bookmark" />
<item android:id="#+id/menu_share"
android:icon="#drawable/icon_share"
android:title="Share" />
<item android:id="#+id/menu_delete"
android:icon="#drawable/icon_delete"
android:title="Delete" />
</menu>
activity.java
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menu, menu);
return true;
}
Instead of creating Menu you have to make a common custom xml layout for your menu and include it with your all activity's layout xml file. For this you can use <merge> and <include> tag of Android XML Layout.
Look at Layout Tricks: Merging Layouts
Layout Tricks: Creating Reusable UI Components
Update:
Now on your question, To open a Menu without pressing Menu Button, you can put this line in your onCreate() of Activity.
openOptionsMenu();
And to close:
closeOptionsMenu();
These both methods for Activity class, So if you are using it out-side of activity then use context of Activity and with this method.
Related
I have a menu icon for my ActionBar, but it always goes into the overflow menu no matter what I do. I want it to show up as an icon and not go into the overflow menu.
Here is my menu_daily_selfie.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">
<item android:id="#+id/camera_button"
android:icon="#android:drawable/ic_menu_camera"
android:title="Camera"
app:showAsAction="always"/>
</menu>
I'm using the appcompat library. What gives? Seems like this should work.
Here is my onCreateOptionsMenu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_daily_selfie, menu);
return true;
}
My main activity extends ListActivity since it uses a ListView
Then you are not using appcompat-v7 properly. Either:
Switch to inheriting from AppCompatActivity, and manage your own ListView, or
Stop using appcompat-v7, and switch your menu resource to use android:showAsAction instead of app:showAsAction
about android Toolbar, how to hide default setting menu in toolbar
in the this activity i hava set a single icon menu,like this –
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:title=""
android:id="#+id/menu_add"
android:icon="#drawable/actionbar_add_icon"
android:showAsAction="ifRoom">
</item>
</menu>
it is menu/main_home.xml
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_home, menu);
return super.onCreateOptionsMenu(menu);
}
but it show a three dots menu. i want know why
and how to show sub menu icon image ,it is normal in the Actionbar, but hide in the Toolbar
If you want to empty the contents of the menu, go to the res/menu folder and delete the item tags from the menu_main.xml file. You will also need to delete references to the items in onOptionsItemSelected(MenuItem item). The menu dots will disappear if there are no items.
If you want to completely remove the three dots menu, then go to the onCreateOptions(...) method in your code and delete it, or simply remove the getMenuInflater().inflate(...); portion of it. You can safely remove the onOptionsItemSelected(MenuItem item) method as well because it will have no purpose.
The simplest way to make the menu disappear is to have the onCreateOptions(...) method return false.
this question is solved use app:showAsAction ="ifRoom" not android:showAsAction ="ifRoom"
<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">
<item
android:title=""
android:id="#+id/menu_button"
app:actionLayout="#layout/menu_single_button"
app:showAsAction = "always"
/></menu>
I'm trying to implement swipe views with viewpager, on the samples I saw the container view has an actionbar , the problem here is that each of the classes I want to use as fragment and want to swipe between them has their own actionbar. I couldn't find a way to implement actionbar on each fragment independently , I'll appreciate if you can help me with this.
Thanks
You can change title, MenuItems or icon, but You can't change actionbar.
This way:
getActionBar().setTitle("My new title");
getActionBar().setIcon(R.drawable.my_icon);
Fragments has onCreateOptionsMenu() method that can be overriden. Implement a custom action bar menu on this method.
Here's a sample:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
Where the R.menu.menu is a xml file containing a custom menu for that fragment.
Also dont forget to add setHasOptionsMenu(true) just incase the menu wont show up.
EDIT:
this is a sample of the xml file used for menu:
<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">
<item android:id="#+id/action_search" android:title="Search"
android:icon="#mipmap/icon_search"
app:showAsAction="ifRoom" />
<item android:id="#+id/action_filter" android:title="Filter"
android:icon="#mipmap/icon_filter"
app:showAsAction="ifRoom" />
</menu>
On a tablet in landscape, the GMail app has the following:
The magnifying glass menu item appears to be aligned to the right side of the list fragment.
How can I achieve this in my own app?
Step I: Create a xml in the res/menu folder.
Step II: Add the following code snippet in the menu_main.xml or the pre-provided menu.xml
<?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/action_search"
android:title="#string/action_search"
android:icon="#drawable/ic_action_search_hdpi"
yourapp:showAsAction="always"
android:enabled="true"
yourapp:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
`
Step III: Inflate the menu.xml in the onCreateOptionsMenu(Menu menu)
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.menu_main, menu);
return true;
}
Since Search is also a menu option, android already provides the widget to use it seamlessly.You can also handle a click listener to integrate the search mechanism into the app with the help of API's.
I hope it helps.
I used this guide to create the main application menu.
Next, I added a button (three dots on the left) and the button I want to add another menu. It is important that the menu was different for each fragment
Code of button:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.actlist, menu);
return true;
}
and xml file for this:
<menu xmlns:android=
"http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/act"
android:icon="#drawable/ic_action_overflow"
android:showAsAction="always"
android:title="title add"
android:titleCondensed="add">
</item>
If I try to add a menu, it replaces the main menu.
Thanks!
Items that don't fit in the actionbar are automatically added to the overflow, there is no need to create another menu. If you want to force the itmes to be in the overflow you can use android:showAsAction="never"