androidbuttonandroid-viewandroid-dialogfragmentshare-button

How to add a share button inside a view in Android?


I need to add a share button to one view inside one of my DialogFragments (Not in the action bar).

Can you give me a clue or a sample maybe? All I could find googling is for action bar (like Sherlok) which is not what I'm looking for.


Solution

  • Step #1: Put a button inside of your DialogFragment.

    Step #2: When the user clicks the button, create an ACTION_SEND Intent to share whatever it is you want to share, and call startActivity() on it (or, if desired, wrap the Intent you create in a chooser Intent via Intent.createChooser()):

      void sendIt(String theMessage) {
        Intent i=new Intent(Intent.ACTION_SEND);
    
        i.setType("text/plain");
        i.putExtra(Intent.EXTRA_SUBJECT, R.string.share_subject);
        i.putExtra(Intent.EXTRA_TEXT, theMessage);
    
        startActivity(Intent.createChooser(i,
                                           getString(R.string.share_title)));
      }