• Latest Code...

    Share in social media or email or message in android


    Here is the simple tutorials for all android application developer who want to
    made share your data on social media, or message to your friend even you can save
    in memo.

    You can also share in social app like, viber, whatsapp, chaton, facebook, twitter, google plus.

    Here is the tips or code:

    first you have to create share menu.
    res/menu/main.xml
    -----------------
    <menu xmlns:android="http://schemas.android.com/apk/res/android" >

         <item
            android:id="@+id/refresh"
            android:alphabeticShortcut="r"
            android:icon="@drawable/ic_action_refresh"
            android:orderInCategory="100"
            android:showAsAction="always"/>
        <item
            android:id="@+id/share"
            android:actionProviderClass="android.widget.ShareActionProvider"
            android:icon="@drawable/ic_action_share"
            android:orderInCategory="1"
            android:showAsAction="collapseActionView"
            android:title="@string/share"/>
     

    </menu>

    -----------
    Information that you want to share
    res/layout/share.xml
    ------------
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:ems="10"
            android:inputType="textPersonName" >

            <requestFocus />
        </EditText>

        <EditText
            android:id="@+id/editText2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/editText1"
            android:layout_centerHorizontal="true"
            android:ems="10"
            android:inputType="number" />

        <EditText
            android:id="@+id/editText3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/editText2"
            android:layout_centerHorizontal="true"
            android:ems="10"
            android:inputType="textEmailAddress" />

    </RelativeLayout>

    ----------------------
    Main Source Code:
    src/MainActivity.java
    ----------------
    public class MainActivity extends Activity {

    private String shareBody;
    String name, phone, email;
    EditText et_name, et_phone, et_email;
    private Menu optionsMenu;

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

    private void setupviews() {
    // TODO Auto-generated method stub
    et_name = (EditText) findViewById(R.id.editText1);
    et_phone = (EditText) findViewById(R.id.editText2);
    et_email = (EditText) findViewById(R.id.editText3);
    }
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @SuppressLint("NewApi")
    public void setRefreshActionButtonState(final boolean refreshing) {
    if (optionsMenu != null) {
    final MenuItem refreshItem = optionsMenu.findItem(R.id.refresh);
    if (refreshItem != null) {
    if (refreshing) {
    refreshItem
    .setActionView(R.layout.actionbar_indeterminate_progress);
    } else {
    refreshItem.setActionView(null);
    }
    }
    }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    this.optionsMenu = menu;
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
    }

    // handle click events for action bar items
    @SuppressLint("NewApi")
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {

    case R.id.share:
    // showToast("Share was clicked.");

    name = et_name.getText().toString();
    phone = et_phone.getText().toString();
    email = et_email.getText().toString();

    shareBody = "\"" + "Sharing Messsage: " + "\"    " + "Name:-"
    + "-->>" + name + ", " + "Phone:- " + "-->>" + phone + ", "
    + "Email:- " + "-->>" + email;
    ShareActionProvider myShareActionProvider = (ShareActionProvider) item
    .getActionProvider();

    Intent myIntent = new Intent();
    myIntent.setAction(Intent.ACTION_SEND);
    myIntent.putExtra(Intent.EXTRA_TEXT, shareBody);
    myIntent.setType("text/plain");

    myShareActionProvider.setShareIntent(myIntent);

    return true;

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


    -----------------
    Output Looks Like:

    Happy Coding   :)

    Circle

    Most recent Elastic Search tutorials for beginner, intermediate and advance level developer, Yubraj Pokharel, in CodingWorkspace