how do i get my ShareActionProvider to submit text Dynamically (from listeners) view pagers etc
Asked Answered
H

2

6

I am using Sherlock Library Action Bar, which is similar to the ICS action bars, the place where I am stuck is this

    @Override
public boolean onCreateOptionsMenu(Menu menu) {

    menu.add("Save")
    .setIcon(R.drawable.ic_compose).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

    menu.add("Search")
    .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

    SubMenu sub = menu.addSubMenu("Options");
    sub.add(0, SubMenu.NONE, 0, "First");
    sub.add(0,SubMenu.NONE, 1, "Second");
    sub.add(0, SubMenu.NONE, 2, "Three");
    sub.getItem().setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

    // HERE IS WHere I AM FACING PROBLEM IN
    getSupportMenuInflater().inflate(R.menu.share_action_provider, menu);
    // Set file with share history to the provider and set the share intent.
    actionItem = menu.findItem(R.id.menu_item_share_action_provider_action_bar);
    ShareActionProvider actionProvider = (ShareActionProvider) actionItem.getActionProvider();
    actionProvider.setShareHistoryFileName(ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME); //this is BRILLIANT WAY TO AVOID REPEATation
    actionProvider.setShareIntent(createShareIntent());

    return super.onCreateOptionsMenu(menu);
}

private Intent createShareIntent() {
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setType("text/plain");

    //THIS TEXT IS WHAT I WANT TO OBTAIN DYNAMICALLY
    shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Something goes here");
    return shareIntent;
}

Whenever I try putting the actionProvider.setShareIntent(createShareIntent()); inside a listener or anywhere other than the actionProvider.setShareIntent(createShareIntent()); I get D/View(11753): onTouchEvent: viewFlags is DISABLED

I wish to include this sharing Action provider and want to input my own text that gets generated on user inputs.

Any inputs are welcome.

Adding this doesn't work

    actionItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            Log.e("THIS IS NEVER CALLED", "??");
            return true;
        }
    });

How do I get an update for the Share button being clicked

Hispid answered 18/7, 2012 at 9:21 Comment(0)
L
0

I hit a similar issue with a android.support.v7.app.ActionBarActivity derivative (though I think this approach should work for any activity) and I overrode onPrepareOptionsMenu and got the effect you were after:

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    ShareActionProvider actionProvider = (ShareActionProvider) actionItem.getActionProvider();
    if (actionProvider != null) {
        actionProvider.setShareIntent(createShareIntent());
    }
    return super.onPrepareOptionsMenu(menu);
}
Lolland answered 1/11, 2014 at 16:13 Comment(0)
T
-1

i use menu like this:

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.your_share_button_name:
        share();
        return true;

    }
}

    private void share() {
    Intent sharingIntent = new Intent(Intent.ACTION_SEND);
    sharingIntent.setType("text/plain");
    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT,
            "your text");
    sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
            "your subject");
    startActivity(Intent.createChooser(sharingIntent, "Share using"));
}

that's all. I suggest you to put all the layout of the menu in a xml in your res/menu folder.

Troytroyer answered 18/4, 2013 at 15:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.