In many application there is a share button for share some text and/or link of application. Or in many apps which share some text to other applications using android sharing functionality by sharing Intent . In many application like Quotes, need to share their quotes to other apps using sharing functionality.
Using Intent.ACTION_SEND we can send some data to other apps. Its show popup of listing of shareable applications. You may also able to share to direct some application using setPackage().
Let’s see one simple example,
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?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"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:orientation="vertical"> <Button android:id="@+id/activity_main_btn_allapps" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Share to Apps" /> <Button android:id="@+id/activity_main_btn_whatsapp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Share on Whatsapp" /> </LinearLayout> </RelativeLayout> |
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
package com.skynils.sharetextdemo; import android.content.Context; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Context context; private Button btnAppApps, btnWhatsApp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); context = this; btnAppApps = (Button) findViewById(R.id.activity_main_btn_allapps); btnWhatsApp = (Button) findViewById(R.id.activity_main_btn_whatsapp); btnAppApps.setOnClickListener(this); btnWhatsApp.setOnClickListener(this); } @Override public void onClick(View v) { if (v == btnAppApps) { Intent i = new Intent(Intent.ACTION_SEND); i.setType("text/plain"); i.putExtra(Intent.EXTRA_SUBJECT, "SkyNils | Android Blog"); String shareText = "Hey this is a simple text sharing app, I have made from skyNils"; shareText = shareText + "http://www.skynils.com/"; // if you have live app then you can share link like below //shareText = shareText + "https://play.google.com/store/apps/details?id=" + context.getPackageName(); i.putExtra(Intent.EXTRA_TEXT, shareText); startActivity(Intent.createChooser(i, "Share via")); } else if (v == btnWhatsApp) { Intent i = new Intent(Intent.ACTION_SEND); i.setType("text/plain"); i.putExtra(Intent.EXTRA_SUBJECT, "SkyNils | Android Blog"); String shareText = "Hey this is a simple text sharing app, I have made from skyNils\n"; shareText = shareText + "http://www.skynils.com/"; i.putExtra(Intent.EXTRA_TEXT, shareText); i.setPackage("com.whatsapp"); startActivity(Intent.createChooser(i, "Share via")); } } } |
Now run project. Our output looks like below
Now see next tutorial Rating Bar in Android.
(Visited 795 times, 1 visits today)