Monday, November 17, 2014

Google plus Integration in android

Hi Friends Please Signed app from Google Developer console.



















































Then
follow these code and then u can get google+ login and share post on Google+ and user profile details .


Design    activity-main:-





code:-


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp"
    tools:context=".MainActivity" >

  <TextView
        android:id="@+id/txt"
        android:layout_width="300dp"
        android:gravity="center"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:text="Google Plus API"/>
   <Button
        android:id="@+id/sign_in_button"
        android:layout_width="300dp"
        android:layout_gravity="center"
        android:layout_height="wrap_content"
        android:text="Sign In to Google +"/>
   <Button
        android:id="@+id/post_button"
        android:layout_width="300dp"
        android:layout_gravity="center"
        android:text="Share on Google Plus"
        android:layout_height="wrap_content"/>
   <Button
        android:id="@+id/get_data_button"
        android:layout_width="300dp"
        android:layout_gravity="center"
        android:text="Get User Data"
        android:layout_height="wrap_content"/>
</LinearLayout>

Dialog Window for user details or profile display:-


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

    
    
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/post_name"
            android:layout_width="150dp"
            android:layout_height="60dp"
            android:text="Name" />

        <TextView
            android:id="@+id/get_name"
            android:layout_width="150dp"
            android:layout_height="60dp" />
    </LinearLayout>

    
    
    
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/post_id"
            android:layout_width="150dp"
            android:layout_height="60dp"
            android:text="Profile ID" />

        <TextView
            android:id="@+id/get_id"
            android:layout_width="150dp"
            android:layout_height="60dp" />
    </LinearLayout>

    
    
    
    
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/post_url"
            android:layout_width="150dp"
            android:layout_height="60dp"
            android:text="Google Plus URL" />

        <TextView
            android:id="@+id/get_url"
            android:layout_width="150dp"
            android:layout_height="60dp" />
    </LinearLayout>

    
    
    
    
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/postDateofBirth"
            android:layout_width="150dp"
            android:layout_height="60dp"
            android:text="Age" />

        <TextView
            android:id="@+id/getDateofBirth"
            android:layout_width="150dp"
            android:layout_height="60dp" />
    </LinearLayout>

    
    
    
     <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/postLocation"
            android:layout_width="150dp"
            android:layout_height="60dp"
            android:text="Location" />

        <TextView
            android:id="@+id/getLocation"
            android:layout_width="150dp"
            android:layout_height="60dp" />
    </LinearLayout>
    
    
     
       <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/postGender"
            android:layout_width="150dp"
            android:layout_height="60dp"
            android:text="Gender" />

        <TextView
            android:id="@+id/getGender"
            android:layout_width="150dp"
            android:layout_height="60dp" />
    </LinearLayout>
    
    
     <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/postEmail"
            android:layout_width="150dp"
            android:layout_height="60dp"
            android:text="Email" />

        <TextView
            android:id="@+id/getEmail"
            android:layout_width="150dp"
            android:layout_height="60dp" />
    </LinearLayout>
    
    
    
    
    
    
    
    
    <Button
        android:id="@+id/ok_button"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:text="OK" />

</LinearLayout>





MainActivity.class:-



public class MainActivity extends Activity
implements
View.OnClickListener,
ConnectionCallbacks,
OnConnectionFailedListener,
com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks {
private static final String TAG = "MainActivity";
private static final int REQUEST_CODE_RESOLVE_ERR = 9000;
private ProgressDialog mConnectionProgressDialog;
private PlusClient mPlusClient;
private ConnectionResult mConnectionResult;
Button ShareButton, GetData, ok_btn;
Dialog get_data_dialog;
TextView name, url, id,dateofBirth,location,gender,email;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPlusClient = new PlusClient.Builder(this, this, this).setActions(
"http://schemas.google.com/AddActivity",
"http://schemas.google.com/BuyActivity").build();
setContentView(R.layout.activity_main);
// Progress bar to be displayed if the connection failure is not
// resolved.
mConnectionProgressDialog = new ProgressDialog(this);
mConnectionProgressDialog.setMessage("Signing in...");
findViewById(R.id.sign_in_button).setOnClickListener(this);
ShareButton = (Button) findViewById(R.id.post_button);
GetData = (Button) findViewById(R.id.get_data_button);

// location gender email
GetData.setOnClickListener(new View.OnClickListener() {
String disp_name, disp_url, disp_id,disp_DateofBirth,disp_location,disp_gender,disp_Email;

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (mPlusClient.isConnected()) {
get_data_dialog = new Dialog(MainActivity.this);
get_data_dialog.setContentView(R.layout.getdatafragment);
get_data_dialog.setTitle("User Details");
name = (TextView) get_data_dialog
.findViewById(R.id.get_name);
url = (TextView) get_data_dialog.findViewById(R.id.get_url);
id = (TextView) get_data_dialog.findViewById(R.id.get_id);
dateofBirth=(TextView)get_data_dialog.findViewById(R.id.getDateofBirth);
gender=(TextView)get_data_dialog.findViewById(R.id.getGender);
location=(TextView)get_data_dialog.findViewById(R.id.getLocation);
gender=(TextView)get_data_dialog.findViewById(R.id.getGender);
email=(TextView)get_data_dialog.findViewById(R.id.getEmail);
 
ok_btn = (Button) get_data_dialog
.findViewById(R.id.ok_button);
ok_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
get_data_dialog.cancel();
}
});
Person currentPerson = mPlusClient.getCurrentPerson();
disp_name = currentPerson.getDisplayName();
disp_url = currentPerson.getUrl();
disp_id = currentPerson.getId();
disp_DateofBirth=currentPerson.getBirthday();
disp_location=currentPerson.getCurrentLocation();
 
/*
disp_gender=currentPerson.hasGender(); 
disp_Email=currentPerson.hasId(); */
 
name.setText(disp_name);
url.setText(disp_url);
id.setText(disp_id);
dateofBirth.setText(disp_DateofBirth);
location.setText(disp_location);
/* 
gender.setText(disp_gender); 
email.setText(disp_Email);*/
get_data_dialog.show();
} else {
Toast.makeText(getApplicationContext(), "Please Sign In",
Toast.LENGTH_LONG).show();
}
}
}); 
 
ShareButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Launch the Google+ share dialog with attribution to your app.
Intent shareIntent = new PlusShare.Builder(MainActivity.this)
.setType("text/plain")
.setText(
"Learn2Crack is a website for beginners to learn Android Programming")
.setContentUrl(Uri.parse("http://www.learn2crack.com"))
.getIntent();
startActivityForResult(shareIntent, 0);
}
});
}

@Override
protected void onStart() {
super.onStart();
mPlusClient.connect();
}

@Override
protected void onStop() {
super.onStop();
mPlusClient.disconnect();
}

@Override
public void onConnectionFailed(ConnectionResult result) {
if (mConnectionProgressDialog.isShowing()) {
// The user clicked the sign-in button already. Start to resolve
// connection errors. Wait until onConnected() to dismiss the
// connection dialog.
if (result.hasResolution()) {
try {
result.startResolutionForResult(this,
REQUEST_CODE_RESOLVE_ERR);
} catch (SendIntentException e) {
mPlusClient.connect();
}
}
}
// Save the result and resolve the connection failure upon a user click.
mConnectionResult = result;
}

@Override
protected void onActivityResult(int requestCode, int responseCode,
Intent intent) {
if (requestCode == REQUEST_CODE_RESOLVE_ERR
&& responseCode == RESULT_OK) {
mConnectionResult = null;
mPlusClient.connect();
}
}

@Override
public void onConnected(Bundle connectionHint) {
String accountName = mPlusClient.getAccountName();
Toast.makeText(this, accountName + " is connected.", Toast.LENGTH_LONG)
.show();
}

public void onDisconnected() {
Toast.makeText(this, " Disconnected.", Toast.LENGTH_LONG).show();
Log.d(TAG, "disconnected");
}

@Override
public void onClick(View view) {
if (view.getId() == R.id.sign_in_button && !mPlusClient.isConnected()) {
if (mConnectionResult == null) {
mConnectionProgressDialog.show();
} else {
try {
mConnectionResult.startResolutionForResult(this,
REQUEST_CODE_RESOLVE_ERR);
} catch (SendIntentException e) {
// Try connecting again.
mConnectionResult = null;
mPlusClient.connect();
}
}
}
}

@Override
public void onConnectionSuspended(int arg0) {
// TODO Auto-generated method stub

}
}


manifest.xml file:-


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.amitsharma.glogin"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.USE_CREDENTIALS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.NETWORK" />
    <uses-permission android:name="android.permission.USE_CREDENTIALS" />

 
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" > 

        <activity
            android:name="in.amitsharma.glogin.MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        <meta-data
              android:name="com.google.android.gms.version"
              android:value="@integer/google_play_services_version" />
        
    </application> 
</manifest>







Thank you friends it may helpfull for you this is in details i have not added output just because of less time i have tested in my device 2.2.6 or 5.0 









No comments:

Post a Comment