MainActivity
package com.zt.twitterintegration;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import com.twitter.sdk.android.Twitter;
import com.twitter.sdk.android.core.Callback;
import com.twitter.sdk.android.core.Result;
import com.twitter.sdk.android.core.TwitterAuthConfig;
import com.twitter.sdk.android.core.TwitterException;
import com.twitter.sdk.android.core.TwitterSession;
import com.twitter.sdk.android.core.identity.TwitterLoginButton;
import com.twitter.sdk.android.core.models.User;
import io.fabric.sdk.android.Fabric;
public class MainActivity extends AppCompatActivity {
private static final String TWITTER_KEY = "r5oW9FVgiXUULcUWMFFDCdCVU";
private static final String TWITTER_SECRET = "JUAhg9xGxBvjMPINiT8X7kHrkJ01Da2owWq1cHRhsa7Wiiu2rg";
private TwitterLoginButton twitterLoginButton;
//
private TextView tvName, tvEmail;
private ImageView imgProfilePicture;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET);
Fabric.with(this, new Twitter(authConfig));
setContentView(R.layout.activity_main);
initUI();
}
public void initUI() {
twitterLoginButton = (TwitterLoginButton) findViewById(R.id.twitter_login_button);
tvName = (TextView) findViewById(R.id.tvName);
tvEmail = (TextView) findViewById(R.id.tvEmail);
imgProfilePicture = (ImageView) findViewById(R.id.imgProfilePicture);
twitterLoginButton.setCallback(new Callback<TwitterSession>() {
@Override
public void success(Result<TwitterSession> result) {
getUserInformation(result);
}
@Override
public void failure(TwitterException exception) {
Utils.showToast(MainActivity.this, "Login failed.");
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
twitterLoginButton.onActivityResult(requestCode, resultCode, data);
}
public void getUserInformation(Result<TwitterSession> result) {
TwitterSession session = result.data;
Twitter.getApiClient(session).getAccountService()
.verifyCredentials(true, false, new Callback<User>() {
@Override
public void failure(TwitterException e) {
Utils.showToast(MainActivity.this, "Login failed.");
}
@Override
public void success(Result<User> userResult) {
try {
User user = userResult.data;
String name = user.name;
String email = user.email;
String followers = String.valueOf(user.followersCount);
String strProfilePicture = user.profileImageUrl;
Utils.showToast(MainActivity.this, "Login Successfully.");
//Name
if (name != null) {
tvName.setVisibility(View.VISIBLE);
tvName.setText("Name : " + name);
}
if (followers != null) {
tvEmail.setVisibility(View.VISIBLE);
tvEmail.setText("Followers : " + String.valueOf(followers));
}
//Profile Picture
if (strProfilePicture != null) {
imgProfilePicture.setVisibility(View.VISIBLE);
Picasso.with(MainActivity.this).load(strProfilePicture).into(imgProfilePicture);
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
/* //Get User Email
TwitterAuthClient authClient = new TwitterAuthClient();
authClient.requestEmail(session, new Callback<String>() {
@Override
public void success(Result<String> result) {
}
@Override
public void failure(TwitterException exception) {
}
});*/
}
}
Utils
package com.zt.twitterintegration;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.view.Gravity;
import android.widget.Toast;
public class Utils {
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager cm =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
return isConnected;
}
public static void showToast(Context context, String strMsg) {
Toast toast = Toast.makeText(context, strMsg, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
}
activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.twitter.sdk.android.core.identity.TwitterLoginButton
android:id="@+id/twitter_login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp" />
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/twitter_login_button"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:textColor="@color/colorPrimary"
android:textSize="20sp"
android:visibility="gone" />
<TextView
android:id="@+id/tvEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tvName"
android:layout_below="@+id/tvName"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:textColor="@color/colorPrimary"
android:textSize="20sp"
android:visibility="gone" />
<ImageView
android:id="@+id/imgProfilePicture"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_below="@+id/tvEmail"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:visibility="gone" />
<Button
android:id="@+id/btnSignOut"
android:layout_width="200dp"
android:layout_height="45dp"
android:layout_below="@+id/imgProfilePicture"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:background="@color/colorPrimary"
android:onClick="onClickSignOut"
android:text="Sign Out"
android:textAllCaps="false"
android:textColor="#FFFFFF"
android:visibility="gone" />
</RelativeLayout>
AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zt.twitterintegration">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="io.fabric.ApiKey"
android:value="b8b6b71c0f6d73b606ea38a444c847acb4176328" />
</application>
</manifest>
Full Source Code Download Here
No comments:
Post a Comment