Share Button is implement in your app to share social network and other option to share your app link.It is Use to ACTION_SEND event of Intent class to send data from one activity to another and from current activity application. Intent class is a share data from one activity to another activity.ACTION_SEND use to action sends URL to Browser. Intent are use createChooser() this method to specify title of the chooser dialog.And User to choose option in this dialog show to share app option to built in browsers, social networking apps like facebook,whatsapp etc.,bluetooth,SMS and other option are visible in this dialog.
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Share App" android:layout_gravity="center" android:textSize="20dp" android:textAllCaps="true" android:id="@+id/btn_share"/> </LinearLayout>
MainActvity.java
package com.example.bhaumik.sharebuton; 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 { Button btn_share; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_share = findViewById(R.id.btn_share); btn_share.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("text/plain"); intent.putExtra(Intent.EXTRA_TEXT,"https://play.google.com/store/apps/details?id="+ getPackageName()); startActivity(Intent.createChooser(intent,"Share")); } }); } }
0 comments:
Post a Comment