Android - Facebook Integration Tutorial

<p >Android allows your application to connect to facebook and share data or any kind of updates on facebook. This chapter is about integrating facebook into your application.</p> <p >There are two ways through which you can integrate facebook and share something from your application. These ways are listed below &minus;</p> <ul class="list" > <li >Facebook SDK</li> <li >Intent Share</li> </ul> <h2 >Integrating Facebook SDK</h2> <p >This is the first way of connecting with facebook. You have to register your application and then receive some Application Id , and then you have to download the facebook SDK and add it to your project. The steps are listed below:</p> <h3 >Generating application signature</h3> <p >You have to generate a key signature, but before you generate it, make sure you have SSL installed, otherwise you have to download SSl. It can be downloaded<a href="https://code.google.com/p/openssl-for-windows/downloads/detail?name=openssl-0.9.8k_WIN32.zip&amp;can=2&amp;q=" >here</a>.</p> <p >Now open command prompt and redirect to your java jre folder. Once you reach there, type this command exactly. You have to replace the path in the inverted commas with your keystore path which you can found in eclipse by selecting the window tab and selecting the preferences tab and then selecting the build option under android from left side.</p> <pre class="result notranslate " > keytool -exportcert -alias androiddebugkey -keystore &quot;your path&quot; | openssl sha1 -binary | openssl base64 </pre> <p >Once you enter it, you will be prompt for password. Give android as the password and then copy the key that is given to you. It is shown in the image below &minus;</p> <p><img alt="Android Facebook Tutorial" src="http://www.tutorialspoint.com/android/images/facebook_2.jpg" /></p> <h3 >Registering your application</h3> <p >Now create a new facebook application at<a href="http://developers.facebook.com/apps" >developers.facebook.com/apps</a>and fill all the information. It is shown below &minus;</p> <p><img alt="Android Facebook Tutorial" src="http://www.tutorialspoint.com/android/images/facebook_3.jpg" /></p> <p >Now move to the native android app section and fill in your project and class name and paste the hash that you copied in step 1. It is shown below &minus;</p> <p><img alt="Android Facebook Tutorial" src="http://www.tutorialspoint.com/android/images/facebook_4.jpg" /></p> <p >If everything works fine, you will receive an application ID with the secret. Just copy the application id and save it somewhere. It is shown in the image below &minus;</p> <p><img alt="Android Facebook Tutorial" src="http://www.tutorialspoint.com/android/images/facebook_5.jpg" /></p> <h3 >Downloading SDK and integrating it</h3> <p >Download facebook sdk<a href="https://github.com/facebook/facebook-android-sdk" >here</a>. Import this into eclipse. Once imported, right click on your facebook project and click on properties.Click on android, click on add button and select facebook sdk as the project.Click ok.</p> <h3 >Creating facebook login application</h3> <p >Once everything is complete , you can run the samples, that comes with SDK or create your own application. In order to login, you need to call<b >openActiveSession</b>method and implements its callback. Its syntax is given below &minus;</p> <pre class="prettyprint notranslate prettyprinted" > <span class="com" >// start Facebook Login</span><span class="pln" > </span><span class="typ" >Session</span><span class="pun" >.</span><span class="pln" >openActiveSession</span><span class="pun" >(</span><span class="kwd" >this</span><span class="pun" >,</span><span class="pln" > </span><span class="kwd" >true</span><span class="pun" >,</span><span class="pln" > </span><span class="kwd" >new</span><span class="pln" > </span><span class="typ" >Session</span><span class="pun" >.</span><span class="typ" >StatusCallback</span><span class="pun" >()</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="com" >// callback when session changes state</span><span class="pln" > </span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >void</span><span class="pln" > call</span><span class="pun" >(</span><span class="typ" >Session</span><span class="pln" > session</span><span class="pun" >,</span><span class="pln" > </span><span class="typ" >SessionState</span><span class="pln" > state</span><span class="pun" >,</span><span class="pln" > </span><span class="typ" >Exception</span><span class="pln" > exception</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="kwd" >if</span><span class="pln" > </span><span class="pun" >(</span><span class="pln" >session</span><span class="pun" >.</span><span class="pln" >isOpened</span><span class="pun" >())</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="com" >// make request to;2 the /me API</span><span class="pln" > </span><span class="typ" >Request</span><span class="pun" >.</span><span class="pln" >executeMeRequestAsync</span><span class="pun" >(</span><span class="pln" >session</span><span class="pun" >,</span><span class="pln" > </span><span class="kwd" >new</span><span class="pln" > </span><span class="typ" >Request</span><span class="pun" >.</span><span class="pln" > </span><span class="typ" >GraphUserCallback</span><span class="pun" >()</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="com" >// callback after Graph API response with user object</span><span class="pln" > </span><span class="lit" >@Override</span><span class="pln" > </span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >void</span><span class="pln" > onCompleted</span><span class="pun" >(</span><span class="typ" >GraphUser</span><span class="pln" > user</span><span class="pun" >,</span><span class="pln" > </span><span class="typ" >Response</span><span class="pln" > response</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="kwd" >if</span><span class="pln" > </span><span class="pun" >(</span><span class="pln" >user </span><span class="pun" >!=</span><span class="pln" > </span><span class="kwd" >null</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="typ" >TextView</span><span class="pln" > welcome </span><span class="pun" >=</span><span class="pln" > </span><span class="pun" >(</span><span class="typ" >TextView</span><span class="pun" >)</span><span class="pln" > findViewById</span><span class="pun" >(</span><span class="pln" >R</span><span class="pun" >.</span><span class="pln" >id</span><span class="pun" >.</span><span class="pln" >welcome</span><span class="pun" >);</span><span class="pln" > welcome</span><span class="pun" >.</span><span class="pln" >setText</span><span class="pun" >(</span><span class="str" >&quot;Hello &quot;</span><span class="pln" > </span><span class="pun" >+</span><span class="pln" > user</span><span class="pun" >.</span><span class="pln" >getName</span><span class="pun" >()</span><span class="pln" > </span><span class="pun" >+</span><span class="pln" > </span><span class="str" >&quot;!&quot;</span><span class="pun" >);</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="pun" >});</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="pun" >}</span></pre> <h2 >Intent share</h2> <p >Intent share is used to share data between applications. In this strategy, we will not handle the SDK stuff, but let the facebook application handles it. We will simply call the facebook application and pass the data to share. This way, we can share something on facebook.</p> <p >Android provides intent library to share data between activities and applications. In order to use it as share intent , we have to specify the type of the share intent to<b >ACTION_SEND</b>. Its syntax is given below &minus;</p> <pre class="prettyprint notranslate prettyprinted" > <span class="typ" >Intent</span><span class="pln" > shareIntent </span><span class="pun" >=</span><span class="pln" > </span><span class="kwd" >new</span><span class="pln" > </span><span class="typ" >Intent</span><span class="pun" >();</span><span class="pln" > shareIntent</span><span class="pun" >.</span><span class="pln" >setAction</span><span class="pun" >(</span><span class="typ" >Intent</span><span class="pun" >.</span><span class="pln" >ACTION_SEND</span><span class="pun" >);</span></pre> <p >Next thing you need to is to define the type of data to pass , and then pass the data. Its syntax is given below &minus;</p> <pre class="prettyprint notranslate prettyprinted" > <span class="pln" >shareIntent</span><span class="pun" >.</span><span class="pln" >setType</span><span class="pun" >(</span><span class="str" >&quot;text/plain&quot;</span><span class="pun" >);</span><span class="pln" > shareIntent</span><span class="pun" >.</span><span class="pln" >putExtra</span><span class="pun" >(</span><span class="typ" >Intent</span><span class="pun" >.</span><span class="pln" >EXTRA_TEXT</span><span class="pun" >,</span><span class="pln" > </span><span class="str" >&quot;Hello, from tutorialspoint&quot;</span><span class="pun" >);</span><span class="pln" > startActivity</span><span class="pun" >(</span><span class="typ" >Intent</span><span class="pun" >.</span><span class="pln" >createChooser</span><span class="pun" >(</span><span class="pln" >shareIntent</span><span class="pun" >,</span><span class="pln" > </span><span class="str" >&quot;Share your thoughts&quot;</span><span class="pun" >));</span></pre> <p >Apart from the these methods, there are other methods available that allows intent handling. They are listed below &minus;</p> <table class="table table-bordered" > <tbody > <tr > <th >Sr.No</th> <th >Method &amp; description</th> </tr> <tr > <td >1</td> <td ><b >addCategory(String category)</b> <p >This method add a new category to the intent.</p> </td> </tr> <tr > <td >2</td> <td ><b >createChooser(Intent target, CharSequence title)</b> <p >Convenience function for creating a ACTION_CHOOSER Intent</p> </td> </tr> <tr > <td >3</td> <td ><b >getAction()</b> <p >This method retrieve the general action to be performed, such as ACTION_VIEW</p> </td> </tr> <tr > <td >4</td> <td ><b >getCategories()</b> <p >This method return the set of all categories in the intent and the current scaling event</p> </td> </tr> <tr > <td >5</td> <td ><b >putExtra(String name, int value)</b> <p >This method add extended data to the intent.</p> </td> </tr> <tr > <td >6</td> <td ><b >toString()</b> <p >This method returns a string containing a concise, human-readable description of this object</p> </td> </tr> </tbody> </table> <h2 >Example</h2> <p >Here is an example demonstrating the use of IntentShare to share data on facebook. It creates a basic application that allows you to share some text on facebook.</p> <p >To experiment with this example, you can run this on an actual device or in an emulator.</p> <table class="table table-bordered" > <tbody > <tr > <th >Steps</th> <th >Description</th> </tr> <tr > <td >1</td> <td >You will use Android studio to create an Android application under a package com.example.sairamkrishna.myapplication. While creating this project, make sure you Target SDK and Compile With at the latest version of Android SDK to use higher levels of APIs.</td> </tr> <tr > <td >2</td> <td >Modify src/MainActivity.java file to add necessary code.</td> </tr> <tr > <td >3</td> <td >Modify the res/layout/activity_main to add respective XML components.</td> </tr> <tr > <td >4</td> <td >Run the application and choose a running android device and install the application on it and verify the results.</td> </tr> </tbody> </table> <p >Following is the content of the modified main activity file<b >MainActivity.java</b>.</p> <pre class="prettyprint notranslate prettyprinted" > <span class="kwd" >package</span><span class="pln" > com</span><span class="pun" >.</span><span class="pln" >example</span><span class="pun" >.</span><span class="pln" >sairamkrishna</span><span class="pun" >.</span><span class="pln" >myapplication</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >app</span><span class="pun" >.</span><span class="typ" >Notification</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >app</span><span class="pun" >.</span><span class="typ" >NotificationManager</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >app</span><span class="pun" >.</span><span class="typ" >PendingIntent</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >content</span><span class="pun" >.</span><span class="typ" >Context</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >content</span><span class="pun" >.</span><span class="typ" >Intent</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >content</span><span class="pun" >.</span><span class="typ" >SharedPreferences</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >graphics</span><span class="pun" >.</span><span class="typ" >Typeface</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >net</span><span class="pun" >.</span><span class="typ" >Uri</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >support</span><span class="pun" >.</span><span class="pln" >v7</span><span class="pun" >.</span><span class="pln" >app</span><span class="pun" >.</span><span class="typ" >ActionBarActivity</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >os</span><span class="pun" >.</span><span class="typ" >Bundle</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >view</span><span class="pun" >.</span><span class="typ" >Menu</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >view</span><span class="pun" >.</span><span class="typ" >MenuItem</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >view</span><span class="pun" >.</span><span class="typ" >View</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >widget</span><span class="pun" >.</span><span class="typ" >Button</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >widget</span><span class="pun" >.</span><span class="typ" >EditText</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >widget</span><span class="pun" >.</span><span class="typ" >ImageView</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >widget</span><span class="pun" >.</span><span class="typ" >TextView</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >widget</span><span class="pun" >.</span><span class="typ" >Toast</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > java</span><span class="pun" >.</span><span class="pln" >io</span><span class="pun" >.</span><span class="typ" >FileNotFoundException</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > java</span><span class="pun" >.</span><span class="pln" >io</span><span class="pun" >.</span><span class="typ" >InputStream</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > </span><span class="kwd" >static</span><span class="pln" > java</span><span class="pun" >.</span><span class="pln" >lang</span><span class="pun" >.</span><span class="typ" >System</span><span class="pun" >.</span><span class="pln" >currentTimeMillis</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >class</span><span class="pln" > </span><span class="typ" >MainActivity</span><span class="pln" > </span><span class="kwd" >extends</span><span class="pln" > </span><span class="typ" >ActionBarActivity</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="kwd" >private</span><span class="pln" > </span><span class="typ" >ImageView</span><span class="pln" > img</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >protected</span><span class="pln" > </span><span class="kwd" >void</span><span class="pln" > onCreate</span><span class="pun" >(</span><span class="typ" >Bundle</span><span class="pln" > savedInstanceState</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="kwd" >super</span><span class="pun" >.</span><span class="pln" >onCreate</span><span class="pun" >(</span><span class="pln" >savedInstanceState</span><span class="pun" >);</span><span class="pln" > setContentView</span><span class="pun" >(</span><span class="pln" >R</span><span class="pun" >.</span><span class="pln" >layout</span><span class="pun" >.</span><span class="pln" >activity_main</span><span class="pun" >);</span><span class="pln" > img</span><span class="pun" >=(</span><span class="typ" >ImageView</span><span class="pun" >)</span><span class="pln" >findViewById</span><span class="pun" >(</span><span class="pln" >R</span><span class="pun" >.</span><span class="pln" >id</span><span class="pun" >.</span><span class="pln" >imageView</span><span class="pun" >);</span><span class="pln" > </span><span class="typ" >Button</span><span class="pln" > b1</span><span class="pun" >=(</span><span class="typ" >Button</span><span class="pun" >)</span><span class="pln" >findViewById</span><span class="pun" >(</span><span class="pln" >R</span><span class="pun" >.</span><span class="pln" >id</span><span class="pun" >.</span><span class="pln" >button</span><span class="pun" >);</span><span class="pln" > b1</span><span class="pun" >.</span><span class="pln" >setOnClickListener</span><span class="pun" >(</span><span class="kwd" >new</span><span class="pln" > </span><span class="typ" >View</span><span class="pun" >.</span><span class="typ" >OnClickListener</span><span class="pun" >()</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="lit" >@Override</span><span class="pln" > </span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >void</span><span class="pln" > onClick</span><span class="pun" >(</span><span class="typ" >View</span><span class="pln" > v</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="typ" >Intent</span><span class="pln" > sharingIntent </span><span class="pun" >=</span><span class="pln" > </span><span class="kwd" >new</span><span class="pln" > </span><span class="typ" >Intent</span><span class="pun" >(</span><span class="typ" >Intent</span><span class="pun" >.</span><span class="pln" >ACTION_SEND</span><span class="pun" >);</span><span class="pln" > </span><span class="typ" >Uri</span><span class="pln" > screenshotUri </span><span class="pun" >=</span><span class="pln" > </span><span class="typ" >Uri</span><span class="pun" >.</span><span class="pln" >parse</span><span class="pun" >(</span><span class="str" >&quot;android.resource://comexample.sairamkrishna.myapplication/*&quot;</span><span class="pun" >);</span><span class="pln" > </span><span class="kwd" >try</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="typ" >InputStream</span><span class="pln" > stream </span><span class="pun" >=</span><span class="pln" > getContentResolver</span><span class="pun" >().</span><span class="pln" >openInputStream</span><span class="pun" >(</span><span class="pln" >screenshotUri</span><span class="pun" >);</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="kwd" >catch</span><span class="pln" > </span><span class="pun" >(</span><span class="typ" >FileNotFoundException</span><span class="pln" > e</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="com" >// TODO Auto-generated catch block</span><span class="pln" > e</span><span class="pun" >.</span><span class="pln" >printStackTrace</span><span class="pun" >();</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > sharingIntent</span><span class="pun" >.</span><span class="pln" >setType</span><span class="pun" >(</span><span class="str" >&quot;image/jpeg&quot;</span><span class="pun" >);</span><span class="pln" > sharingIntent</span><span class="pun" >.</span><span class="pln" >putExtra</span><span class="pun" >(</span><span class="typ" >Intent</span><span class="pun" >.</span><span class="pln" >EXTRA_STREAM</span><span class="pun" >,</span><span class="pln" > screenshotUri</span><span class="pun" >);</span><span class="pln" > startActivity</span><span class="pun" >(</span><span class="typ" >Intent</span><span class="pun" >.</span><span class="pln" >createChooser</span><span class="pun" >(</span><span class="pln" >sharingIntent</span><span class="pun" >,</span><span class="pln" > </span><span class="str" >&quot;Share image using&quot;</span><span class="pun" >));</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="pun" >});</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="lit" >@Override</span><span class="pln" > </span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >boolean</span><span class="pln" > onCreateOptionsMenu</span><span class="pun" >(</span><span class="typ" >Menu</span><span class="pln" > menu</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="com" >// Inflate the menu; this adds items to the action bar if it is present.</span><span class="pln" > getMenuInflater</span><span class="pun" >().</span><span class="pln" >inflate</span><span class="pun" >(</span><span class="pln" >R</span><span class="pun" >.</span><span class="pln" >menu</span><span class="pun" >.</span><span class="pln" >menu_main</span><span class="pun" >,</span><span class="pln" > menu</span><span class="pun" >);</span><span class="pln" > </span><span class="kwd" >return</span><span class="pln" > </span><span class="kwd" >true</span><span class="pun" >;</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="lit" >@Override</span><span class="pln" > </span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >boolean</span><span class="pln" > onOptionsItemSelected</span><span class="pun" >(</span><span class="typ" >MenuItem</span><span class="pln" > item</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="com" >// Handle action bar item clicks here. The action bar will</span><span class="pln" > </span><span class="com" >// automatically handle clicks on the Home/Up button, so long</span><span class="pln" > </span><span class="com" >// as you specify a parent activity in AndroidManifest.xml.</span><span class="pln" > </span><span class="kwd" >int</span><span class="pln" > id </span><span class="pun" >=</span><span class="pln" > item</span><span class="pun" >.</span><span class="pln" >getItemId</span><span class="pun" >();</span><span class="pln" > </span><span class="com" >//noinspection SimplifiableIfStatement</span><span class="pln" > </span><span class="kwd" >if</span><span class="pln" > </span><span class="pun" >(</span><span class="pln" >id </span><span class="pun" >==</span><span class="pln" > R</span><span class="pun" >.</span><span class="pln" >id</span><span class="pun" >.</span><span class="pln" >action_settings</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="kwd" >return</span><span class="pln" > </span><span class="kwd" >true</span><span class="pun" >;</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="kwd" >return</span><span class="pln" > </span><span class="kwd" >super</span><span class="pun" >.</span><span class="pln" >onOptionsItemSelected</span><span class="pun" >(</span><span class="pln" >item</span><span class="pun" >);</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="pun" >}</span></pre> <p >Following is the modified content of the xml<b >res/layout/activity_main.xml</b>.</p> <pre class="prettyprint notranslate prettyprinted" > <span class="tag" >&lt;RelativeLayout</span><span class="pln" > </span><span class="atn" >xmlns:android</span><span class="pun" >=</span><span class="atv" >&quot;http://schemas.android.com/apk/res/android&quot;</span><span class="pln" > </span><span class="atn" >xmlns:tools</span><span class="pun" >=</span><span class="atv" >&quot;http://schemas.android.com/tools&quot;</span><span class="pln" > </span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >&quot;match_parent&quot;</span><span class="pln" > </span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >&quot;match_parent&quot;</span><span class="pln" > </span><span class="atn" >android:paddingLeft</span><span class="pun" >=</span><span class="atv" >&quot;@dimen/activity_horizontal_margin&quot;</span><span class="pln" > </span><span class="atn" >android:paddingRight</span><span class="pun" >=</span><span class="atv" >&quot;@dimen/activity_horizontal_margin&quot;</span><span class="pln" > </span><span class="atn" >android:paddingTop</span><span class="pun" >=</span><span class="atv" >&quot;@dimen/activity_vertical_margin&quot;</span><span class="pln" > </span><span class="atn" >android:paddingBottom</span><span class="pun" >=</span><span class="atv" >&quot;@dimen/activity_vertical_margin&quot;</span><span class="pln" > </span><span class="atn" >tools:context</span><span class="pun" >=</span><span class="atv" >&quot;.MainActivity&quot;</span><span class="tag" >&gt;</span><span class="pln" > </span><span class="tag" >&lt;TextView</span><span class="pln" > </span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >&quot;@+id/textView&quot;</span><span class="pln" > </span><span class="atn" >android:layout_alignParentTop</span><span class="pun" >=</span><span class="atv" >&quot;true&quot;</span><span class="pln" > </span><span class="atn" >android:layout_centerHorizontal</span><span class="pun" >=</span><span class="atv" >&quot;true&quot;</span><span class="pln" > </span><span class="atn" >android:textSize</span><span class="pun" >=</span><span class="atv" >&quot;30dp&quot;</span><span class="pln" > </span><span class="atn" >android:text</span><span class="pun" >=</span><span class="atv" >&quot;Facebook share &quot;</span><span class="pln" > </span><span class="tag" >/&gt;</span><span class="pln" > </span><span class="tag" >&lt;TextView</span><span class="pln" > </span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:text</span><span class="pun" >=</span><span class="atv" >&quot;Tutorials Point&quot;</span><span class="pln" > </span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >&quot;@+id/textView2&quot;</span><span class="pln" > </span><span class="atn" >android:layout_below</span><span class="pun" >=</span><span class="atv" >&quot;@+id/textView&quot;</span><span class="pln" > </span><span class="atn" >android:layout_centerHorizontal</span><span class="pun" >=</span><span class="atv" >&quot;true&quot;</span><span class="pln" > </span><span class="atn" >android:textSize</span><span class="pun" >=</span><span class="atv" >&quot;35dp&quot;</span><span class="pln" > </span><span class="atn" >android:textColor</span><span class="pun" >=</span><span class="atv" >&quot;#ff16ff01&quot;</span><span class="pln" > </span><span class="tag" >/&gt;</span><span class="pln" > </span><span class="tag" >&lt;ImageView</span><span class="pln" > </span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >&quot;@+id/imageView&quot;</span><span class="pln" > </span><span class="atn" >android:layout_below</span><span class="pun" >=</span><span class="atv" >&quot;@+id/textView2&quot;</span><span class="pln" > </span><span class="atn" >android:layout_centerHorizontal</span><span class="pun" >=</span><span class="atv" >&quot;true&quot;</span><span class="pln" > </span><span class="atn" >android:src</span><span class="pun" >=</span><span class="atv" >&quot;@drawable/logo&quot;</span><span class="tag" >/&gt;</span><span class="pln" > </span><span class="tag" >&lt;Button</span><span class="pln" > </span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:text</span><span class="pun" >=</span><span class="atv" >&quot;Share&quot;</span><span class="pln" > </span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >&quot;@+id/button&quot;</span><span class="pln" > </span><span class="atn" >android:layout_marginTop</span><span class="pun" >=</span><span class="atv" >&quot;61dp&quot;</span><span class="pln" > </span><span class="atn" >android:layout_below</span><span class="pun" >=</span><span class="atv" >&quot;@+id/imageView&quot;</span><span class="pln" > </span><span class="atn" >android:layout_centerHorizontal</span><span class="pun" >=</span><span class="atv" >&quot;true&quot;</span><span class="pln" > </span><span class="tag" >/&gt;</span><span class="pln" > </span><span class="tag" >&lt;/RelativeLayout&gt;</span></pre> <p >Following is the content of<b >AndroidManifest.xml</b>file.</p> <pre class="prettyprint notranslate prettyprinted" > <span class="pun" >&lt;?</span><span class="pln" >xml version</span><span class="pun" >=</span><span class="str" >&quot;1.0&quot;</span><span class="pln" > encoding</span><span class="pun" >=</span><span class="str" >&quot;utf-8&quot;</span><span class="pun" >?&gt;</span><span class="pln" > </span><span class="tag" >&lt;manifest</span><span class="pln" > </span><span class="atn" >xmlns:android</span><span class="pun" >=</span><span class="atv" >&quot;http://schemas.android.com/apk/res/android&quot;</span><span class="pln" > </span><span class="atn" >package</span><span class="pun" >=</span><span class="atv" >&quot;com.example.sairamkrishna.myapplication&quot;</span><span class="pln" > </span><span class="tag" >&gt;</span><span class="pln" > </span><span class="tag" >&lt;application</span><span class="pln" > </span><span class="atn" >android:allowBackup</span><span class="pun" >=</span><span class="atv" >&quot;true&quot;</span><span class="pln" > </span><span class="atn" >android:icon</span><span class="pun" >=</span><span class="atv" >&quot;@mipmap/ic_launcher&quot;</span><span class="pln" > </span><span class="atn" >android:label</span><span class="pun" >=</span><span class="atv" >&quot;@string/app_name&quot;</span><span class="pln" > </span><span class="atn" >android:theme</span><span class="pun" >=</span><span class="atv" >&quot;@style/AppTheme&quot;</span><span class="pln" > </span><span class="tag" >&gt;</span><span class="pln" > </span><span class="tag" >&lt;activity</span><span class="pln" > </span><span class="atn" >android:name</span><span class="pun" >=</span><span class="atv" >&quot;.MainActivity&quot;</span><span class="pln" > </span><span class="atn" >android:label</span><span class="pun" >=</span><span class="atv" >&quot;@string/app_name&quot;</span><span class="pln" > </span><span class="tag" >&gt;</span><span class="pln" > </span><span class="tag" >&lt;intent-filter&gt;</span><span class="pln" > </span><span class="tag" >&lt;action</span><span class="pln" > </span><span class="atn" >android:name</span><span class="pun" >=</span><span class="atv" >&quot;android.intent.action.MAIN&quot;</span><span class="pln" > </span><span class="tag" >/&gt;</span><span class="pln" > </span><span class="tag" >&lt;category</span><span class="pln" > </span><span class="atn" >android:name</span><span class="pun" >=</span><span class="atv" >&quot;android.intent.category.LAUNCHER&quot;</span><span class="pln" > </span><span class="tag" >/&gt;</span><span class="pln" > </span><span class="tag" >&lt;/intent-filter&gt;</span><span class="pln" > </span><span class="tag" >&lt;/activity&gt;</span><span class="pln" > </span><span class="tag" >&lt;/application&gt;</span><span class="pln" > </span><span class="tag" >&lt;/manifest&gt;</span></pre> <p >Let&#39;s try to run your Application. I assume you have connected your actual Android Mobile device with your computer. To run the app from Android studio, open one of your project&#39;s activity files and click Run<img alt="Eclipse Run Icon" class="inline" src="http://www.tutorialspoint.com/android/images/eclipse_run.jpg" />icon from the toolbar. Before starting your application, Android studio will display following window to select an option where you want to run your Android application.</p> <p><img alt="Android facebook Tutorial" src="http://www.tutorialspoint.com/android/images/choose_device.jpg" /></p> <p >Select your mobile device as an option and then check your mobile device which will display your default screen &minus;</p> <p><img alt="Android facebook Tutorial" src="http://www.tutorialspoint.com/android/images/facebook.jpg" /></p> <p >Now just tap on the button and you will see a list of share providers.</p> <p><img alt="Android facebook Tutorial" src="http://www.tutorialspoint.com/android/images/facebook1.jpg" /></p> <p >Now just select facebook from that list and then write any message. It is shown in the image below &minus;</p> <p><img alt="Android facebook Tutorial" src="http://www.tutorialspoint.com/android/images/facebook2.jpg" /></p> <div></div>
RangeTime:0.006583s
RangeMem:243.54 KB
返回顶部 留言