In my application, I have created a class Base_Activity extending SherlockFragmentActivity in which I have defined an action bar this way (it is a menu bar with three different items):
public class Base_Activity extends SherlockFragmentActivity {
private ActionBar mActionBar;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getSupportMenuInflater().inflate(R.menu.main, menu);
mActionBar = getSupportActionBar();
mActionBar.setDisplayUseLogoEnabled(false);
mActionBar.setDisplayShowCustomEnabled(true);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.home:
Intent i = new Intent(this, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
return true;
case R.id.map:
Intent ii = new Intent(this, MainActivity.class);
ii.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(ii);
return true;
case R.id.favorites:
Intent iii = new Intent(this, FavoritesListView.class);
iii.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(iii);
return true;
default:
return super.onOptionsItemSelected(item);
}
} }
Then I have added this line to set this bar in the bottom of my application :
android:uiOptions="splitActionBarWhenNarrow"
It's working well, so far I don't have any problems : I have my bottom action bar with listener for every button.
I now want to add a top action bar, with a SearchBar (or SearchWidget) and a item opening a submenu in the up-right corner.
My problem is now : at the top of every activity, instead of the useless bar with the name of each activity, I want to have another top bar with :
1/ A search bar
2/ An icon opening a sub-menu in the up-right corner.
Unfortunately, I often read that it's not really possible but many application do use this. I am really having a hard time finding how to add my search bar, which doesn't make sense : I know how to write my query method, I have my databaseAdapter and Helper class, so basically it's just about displaying a simple search bar on the top, putting a listener and sending my query to the database and then display a list view.
Does anyone know something which can do the trick to keep an action bar on the bottom and have a search bar also on the top ? Thanks !
See my question. I think will answer for your first question. Second is very similar to this what I need.
Related
I have been struggling to implement switching visibility of Share option on Menu between fragments. I am using sliding tab layout and has a fragment in each of the 2 tabs. First tab (uses list view) and when an item is selected, I am setting a flag as true and calls invalidateOptionsMenu() and it works fine by showing a share option on the App bar menu, but I am not able to cancel it when I move to the other fragment which has mainly preferences. Code is similar to the below.
public void setSharedIntentText(String text) {
sharedText = text;
if (shareOptionVisibility == false) {
shareOptionVisibility = true;
invalidateOptionsMenu();
}
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);
// Locate MenuItem with ShareActionProvider
MenuItem shareItem = menu.findItem(R.id.menu_share);
// Fetch and store ShareActionProvider
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareItem);
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
sendIntent.putExtra(Intent.EXTRA_TEXT, sharedText);
mShareActionProvider.setShareIntent(sendIntent);
shareItem.setVisible(shareOptionVisibility);
When I switch between tabs, visibility should be set appropriately. I tried to set it onPause method of the first fragment, and then onResume method of second fragment, but control doesn't seem to go these methods when I look at the logcat. The code I have used to set visibility to false is as below in fragment2.
public void onResume() {
super.onResume();
Log.i(TAG, "On Resume ");
((MainActivity) getActivity()).shareOptionVisibility = false;
((MainActivity) getActivity()).invalidateOptionsMenu();
}
So would like to know where is the best place to put the code to control visibility when we switch between tabs. Here are the list of classes I use.
MainActivity, fragment1, fragment2, SlidingTabLayout, SlidingTabStrip and ViewPagerAdaptor. This code was implemented on top of the com.example.android.actionbarcompat.shareactionprovider example.
Thanks in advance
Issue now resolved by implementing the onCreateOptionsMenu method from the fragment rather than from the MainActivity.
Thanks
I need my app can show text selection toolbar after I click on a menu bar. I mean when I choose an item on menu bar may be like this Menu Bar, after that my device will appear Text Selection Toolbar like this
Text Selection Toolbar
Is it possible to do that? Please help me. Thank you.
This is my code
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
// Inflate the menu; this adds items to the action bar if it is present.
//add(groupId, itemId, order, name)
menu.add(1, 1, 1, "Copy Text");
menu.add(1, 2, 2, "Exit");
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml
case 1:
//Show text selection toolbar
case 2:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
return true;
default:
break;
}
return super.onOptionsItemSelected(item);
}
I'm programming an app based on the Weather App from Udacity Courses.
The app is based in a two activies: one general and other for weather detail with a fragment inside.
I've been experimenting with menu creation and I have and issue that I would like to comment with you all.
AFAIK, If I want to share an option between different fragments, it seems a good idea to put that option in the activity menu instead of the fragment menu, so that's what I'm doing with the Share button.
But I've seen that I have two possible situations:
1) If the menu item inside the xml is declared as showAsAction = never I can create the instance of the ShareActionProvider and the intent inside the onOptionsItemSelected, within the corresponding if sentence (and works fine):
public class DetailActivity extends Activity {
ShareActionProvider mShareActionProvider;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
getActionBar().setDisplayHomeAsUpEnabled(true);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction().add(R.id.container, new DetailFragment()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.detail, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
startActivity(new Intent(this, SettingsActivity.class));
return true;
}
if (id == R.id.action_share) {
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "nada");
mShareActionProvider.setShareIntent(shareIntent);
return true;
}
return super.onOptionsItemSelected(item);
}
2) If the menu item is declared as showAsAction = always and the Share button is in the action bar, the above doesn't work as the Share button isn't even clickable. I have to declare the above code inside the onCreateOptionsMenu to make things work.
Why is that? Is different the chain of events for creating a menu when is not in the action bar Vs. when it's in it?
Thanks for the answers and excuse me for my english.
In my app, I have an icon in the action bar. When I click on it, I get an option called Settings. However, when I click on this option, nothing happens. I have searched, but I can't find out how to utilize this option. I would like for a dialog box to open when Settingsclicked (or if another Activity opened that would be cool too), which I could then add content to. Does anyone know how I would go about this?
The solution Dave S provided works but I want to share another way you can handle this functionality.
In your Activity you can have the following code to interact with your menu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
displaySettingsDialog();
}
return super.onOptionsItemSelected(item);
}
The key is to make sure you are checking for the right ID you assigned to your menu item.
If you wanted to launch a new activity (SettingsActivity) then you can replace the displaySettingsDialog() function with the following:
Intent intent = new Intent(this, SettingsActivity.class)
startActivity(intent);
Look for main.xml in your res/menu/ folder. (at least for eclipse ADT) There you can specify an onClick attribute which will call a function in your code. from there you can do whatever you want with it.
EDIT
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity" >
<item
android:id="#+id/fb_login"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/facebook_login_label"
android:onClick="showFacebookDialog"/>
</menu>
This would define the item for my MainActivity, inside which you define the onClick like so
public void showFacebookDialog(final MenuItem item)
{
...
}
I have an app with no home/title bar, and an actionbar in tab navigation mode. I've added a menu to my app, which I want to show in Android 4+ as an overflow menu in the tab actionbar.
It sounds well and simple, but when I add the menu
// Initiating menu XML file
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menu, menu);
return true;
}
// Event handling for individual menu item selected
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_search:
// Load search fragment
return true;
case R.id.menu_add:
// Load add fragment
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I get this in landscape view, which is good:
But I get this in portrait mode, which is bad:
How do I prevent the tab actionbar from splitting in portrait mode?
EDIT: So it appears that when a menu is created in android 4, it immediately becomes an action overflow menu in the activity's titlebar. The problem is, I don't have a titlebar, just a tab navigation actionbar. So the activity creates a new title/action bar with an action overflow option.
I need to find a way to make the activity add the action overflow tab to the tab navigation actionbar. Is there a way to do it?
Thanks