Android swipe views with independent actionbar on each view - android

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>

Related

How to add only one item from menu.xml to overflow menu in android?

I have menu xml file in menu->main.xml in which two item are available. I have two activities and i want to add this two item in overflow menu in first activity but in the second activity i want to add only one item in overflow menu.
menu->main.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/action_refresh"
android:title="#string/action_refresh"
app:showAsAction="never"/>
<item
android:id="#+id/action_setting"
android:title="#string/action_setting"
app:showAsAction="never"/>
</menu>
So how to do that?
you can do it in two ways:
Have separate menu XMLs for both activities. one with both items and other with only one that you require.
remove the unneeded menu item in code inside 2nd activity, by overriding onCreateOptionsMenu e.g:
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main,menu);
menu.removeItem(R.id.action_setting);
return super.onCreateOptionsMenu(menu);
}
you could do it programmatically by hiding it...For example
MenuItem refreshIcon = menu.findItem(R.id. action_refresh);
refreshIcon.setVisible(false);
Get a reference to your menu item in onCreateOptionsMenu(menu) and update show as action as below in your second activity.
MenuItem menuitem = menu.finditem(your_id);
menuitem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
You can use the onCreateOptionsMenu that you'll have to override. You could try something like that, depending on what you need to show here:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.getItem(0).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
}

Menu item always goes in the overflow menu in Android

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

Menu item goes in dropdown list menu not in the action bar

Hello
I'm trying to add 2 menu items in the action bar. On the designer they look ok, but when I run the application, both menu items goes in the dropdown list hamburger menu (there's enough "room" to display on the action bar).
I tried to replace app:showAsAction to android:showAsAction, doesn't work this replacement.
this is my menu_main.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.tabdemo.MainActivity">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title=""
app:showAsAction="never"
/>
<item
android:id="#+id/userMenu"
android:title="User"
app:showAsAction="ifRoom" />
<item
android:id="#+id/logoutMenu"
android:icon="#drawable/opendoorlogo2"
android:title="Logout"
app:showAsAction="ifRoom" />
And this is the java code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
MenuItem userMenuItem = menu.findItem(R.id.userMenu);
userMenuItem.setTitle(username);// global string
MenuItem logoutMenuItem = menu.findItem(R.id.logoutMenu);
logoutMenuItem.setIcon(R.drawable.opendoorlogo2);
return true;
}
Thanks in advance (P.S. Logo doesn't load on Logout menu Item)
First of all make sure your drawable file is not too big for the actionbar, if so you can convert it to actionbar icon size.
https://romannurik.github.io/AndroidAssetStudio/icons-actionbar.html#source.space.trim=1&source.space.pad=0&name=ic_action_example&theme=light&color=33b5e5%2C60
android:showAsAction="always"
if this doesn't help you.
Best way is to create a custom actionbar layout and place your icon over there instead of adding it as Menus.

How I can add two menu in ActionBar Sherlock?

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"

general menu for all layout android

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.

Categories

Resources