androidshare-button

Android share button


After I've updated my question based on your responses, this is my .Class:

public class ShareScreen extends Activity{

         private ShareScreenAdapter adapter;
         private ViewPager viewPager;
         private String[] mThumbIds = Constants.IMAGES;

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

             viewPager = (ViewPager) findViewById(R.id.pager);

             ImageButton sharingButton = new ImageButton(this);   
             sharingButton.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
             sharingButton.setImageResource(R.drawable.share);

             Intent i = getIntent();
             int position = i.getIntExtra("position", 0);

             adapter = new ShareScreenAdapter(ShareScreen.this, mThumbIds);

             viewPager.setAdapter(adapter);

             // displaying selected image first
             viewPager.setCurrentItem(position);   

     }

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


        @Override
        public boolean onOptionsItemSelected(MenuItem item) {

             switch (item.getItemId()) {
                case R.id.menu_item_share:
                    shareIt();
                    break;
                default:
                    break;
            }
             return true;

        }


        private void shareIt()
        {
            //sharing implementation here
            String shareBody = "Here is the share content body";

            Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
            sharingIntent.setType("text/plain");

            sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
            sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);

            startActivity(Intent.createChooser(sharingIntent, "Share via"));
        }


}

In menu/main_share.xml I have the share button :

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
     <item
            android:id="@+id/menu_item_share"
            android:title="Share"
            android:showAsAction="always"
            android:actionProviderClass=
                "android.widget.ShareActionProvider" />

</menu>

Still not working ! The problem is that my shareButton from actionbar is not working. I click on it but nothing happen !


Solution

  • I guess what you wanto to do is the following

    First, create onCreateOptionsMenu and onOptionsItemSelected, which works for buttons clicked in ActionBar. Then, you fill code to match your objectives, like the following:

    MainActivity.java

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_id, menu);
        return true;
    }
    
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.shareButton:
                shareit();
                break;
            default:
                break;
        }
    return true;
    } 
    

    Menu.xml

    <menu xmlns:android="http://schemas.android.com/apk/res/android" >
        <item
            android:id="@+id/shareButton"
            android:orderInCategory="100"
            android:showAsAction="always"/> 
    </menu>