How to share something on social media from your Android Device?

In this article, we are going to implement an example how open share window and share something through Android Device?
Submitted by Manu Jemini, on February 16, 2018

In the example, below we are going to use the Content.Intent class to share the content with various social media with the help of INTENT. To implement this in your program you will need to import: android.content.Intent, android.net.Uri, android.os.Bundle in your java file.

Then create a layout file with a single ImageView and Button:

<ImageView
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:id="@+id/image"
	android:layout_centerHorizontal="true"
	android:src="@drawable/includehelp"/>

<Button
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:text="Click to save text"
	android:id="@+id/button"
	android:layout_below="@+id/editText"
	android:onClick="share" />

In the Button above, we have put an onClick property and set it to share function which is in the java File. We assign on click function directly like this : android:onClick="click".

First, we will take the reference of the ImageView from the layout file and set it to a local variable.

Then when ever user clicks the button we parse the resource we want to send and with help of Intent share it on different social media. Like this:

    Intent S = new Intent(Intent.ACTION_SEND);
    Uri SU = Uri.parse("android.resource://com.example.hp.myapplication/*");
    S.setType("image/jpeg");
    S.putExtra(Intent.EXTRA_STREAM, SU);
    startActivity(Intent.createChooser(S, "Share image using"));

1) Java file:

package com.example.hp.myapplication;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;


public class MainActivity extends AppCompatActivity
{
	ImageView image;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		image=(ImageView)findViewById(R.id.image);
	}

	public void share(View v) {

		Intent S = new Intent(Intent.ACTION_SEND);
		Uri SU = Uri.parse("android.resource://com.example.hp.myapplication/*");
		S.setType("image/jpeg");
		S.putExtra(Intent.EXTRA_STREAM, SU);
		startActivity(Intent.createChooser(S, "Share image using"));
	}
}

2) XML file:

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.hp.myapplication.MainActivity">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/image"
        android:layout_centerHorizontal="true"
        android:src="@drawable/includehelp"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Share"
        android:id="@+id/button"
        android:layout_marginTop="61dp"
        android:layout_below="@+id/image"
        android:layout_centerHorizontal="true"
        android:onClick="share"/>

</RelativeLayout>

3) Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hp.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Output

Share in Android 0
Share in Android 1


Related Tutorials




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.