Tuesday, 23 August 2016

Share Via Option in Android


In this Application you will learn to implement an effective and user friendly share action to android app. You can add android share action anywhere in your application.
Here I have added share action in actionbar/toolbar and on button’s click event.
 If user clicks the share button from actionbar/toolbar,
many options will be available there to share like whatsapp,email, gmail, facebook, linkedin,twitter  



activity_main.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"
   
app:layout_behavior="@string/appbar_scrolling_view_behavior"
   
tools:context="com.sgp.anil.sharevia.MainActivity"
   
tools:showIn="@layout/activity_main">


    <
TextView
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:textAppearance="?android:attr/textAppearanceLarge"
       
android:text="Share via Option Example"
       
android:id="@+id/textView"
        
android:layout_gravity="center_horizontal" />

    <
TextView
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:textAppearance="?android:attr/textAppearanceLarge"
       
android:text="Anil Android"
       
android:id="@+id/textView2"
       
android:layout_gravity="center_horizontal"
       
android:layout_marginTop="20dp" />
</
LinearLayout>

menu_main.xml
-----------------------
<menu 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"
   
tools:context="com.sgp.anil.sharevia.MainActivity">
    <
item
       
android:id="@+id/share"
       
android:actionProviderClass="android.widget.ShareActionProvider"
       
android:icon="@android:drawable/ic_menu_share"
       
android:title="Share"
       
app:showAsAction="always" />
</
menu>

MainActivity.Java
-----------------------
 package com.sgp.anil.sharevia;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.
toolbar);
        setSupportActionBar(toolbar);


    }

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

   
@Override
   
public boolean onOptionsItemSelected(MenuItem item) {
       
// Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
       
int id = item.getItemId();

       
//noinspection SimplifiableIfStatement
       
if (id == R.id.share) {
            Intent sharingIntent =
new Intent(android.content.Intent.ACTION_SEND);
            sharingIntent.setType(
"text/plain");
            String shareBodyText =
"This is My Text to Send";
            sharingIntent.putExtra(android.content.Intent.
EXTRA_SUBJECT, "Subject Test");
            sharingIntent.putExtra(android.content.Intent.
EXTRA_TEXT, shareBodyText);
            startActivity(Intent.createChooser(sharingIntent,
"Sharing Option"));
           
return true;
        }

       
return super.onOptionsItemSelected(item);
    }
}


No comments:

Post a Comment

Encryption Decryption Example Android Using AES Alogirthm

activity_main.xml ------------------------- <?xml version="1.0" encoding="utf-8"?> <LinearLayou...