Share item in actionbar using contextual actionbar - java

For the first time i use the contextual actionbar. When i click in the item of my ListView i want have the possibility to share that item in some way (whatsapp/mail/gmail/drive etc etc..). I'm trying in with this method but no works:
the menu layout:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/shareButton"
android:actionProviderClass="android.widget.ShareActionProvider"
android:title="#string/share"
android:showAsAction="always"/>
</menu>
The contextual actionbar class:
public class CABMode implements ActionMode.Callback {
private ShareActionProvider mshare;
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.cab, menu);
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// don't need to do anything
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
mshare = (ShareActionProvider) item.getActionProvider();
mshare.setShareIntent(Share());
return true;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
Mode = null;
}
public Intent Share() {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "Share");
return intent;
}
}
Well, the contextual actionbar it opens well when i do an long click, but the share button in the actionbar doesn't work. When i say "not works" i mean that seems not clickable. Nothing works when i click over the item. Nothing appears. What i want it's open the multiple share ways. Thanks

When i say "not works" i mean that seems not clickable
ActivityChooserView, the action view ShareActionProvider returns, disables the "share" button when there are no items to display, which in your case if because you're setting up ShareActionProvider incorrectly. All of the "on click" events are handled internally, you won't receive any callbacks to ActionMode.Callback.onActionItemClicked.
Rather than settings it up in ActionMode.Callback.onActionItemClicked, move that code into ActionMode.Callback.onCreateActionMode and call Menu.findItem to initialize it.
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.cab, menu);
final MenuItem item = menu.findItem(R.id.your_share_action_provider_id);
mshare = (ShareActionProvider) item.getActionProvider();
mshare.setShareIntent(Share());
return true;
}

Related

Share links button for all apps in android

I have the next share ImageView button in my android project:
final ImageView share = findViewById(R.id.btn_share);
share.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(Intent.ACTION_SEND);
myIntent.setType("text/plane");
myIntent.putExtra(Intent.EXTRA_TEXT, some_url_from_my_app);
startActivity(Intent.createChooser(myIntent, "Share Using"));
}
});
My problem is when i click on the button i am seeing only few apps and not all the apps in my phone as i want, like Facebook, Instagarm etc...
What should i do in order to see all the apps in my phone in order to be able to share the link to all of them?
Take the startActivity line out.
Create a XML Resource folder with the name menu inside the res folder.
Create a layout menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/share_menu">
<item
android:id="#+id/menu_item_share"
android:showAsAction="ifRoom"
android:title="Share"
android:actionProviderClass=
"android.widget.ShareActionProvider" />
</menu>
Make sure you override the onCreateOptionsMenu:
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.ShareActionProvider;
...
private ShareActionProvider mShareActionProvider;
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate menu resource file.
// R.<XML resource folder>.<XML layout filename>
getMenuInflater().inflate(R.menu.menu, menu);
// Locate MenuItem with ShareActionProvider
MenuItem shareItem = menu.findItem(R.id.menu_item_share);
// Fetch and store ShareActionProvider
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareItem);
// Return true to display menu
return true;
}
// Call to update the share intent
private void setShareIntent(Intent shareIntent) {
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(shareIntent);
}
}
After some investigation and few hours I managed to solve the issue.
What I have done is instead of doing:
myIntent.setType("text/plane");
I made it like:
myIntent.setType("text/*");
That's showed me all the apps for sharing.
And about the Facebook share I used the Facebook sdk.

Disable refresh icon on Menu while content is being refreshed

I have a refresh icon on my menu which when clicked will refresh the contents of the Activity. But the problem is that the refresh icon is still clickable while it is refreshing which I do not want. Here is my menu.xml file
<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_refresh"
android:orderInCategory="100"
android:icon="#mipmap/ic_refresh"
android:enabled="true"
android:title="#string/action_settings"
app:showAsAction="always"/>
And here is where I handle the click event for the icon
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_refresh) {
// getData() fetches the data and updates the display
getData();
return true;
}
return super.onOptionsItemSelected(item);
}
Also, is there a way to maybe dim the icon so that the user knows that they have already clicked the icon?
When you click the icon, use the isEnabled() method of your Menuitem to check if it is currently enabled. If it is, then disable it until the data is loaded. Once the data loads successfully, then you can enable it again. Example:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if(item.isEnabled()) {
// "Dim" the icon like you said. You can use other alpha values if you like.
item.getIcon().setAlpha(130);
// Disable the menu icon
item.setEnabled(false);
}
getData();
return true;
}
return super.onOptionsItemSelected(item);
}
Now, you said that you have a getData() method which updates the display after it fetches the data. Once the data is fetched, check if the Menuitem is disabled. If it is disabled, then enable it again. Example:
if(item != null && item.isEnabled() == false) {
// "Undim" the icon
item.getIcon().setAlpha(255);
// Enable it again
item.setEnabled(true);
}
You can use setEnabled(true) method to enable/disable menu item
You could create a second menu.xml, that is used after the refresh button is clicked. To force a menu redraw call invalidateOptionsMenu();
In this second menu.xml, the refresh button could be disabled, or switched to a diffrent one, indicating the user, that the refresh is on going.
Easiest way to fix this is to hold on to the menu item in your activity.
private MenuItem mRefreshItem;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater infl = new MenuInflater(this);
infl.inflate(R.menu.menu, menu);
mRefreshItem = menu.findItem(R.id.action_refresh);
return super.onCreateOptionsMenu(menu);
}
/** Call this when you begin or end loading */
private void setRefreshEnabled(boolean enabled) {
mRefreshItem.setEnabled(enabled);
}

Android ActionBar - Item icon change continuously?

On my ActionBar there is one item that has an icon about a star(without filled).
When the user clicks on it, the icon changes to another icon(filled star).
But the problem is that if the user cliccks again the icon doesn't change one more time.
So, this is what i want
icon1->click->icon2->click->icon1->click->icon2
My item xml:
<item
android:id="#+id/bookmark"
android:icon="#drawable/bookmark"
android:showAsAction="ifRoom"
android:title="Add to Favorites"/>
My Java for actionBar:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
inflater.inflate(R.menu.activity_desc, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.bookmark){
// i tried this: something like: if(item.getIcon() == (R.drawable.nobookmark){} it doesn't work
item.setIcon(R.drawable.nobookmark);
return true;
}
return true;
}
In your item.xml make it checkable:
<item
android:id="#+id/bookmark"
android:icon="#drawable/nobookmark"
android:checkable="true"
android:showAsAction="ifRoom"
android:title="Add to Favorites"/>
In the java you must manually toggle the checked state and select the icon:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.bookmark){
item.setChecked(!item.isChecked());
item.setIcon(item.isChecked() ? R.drawable.bookmark : R.drawable.nobookmark);
return true;
}
return false;
}

Change Android Action Bar Menu Item's Icon on Click

I'm using sherlock action bar.
I have 2 items on the action bar. When the item is chosen (active), I want to change the icon's image.
This is my code on Java
#Override
public boolean onPrepareOptionsMenu (Menu menu){
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menutes, menu);
todaySched=menu.findItem(R.id.todaySched);
if(todaySched.isEnabled()){
todaySched.setIcon(R.drawable.calendarselected);
}
return true;
}
but when I do this the icon become double, and the icon won't change neither.
Can someone help?
Use the on onOptionsItemSelected method
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.todaySched:
// put your code here to change the icon
return true;
default:
return super.onOptionsItemSelected(item);
}
}
you may need to include the correct namespace for the ActionBar Sherlock library to ensure it Overrides the correct menu item. So the start of the method will look like this:
#Override
public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item)

Get options menu to show in system bar

I'm trying to get my options menu to show up in the new Android 3.0 system bar. I can get this behavior to work with the Notepadv3 tutorial. However, when implementing nearly the identical functions, the menu shows in the above action bar, but not the system bar. Any ideas?
Attached is the appropriate code for my application:
public class AACUser extends Activity {
private static final int ADMIN_ID = Menu.FIRST;
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, ADMIN_ID, 0, R.string.menu_admin);
return true;
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch(item.getItemId()) {
case ADMIN_ID:
enterAdminMode();
return true;
default:
return super.onMenuItemSelected(featureId, item);
}
}
}
I've also tried implementing these menu functions as recomended by the creating menu documentation:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.user_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_adminMode:
enterAdminMode();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
but no difference: it still displays the option in the overflow of the action bar, but not the system bar.
As of Android 3.0, the options menu is not required (if not deprecated). If you target API 11 (Android 3.0) or later, it is assumed that your app will work without an options menu, so the system bar will not display one. The options menu in the system bar is only there for backwards compatibility with old applications. New applications should have another affordance for accessing options, such as that provided by the action bar.
I think that the menu button in the system bar is for backward compatibility purposes only. So if you want to design your app for Honeycomb then the menu should be and will be in the action bar.

Categories