简体   繁体   中英

Using ShareActionProvider with Button in Layout

I have a layout with a Button. Upon click of the button, I should be able to get the same functionality of the 'Action bar Share button'(which we can implemented using ShareActionProvider). Tried looking for an example in web ; but could not find one. Is this possible?

Yes, you can achieve the same functionality by firing an implicit intent in response to a Button click. Like the example below:

Main.java

public class MAIN extends Activity {


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


 }

 public void SHARE(View view) {

    // Do something in response to button

    EditText content = (EditText) findViewById(R.id.editText1);
     String shareBody = content.getText().toString();
        Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
            sharingIntent.setType("text/plain");
            sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "\n\n");
            sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
            startActivity(Intent.createChooser(sharingIntent,  getResources().getString(R.string.a5)));

 }
}

layout_main.java

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >



            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="31dp"
                android:text="@string/MS1"
                android:gravity="center"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <EditText
                android:id="@+id/editText1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/textView1"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="36dp"
                android:ems="10" >

                <requestFocus />
            </EditText>

            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/editText1"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="50dp"
                android:onClick="SHARE"
                android:text="SEND" />

</RelativeLayout>

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM