Friday, 8 January 2016

Sharing Content

05:55

   
Create the sharing Intent. Add the following Java code inside your sharing method, creating an Intent object with the send action type:
Option List to Appear on Pressing the Share Button

Set a MIME type for the content you're sharing. This will determine which applications the chooser list presents to your users. Plain text, HTML, images and videos are among the common types to share. The following Java code demonstrates sending plain text:
This is a flexible option, as you can send plain text reliably through many different channels.
It is possible to target specific applications using the "setType" method, but this can be a risky strategy, potentially causing problems if the user does not have those particular apps installed. By keeping the sharing function as generic as possible, you give your users control over how they want to share your content. Sticking to the standard behaviour for sharing in Android applications also creates an intuitive user experience.

You can pass various elements of your sharing content to the send Intent, including subject, text / media content, and addresses to copy to in the case of email sharing. This Java code builds a string variable to hold the body of the text content to share:
You can of course build the content using variables and methods within your application.
Remember that some of your content will not appear when the user chooses certain channels. For example, if you set a subject, it will not appear if the user chooses to share using text messaging, and anything over 140 characters will be cropped if the user chooses Twitter.
Pass your sharing content to the "putExtra" method of the Intent class using the following Java code:

(1)    write  code for  share.java file :
package eduweb.om.com.eduweb;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.content.Intent;

public class share extends AppCompatActivity {

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_share);
        Intent shareIntent =
               
new Intent(android.content.Intent.ACTION_SEND);

//set the type
       
shareIntent.setType("text/plain");

//add a subject
       
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
               
"Insert Subject Here");

//build the body of the message to be shared
       
String shareMessage = "Insert message body here.";

//add the message
       
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT,
                shareMessage);

//start the chooser for sharing
       
startActivity(Intent.createChooser(shareIntent,
               
"select share chooser title here"));
        Toolbar toolbar = (Toolbar) findViewById(R.id.
toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.
fab);
        fab.setOnClickListener(
new View.OnClickListener() {
           
@Override
           
public void onClick(View view) {
                Snackbar.make(view,
"Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction(
"Action", null).show();
            }
        });
    }

}

(2)write   code for  activity_share.xml   file  :
<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout

    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:fitsSystemWindows="true"

    tools:context="eduweb.om.com.eduweb.share">



    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content"

        android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">



        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"

            android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"

            android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />



    </android.support.design.widget.AppBarLayout>



    <include layout="@layout/content_share" />



    <android.support.design.widget.FloatingActionButton android:id="@+id/fab"

        android:layout_width="wrap_content" android:layout_height="wrap_content"

        android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"

        android:src="@android:drawable/ic_dialog_email" />



</android.support.design.widget.CoordinatorLayout>

(3)write code for  content_share.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"

    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"

    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:paddingBottom="@dimen/activity_vertical_margin"

    app:layout_behavior="@string/appbar_scrolling_view_behavior"

    tools:showIn="@layout/activity_share" tools:context="eduweb.om.com.eduweb.share">



</RelativeLayout>



Written by

We are Creative Blogger Theme Wavers which provides user friendly, effective and easy to use themes. Each support has free and providing HD support screen casting.

0 comments:

Post a Comment

 

© 2013 ANDROID TRAINING IN MUMBAI BY OM SIR. All rights resevered. Designed by Templateism

Back To Top