Pranish Ajagekar
Pranish Ajagekar

Reputation: 11

How to add a share button in my android app?

i am making an app that has collection of images placed in gridview. Now my prob is how to add an share button to it. i searched for it but unable to implement it successfully ?

Upvotes: 0

Views: 1367

Answers (3)

user9543620
user9543620

Reputation:

By typing the following commands you can add share button in an android app.

.XML CODE:

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="19dp"
        android:layout_marginStart="19dp"
        android:onClick="Clicked"
        android:text="@string/it"
        android:id="@+id/button2" />

STRING CODE:

<string name="it">Share</string>

JAVA CODE:

package in.android.example.birthdaycardapp;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    private EditText editText;

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

        editText = (EditText) findViewById(R.id.editText);

    }

    public void Clicked(View view) {
       /* ACTION_SEND: Deliver some data to someone else.
        createChooser (Intent target, CharSequence title): Here, target- The Intent that the user will be selecting an activity to perform.
            title- Optional title that will be displayed in the chooser.
        Intent.EXTRA_TEXT: A constant CharSequence that is associated with the Intent, used with ACTION_SEND to supply the literal data to be sent.
        */
        Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT, editText.getText().toString());
        sendIntent.setType("text/plain");
        Intent.createChooser(sendIntent, "Share via");
        startActivity(sendIntent);
    }

    Button bSave = (Button) findViewById(R.id.bSave);
bSave.setOnClickListener(new View.OnClickListener()

    {
        saveImage(true);
        Toast.makeText(getApplicationContext(), "Image Saved", 0).show();
    });

    public void saveImage(boolean isNotifyAfterSave) {
        Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        String extStorageDirectory = Environment.getExternalStorageDirectory().toString();

        File file = new File(extStorageDirectory, "ic_launcher.PNG");
        outStream = new FileOutputStream(file);
        bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
        outStream.flush();
        outStream.close();

        //if isNotifyAfterSave = true, then it will show an NotificationAlert.
        if (isNotifyAfterSave) {
            Uri uriOfFile = Uri.fromFile(file);
            showNotification("Image Saved", "desc:ic_launcher.png", "IMAGE SAVED!", uriOfFile);
        }

    }

    public void showNotification(String title, String text, String ticker, Uri pathToImage) {

        NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(
                context);

        Intent intent = new Intent(Intent.ACTION_VIEW, pathToImage);
        intent.setDataAndType(pathToImage, "image/*");

        PendingIntent pIntent = PendingIntent.getActivity(
                context.getApplicationContext(), 0, intent,
                PendingIntent.FLAG_ONE_SHOT);

        nBuilder.setContentText(text);
        nBuilder.setContentTitle(title);
        nBuilder.setContentIntent(pIntent);
        nBuilder.setTicker(ticker);
        nBuilder.setAutoCancel(true);
        nBuilder.setDefaults(Notification.DEFAULT_ALL);
        nBuilder.setSmallIcon(R.drawable.ic_stat_notify);
    }

Upvotes: 0

QArea
QArea

Reputation: 4981

Try this http://developer.android.com/training/sharing/shareaction.html

Or add in intent

Intent intent=new Intent(android.content.Intent.ACTION_SEND);
intent.setType("text/plain");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);

// Add data to the intent, the receiving app will decide what to do with it.
intent.putExtra(Intent.EXTRA_SUBJECT, “Some Subject Line”);
intent.putExtra(Intent.EXTRA_TEXT, “Body of the message, woot!”);

Upvotes: 0

Rawa
Rawa

Reputation: 13916

I don't know what you have tried but you really should take a look at the developer guides for share action. It has a fully working guide on how to implement the "share button" in android.

Upvotes: 1

Related Questions