Ignacio Perez
Ignacio Perez

Reputation: 2361

Android how can I get ShareActionProvider working on ImageButton click

I am working on a project that requires that the ShareAction menu be opened on an Onclick method and I can not seem to get it to work. First of I have the ShareAction menu working but only in the settings menu and this is; is what I have when I try to apply that to ImageButton Onclick method

// This is inside the Oncreate Method

share= (ImageButton)findViewById(R.id.share);
        share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ShareActionProvider nn= new ShareActionProvider(Login.this);
                Intent ShareIntent=new Intent(Intent.ACTION_SEND);
                ShareIntent.setAction(Intent.ACTION_SEND);
                ShareIntent.setType("text/plain");
                ShareIntent.putExtra(Intent.EXTRA_TEXT, "Text To share");
               nn.setShareIntent(ShareIntent);
            }
        });

The code above does not work it does not give any errors or nothing. I have that same code working using the settings menu and doing the following Menu for activity

<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=".Login">

    <item android:id="@+id/action_settings" android:title="@string/action_settings"
        android:orderInCategory="200" android:icon="@drawable/overflow" app:showAsAction="never" />
    <item android:id="@+id/action_share" android:title="@string/action_share" android:orderInCategory="100" app:showAsAction="ifRoom"
        android:actionProviderClass="android.widget.ShareActionProvider"
        />

</menu>

then simply add it to the 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.menu_login, menu);
    MenuItem shareItem= menu.findItem(R.id.action_share);

    ShareActionProvider  mShare= (ShareActionProvider)shareItem.getActionProvider();
   Intent ShareIntent=new Intent(Intent.ACTION_SEND);
    ShareIntent.setAction(Intent.ACTION_SEND);
    ShareIntent.setType("text/plain");
    ShareIntent.putExtra(Intent.EXTRA_TEXT, "Text To share");
    mShare.setShareIntent(ShareIntent);
    return true;
}

What am I missing or doing wrong that is not letting the menu open in Oncreate?

Upvotes: 0

Views: 550

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006614

I am working on a project that requires that the ShareAction menu be opened on an Onclick method and I can not seem to get it to work

This does not make much sense. ShareActionProvider is an action bar item. It has its own "button" to allow the user to share content, based on a configured Intent indicating what sort of sharing is to be done (e.g., MIME type).

So, there are three possibilities here, that I can see:

  1. Just use the ShareActionProvider and get rid of this ImageButton.

  2. Get rid of the ShareActionProvider, and do something else to show a list of sharing actions in onClick() of the ImageButton. For example, you could wrap the Intent in Intent.createChooser() and call startActivity() on that Intent from onClick() -- if there are two or more activities that match the Intent, the user will get the chooser "menu".

  3. Fork ShareActionProvider to have it expose some sort of open() method that allows you to activate it from onClick() of your ImageButton. I cannot envision a situation in which this makes any sense (why would clicking a button over here pop open a drop-down menu over there?).

Upvotes: 1

Related Questions