ActionProvider. Change text in submenu and know when its pressed - android

I use this example
http://developer.android.com/guide/topics/ui/actionbar.html#ActionProvider
But, i want to now, if its possible change the text "see all" for other text, for example, my gallery app, have the text in spanish "ver todo", if possible to change?
And.. if possible know when the share button in the action bar (in my case actionbarsherlock) are press?
i see "However, if the action provider provides a submenu of actions, then your activity does not receive a call to onOptionsItemSelected() when the user opens the list or selects one of the submenu items" but..no other way to know it? I want that when the user press that "button" make one accion also show the list of app to share.
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater= getSupportMenuInflater();
inflater.inflate(R.menu.menu_resultados, menu);
MenuItem item = menu.findItem(R.id.menu_compartir);
mShareActionProvider =(com.actionbarsherlock.widget.ShareActionProvider) item.getActionProvider();
mShareActionProvider.setShareIntent(createShareIntent());
return true;
}
private Intent createShareIntent ()
{
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/jpeg");
shareIntent.putExtra(Intent.EXTRA_EMAIL,"TestText");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "TestSubject");
return shareIntent;
}

Try setting setOnMenuItemClickListener for ex:
MenuItem item = menu.findItem(R.id.menu_compartir);
item.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem arg0) {
// TODO Auto-generated method stub
return false;
}
});

You can change the string at actionbarsherlock 's project, by changing lines at res/values/abs__strings, or you could create a folder res/values-es, copy there abs__strings, and edit the lines there. That way, you would have both english and spanish strings on your code, depending on device's language. The line you're looking for in abs__strings is called abs__activity_chooser_view_see_all.
Just a tip, try using the search function in Eclipse for these string changing next time. I found this out by searching "See all...".
About knowing when the button has been pressed, I couldn't figure it out. I just tested that onOptionsItemSelected() does not work, just as you said...

Related

How to change text content in bottomNavigationView dynamically

I've got bottomNavigationView which is already created and attach to activity. Currently I'm trying to find out how to change text in one of navigation items which are inflated from res-menu dynamically.
I've tried to look into existing threads and none of that help me (I've already tried to pick menu from onCreateOptionsMenu and change it with setTitle option later but that wouldn't change text in the activity)
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
this.menu = menu;
return true;
}
//later update on button click (this action is performed)
menu.findItem(R.id.secondItem).setTitle("New Text");
I've expected to change text to "New Text" but it's still same as before
Okay after a while of trying I've finally figured out how to do that. Instead of taking menu during onCreateOptionsMenu I've pick menu directly from bottomNavigationView when I click on the button
//your code to invoke this action
Menu menu = bottomNavigationView.getMenu();
menu.getItem(2).setTitle("New Text");
try this method inside your activity/fragment:
public void setNavigationTitle(String title) {
MenuItem menuItem = bottomNavigationView.getMenu().findItem(R.id.action_navigation_next);
menuItem.setTitle(title);
}

ShareActionProvider from support.v7 not clicking

I am trying to get a working "share" button on the title bar. Initially, I followed the advice on developers.android.com for this, but got an exception saying that I needed to use MenuItemCompat. I am doing so, but now my share button won't even "click".
My relevant code is:
public boolean onCreateOptionsMenu(Menu menu) {
Log.d("CYCLE:","onCreateOptionsMenu()");
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_to_do_list, menu);
// Locate MenuItem with ShareActionProvider
MenuItem item = menu.findItem(R.id.action_share);
// Fetch and store ShareActionProvider
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
setShareIntent();
mShareActionProvider.setShareHistoryFileName("my_share_history.xml");
return true;
}
public void setShareIntent(){
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "some text to share");
mShareActionProvider.setShareIntent(shareIntent);
}
and my menu item in XML is:
<item android:id="#+id/action_share"
android:title="Share"
app:showAsAction="ifRoom"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"/>
am I missing something to make this button be clickable? (I am not even talking about functionality yet. Simply the ability to be clicked (you know, like with the clicking/tapping animation).
thanks.
Have you set share Intent? Without the share Intent, Android would not know what exactly need to be shared by the provider.

Android share action doesn't do anything

I'm following a course on Udacity on building an Android app (weather, in this case). I've been having trouble implementing a Share action. After getting some advice from another forum, I changed the min SDK version from 10 or 11 to 17, since this is just a learning activity. Currently, I have the "Share" button showing up in the action bar, but tapping on it does nothing. I tried putting it in the overflow menu, but still, nothing. I tried some debugging, but I don't know where the button click is supposed to be handled; the debugger goes through and creates the shareIntent object, but then nothing seems to happen with it. I looked at this doc, but when I try to handle the sharing in the view's onOptionsItemSelected, I get a null pointer exception on the call to createShareIntent. What am I missing?
Here's the nested fragment's onCreateOptionsMenu:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.detail_fragment, menu);
MenuItem item = menu.findItem(R.id.action_share);
ShareActionProvider mShareActionProvider = new ShareActionProvider(getActivity());
if(mShareActionProvider != null) {
mShareActionProvider.setShareIntent(createShareIntent());
} else {
Log.d(LOG_TAG, "Share action provider is null");
}
}
Here's the containing view's onOptionsItemSelected, with the problematic code commented out:
#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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
startActivity(new Intent(this, SettingsActivity.class));
return true;
} else if (id == R.id.action_share) {
//DetailFragment details = (DetailFragment) getFragmentManager().findFragmentByTag("detailFragment");
//startActivity(details.createShareIntent());
}
return super.onOptionsItemSelected(item);
}
And here's the createShareIntent method:
private Intent createShareIntent() {
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, mForecastStr + FORECAST_SHARE_HASHTAG);
return shareIntent;
}
Currently, I have the "Share" button showing up in the action bar, but tapping on it does nothing.
Make sure you are going through your code branch that calls setShareIntent(), and make sure that the device or emulator you are testing on has an activity from some app that supports ACTION_SEND for text/plain. Also, if there is only one activity that supports ACTION_SEND for text/plain, the icon for that activity will appear adjacent to the ShareActionProvider share icon, and you would tap the activity icon to share with that activity.
I tried some debugging, but I don't know where the button click is supposed to be handled
That is inside the implementation of ShareActionProvider.
I looked at this doc, but when I try to handle the sharing in the view's onOptionsItemSelected, I get a null pointer exception on the call to createShareIntent.
onOptionsItemSelected() is for regular action items, not action providers.
It turns out I had put android:actionProviderClass="android.widget.ShareActionProvider in a layout XML file instead of a menu one. I don't think the Udacity course I'm taking mentioned the correct location for that (perhaps due to compatibility things).

How to hide the share action (which use most) icon near the share action provider?

See the picture. How can i hide the icon "P" which means share to Pinterest?
They are both on the action bar and I use ActionBarSherlock.
If you wish to keep all the share history data model, but just don't want the extra "default share activity" icom. The answer at How do you turn off share history when using ShareActionProvider? is not good enough.
What you should do is:
Copy these classes from the ActionBarSherlock to your project code
ShareActionProvider.java
ActivityChooserView.java
At your ShareActionProvider.java class, import the ActivityChooserView.java which you just copied instead of the ActionBarShelock file location
At the ActivityChooserView.java -
find the line if (activityCount > 0 && historySize > 0)
replace this line with if (false) (it's pretty ugly, but it's the quickest fix. you can delve into the code to remove all occurrences of DefaultActivity implementation)
Edit:
Don't forget to set the new ActionProvider to your menu item, from XML it would look like: android:actionProviderClass="com.*.CustomShareActionProvider"
That's it!
I found a way to work around this. I am using support library 23.0.1, I have not tested this on other support library versions.
The solution is easy, when you create ShareActionProvider, just override method onCreateActionView() and return null for it. Then you can track all history in the popup menu, but the history will not be shown in toolbar.
Here is a code sample:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem item = menu.add(Menu.NONE, R.id.menu_share, Menu.NONE, R.string.share);
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
mShareActionProvider = new ShareActionProvider(this) {
#Override
public View onCreateActionView() {
return null;
}
};
item.setIcon(R.drawable.abc_ic_menu_share_mtrl_alpha);
MenuItemCompat.setActionProvider(item, mShareActionProvider);
return true;
}
Currently I have not found any problem using this work around.
Based off Sean's answer I created the necessary classes, you can copy them into your project (https://gist.github.com/saulpower/10557956). This not only adds the ability to turn off history, but also to filter the apps you would like to share with (if you know the package name).
private final String[] INTENT_FILTER = new String[] {
"com.twitter.android",
"com.facebook.katana"
};
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.journal_entry_menu, menu);
// Set up ShareActionProvider's default share intent
MenuItem shareItem = menu.findItem(R.id.action_share);
if (shareItem instanceof SupportMenuItem) {
mShareActionProvider = new ShareActionProvider(this);
mShareActionProvider.setShareIntent(ShareUtils.share(mJournalEntry));
mShareActionProvider.setIntentFilter(Arrays.asList(INTENT_FILTER));
mShareActionProvider.setShowHistory(false);
((SupportMenuItem) shareItem).setSupportActionProvider(mShareActionProvider);
}
return super.onCreateOptionsMenu(menu);
}

What is the method on an onOptionsItemSelected menu in WebView for "forward" and "back" in Android?

I am trying to get my webView to move back and forward by calling up the menu. Thus far I have the menu working fine and opening, and the refresh button will refresh the browser. However, I cannot find documentation anywhere about the methods for forward of back. If the default internet browser has them by just pressing the menu key there must be out there. I just can't find them/don't know how to implement them. Any help would be beneficial, thank you.
Here is what I am trying to do:
WebView.Java:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_navigation, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.back_button:
"???"
case R.id.forward_button:
"???"
case R.id.refresh_button:
refreshBrowser();
return true;
}
return false;
}
The part that I'm missing is marked by the three question marks "???". Let me know if you need more information to get some help, and thanks again.
Use webView.canGoBack and webView.canGoForward to check if there is history data to go back / forward and then load that url.

Categories

Resources