How to make Share button for your android app using Intent tutorial


In this lesson you'll learn to share data with other apps using share intents. Run this basic app. The onShareButtonClick method is called when the button is clicked. Now create a new Intent object with the code Intent intent = new Intent(Intent.ACTION_SEND);. The ACTION SEND argument indicates that this is a share intent to share some data. Add the code intent.setType("text/plain"); to specify that this intent shares some plain text (you could also share images or videos, etc). Add the code intent.putExtra(Intent.EXTRA_TEXT, "This is my text to send"); to add the actual text to be shared: This is my text to send. Call startActivity(intent);. This will make Android start an activity which can handle the share intent and send the text - for example an email app.

REQUIREMENTS:
Download:
Eclipse

Instruction:
After you download all the files needed in this tutorial, we need to install and set-up a java JDK path to our environment variables, the step below will show you how.

--Go to Java JDK installation directory in "C:\Program Files\Java\jdk1.7.0\bin" Copy this path
--Go to the environment path by Clicking: Window taskbar--->right click Computer--->Click Advance System setting ---> then go to Advanced tab Then select "Environment Variable" then create new Variable name and variable value by clicking the New radio button. Within the "Variable name" input these "JAVA_HOME" then in the "Variable value" point it to your C:\Program Files\Java\jdk1.7.0\bin 
that's it we are on a way of creating our first game and deploy it to a different platform.

After that we will also set-up Android SDK, to its path by extracting first it to the directory you want for example in "drive D:" or "drive C:"  once we did this, we can now copy the root folder of this SDK which is likely to this format "D:\Android SDK" copy it then add it also to the "Environment path" like what you did before in "Java JDK".

COMPLETE CODES

MAIN ACTIVTY CODE

import android.app.*;
import android.content.*;
import android.net.*;
import android.os.*;
import android.view.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    public void onShareButtonClick(View view)
    {
    Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT,"This is my text to send");
startActivity(intent);
    }
    
}


MAIN XML CODE

<?xml version="1.0" encoding="utf-8">
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onShareButtonClick"
        android:text="Share..." />
</LinearLayout>



Post a Comment

0 Comments