Add required permissions
First, let's add permission to access the internet to our app. In the "Package Explorer" pane, double-click the AndroidManifest.xml. Select the "Permissions" tab, then press the "Add..." button. Select a "uses-permission". In the "Attributes for Uses Permission", enter "android.permission.INTERNET", and then save the file.
Create our main UI
Next, let's add our "Login with Twitter" button to our main Activity. In the "Package Explorer" pane, open the "res" folder, and then "layout" folder. You should see a file called "activity_main.xml". Double-click the file to open the graphical layout. At the bottom of the layout pane, select "activity_main.xml" to see the source code for the Activity.
Replace the contents of this file with:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical|center_horizontal" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical|center_horizontal" android:gravity="center" android:onClick="buttonPushed" android:text="@string/login_with_twitter" /> </LinearLayout>
Save the file and close it.
Create our webview's UI
Right-click the "layout" folder, select New, then Android XML File. Ensure that the "Resource Type" is set to "Layout". For "File", enter "oauth_web_view.xml". Under "Root Element", select "WebView". Finally, click Finish.
[image] Replace the contents of this file with:<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</WebView>
Save the file and close it.
package com.example.twittertutorial;
public class Constants {
public final static String CONSUMER_KEY = "";
public final static String CONSUMER_SECRET = "";
public final static String OAUTH_TOKEN = "oauth_token";
public final static String OAUTH_VERIFIER = "oauth_verifier";
public final static String OAUTH_DENIED = "denied";
public final static String OAUTH_CALLBACK = "twittertutorial://oauth";
}
public void onOAuthException(Exception e);
public void onRequestTokenReceived(String authorizationURL);
public void onAccessTokenReceived(AccessToken accessToken);
import twitter4j.auth.AccessToken;