How to customize the ShareActionProvider in Androi

2019-09-07 00:24发布

问题:

How to customize the menus in this image. can anyone tell the solution

I want to display four submenus(FB, Google+, Twitter and SeeAll) If i Touch SeeAll a dialog has to popup which consist of more(FB, Google+, Twitter, Linkedin, NetLog, etc..,)

Code :

Menu.xml:-

   <menu xmlns:android="http://schemas.android.com/apk/res/android" >

       <item android:id="@+id/menu_item_share"
            android:showAsAction="ifRoom"
            android:title="Share"
            android:actionProviderClass="android.widget.ShareActionProvider" />

    </menu>

Activity:-

  public boolean onCreateOptionsMenu(Menu menu) {
         getMenuInflater().inflate(R.menu.action_bar_share_menu, menu);
         MenuItem item = menu.findItem(R.id.menu_item_share);

         ShareActionProvider myShareActionProvider = (ShareActionProvider) item.getActionProvider();

         Intent myIntent = new Intent();
         myIntent.setAction(Intent.ACTION_SEND);
         myIntent.putExtra(Intent.EXTRA_TEXT, "Whatever message you want to share");
         myIntent.setType("text/plain");


         myShareActionProvider.setShareIntent(myIntent);

         return true;

回答1:

Try This :-

      <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/action_location_found"
        android:clickable="true"
        android:icon="@drawable/ic_launcher"
        android:showAsAction="always"
        android:title="Share">
        <menu>
            <item
                android:id="@+id/facebook"
                android:orderInCategory="1"
                android:showAsAction="never"
                android:title="Facebook">
            </item>
            <item
                android:id="@+id/twitter"
                android:orderInCategory="2"
                android:showAsAction="never"
                android:title="Twitter">
            </item>
            <item
                android:id="@+id/gplus"
                android:orderInCategory="3"
                android:showAsAction="never"
                android:title="Google Plus">
            </item>
            <item
                android:id="@+id/seeall"
                android:orderInCategory="4"
                android:showAsAction="never"
                android:title="See All">
            </item>
        </menu>
    </item>

</menu>

Activity (Java Code):-

     public class MainActivity extends Activity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    }

            @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

        case R.id.facebook:

            Toast.makeText(getApplicationContext(), "Testing", 1000).show();

            // write the code here, what you want the action when user click
            // facebook submenu....

            break;
        case R.id.twitter:

            break;

        case R.id.gplus:

            break;

        case R.id.seeall:

            break;

        default:
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

Kotlin Code : -

class MainActivity:Activity() {
  protected fun onCreate(savedInstanceState:Bundle) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
  }
  fun onCreateOptionsMenu(menu:Menu):Boolean {
    val inflater = getMenuInflater()
    inflater.inflate(R.menu.main, menu)
    return super.onCreateOptionsMenu(menu)
  }
  fun onOptionsItemSelected(item:MenuItem):Boolean {
    when (item.getItemId()) {
      R.id.facebook ->
      Toast.makeText(getApplicationContext(), "Testing", 1000).show()
      R.id.twitter ->
      { //code
      }
      R.id.gplus ->
      { //code
      }
      R.id.seeall ->
      { //code
      }
      else -> return true
    }// write the code here, what you want the action when user click
    // facebook submenu....
    return super.onOptionsItemSelected(item)
  }
}