Thursday 26 April 2018

Marvel Api Integration In Android


                            


Full Source Code Download Here:
https://github.com/sanjaymangaroliya/MarvelApiIntegration.git


MainActivity

package com.zt.marvelapiintegration.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.GridView;
import android.widget.TextView;

import com.zt.marvelapiintegration.R;
import com.zt.marvelapiintegration.global.GlobalConstant;
import com.zt.marvelapiintegration.global.Utils;
import com.zt.marvelapiintegration.restapicall.AsyncTaskCompleteListener;
import com.zt.marvelapiintegration.restapicall.ParseController;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    private GridView grid_view;
    private ArrayList<HashMap<String, String>> comicList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Toolbar
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        setTitle("");

        initUI();

    }

    private void initUI() {
        TextView tvTitle = findViewById(R.id.tv_title);
        tvTitle.setText("Comic News");
        grid_view = findViewById(R.id.grid_view);

        getComicNews();
    }

    private void getComicNews() {
        Map<String, String> map = new HashMap<>();
        map.put("url", GlobalConstant.URL + GlobalConstant.COMICS);
        map.put("ts", "1");
        map.put("apikey", GlobalConstant.PUBLIC_KEY);
        map.put("hash", Utils.MD5String("1" + GlobalConstant.PRIVATE_KEY + GlobalConstant.PUBLIC_KEY));

        Utils.hideKeyboard(this);
        new ParseController(this, ParseController.HttpMethod.GET, map,
                true, getResources().getString(R.string.loading),
                new AsyncTaskCompleteListener() {
                    @Override
                    public void onSuccess(String response) {
                        try {
                            JSONObject objMain = new JSONObject(response);
                            String code = objMain.getString("code");
                            if (code.equals("200")) {
                                JSONObject objData = objMain.getJSONObject("data");
                                JSONArray jsonArray = objData.getJSONArray("results");
                                if (jsonArray.length() > 0) {
                                    comicList.clear();
                                    for (int i = 0; i < jsonArray.length(); i++) {
                                        JSONObject object = jsonArray.getJSONObject(i);
                                        JSONObject objectThumbnail = object.getJSONObject("thumbnail");
                                        //
                                        HashMap<String, String> hashMap = new HashMap<>();
                                        String path = objectThumbnail.getString("path");
                                        String extension = objectThumbnail.getString("extension");
                                        hashMap.put("image_path", path + "." + extension);
                                        comicList.add(hashMap);
                                    }
                                    setData();
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailed(int statusCode, String msg) {
                        Utils.showToast(MainActivity.this, msg);
                    }
                });
    }

    private void setData() {
        grid_view.setAdapter(new GridViewAdapter(this, comicList));
    }

}


GridViewAdapter

package com.zt.marvelapiintegration.activity;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.zt.marvelapiintegration.R;

import java.util.ArrayList;
import java.util.HashMap;

public class GridViewAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<HashMap<String, String>> imageList;
    private DisplayImageOptions options;

    public GridViewAdapter(Context context, ArrayList<HashMap<String, String>> list) {
        mContext = context;
        imageList = list;
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.ic_launcher)
                .showImageForEmptyUri(R.mipmap.ic_launcher)
                .showImageOnFail(R.mipmap.ic_launcher)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(true)
                .build();
    }

    @Override
    public int getCount() {
        return imageList.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            view = new View(mContext);
            view = inflater.inflate(R.layout.row_gridview, null);
            ImageView img_row = view.findViewById(R.id.img_row);
            String picturePath = imageList.get(position).get("image_path");
            ImageLoader.getInstance().displayImage(picturePath, img_row, options);
        } else {
            view = convertView;
        }
        return view;
    }
}


AsyncTaskCompleteListener

package com.zt.marvelapiintegration.restapicall;

public interface AsyncTaskCompleteListener {
    void onSuccess(String response);
    void onFailed(int statusCode, String msg);
}


ParseController

package com.zt.marvelapiintegration.restapicall;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import com.zt.marvelapiintegration.global.Utils;
import java.io.File;
import java.net.MalformedURLException;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.SSLContext;
import okhttp3.ConnectionPool;
import okhttp3.ConnectionSpec;
import okhttp3.FormBody;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.TlsVersion;

public class ParseController {

    private Map<String, String> map;
    private Context activity;
    private AsyncTaskCompleteListener listener = null;
    private String strURL;
    private boolean isShowLoading;
    private String loadingMsg;
    private HttpMethod httpMethod;
    private static String strInternet = "Internet Connection is not available";
    private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/jpg");
    private static final int TIMEOUT = 15 * 60 * 1000;
    private int statusCode = 0;

    public static OkHttpClient.Builder enableTls12OnPreLollipop(OkHttpClient.Builder client) {
        if (Build.VERSION.SDK_INT >= 16 && Build.VERSION.SDK_INT < 22) {
            try {
                SSLContext sc = SSLContext.getInstance("TLSv1.2");
                sc.init(null, null, null);
                client.sslSocketFactory(new Tls12SocketFactory(sc.getSocketFactory()));

                ConnectionSpec cs = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
                        .tlsVersions(TlsVersion.TLS_1_2)
                        .build();

                List<ConnectionSpec> specs = new ArrayList<>();
                specs.add(cs);
                specs.add(ConnectionSpec.COMPATIBLE_TLS);
                specs.add(ConnectionSpec.CLEARTEXT);

                client.connectionSpecs(specs);
            } catch (Exception exc) {
                Log.e("OkHttpTLSCompat", "Error while setting TLS 1.2", exc);
            }
        }

        return client;
    }

    protected static OkHttpClient configureHttpClient() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder()
                .followRedirects(true)
                .followSslRedirects(true)
                .retryOnConnectionFailure(true)
                .cache(null)
                .connectTimeout(TIMEOUT, TimeUnit.MILLISECONDS)
                .writeTimeout(TIMEOUT, TimeUnit.MILLISECONDS)
                .readTimeout(TIMEOUT, TimeUnit.MILLISECONDS)
                .retryOnConnectionFailure(true).
                        connectionPool(new ConnectionPool(0, 1, TimeUnit.NANOSECONDS));
        return enableTls12OnPreLollipop(builder).build();
    }

    public enum HttpMethod {
        GET,
        POST,
        PUT,
        DELETE,
        FILEUPLOAD
    }

    // ParseController constructor,get all parameter via map,checking internet
    // connection
    public ParseController(Context act, HttpMethod method,
                           Map<String, String> map, boolean isShowLoading, String loadingMsg,
                           AsyncTaskCompleteListener listener) {
        this.map = map;
        this.httpMethod = method;
        this.activity = act;
        this.listener = listener;
        this.isShowLoading = isShowLoading;
        this.loadingMsg = loadingMsg;
        this.statusCode = 0;
        // is Internet Connection Available...
        if (Utils.isNetworkAvailable(activity)) {
            if (map.containsKey("url")) {
                strURL = map.get("url");
                map.remove("url");
            }
            //Multipart body must have at least one part.
            if (map.size() == 0) {
                map.put("device_type", "android");
            }
            //  getUnsafeOkHttpClient();
            new AsyncHttpRequest().execute();
            Log.d("TYPE", httpMethod.toString());
        } else {
            listener.onFailed(100, strInternet);
        }
    }


    // API call via async Task
    class AsyncHttpRequest extends AsyncTask<Void, Void, String> {
        private ProgressDialog progressDialog = new ProgressDialog(activity);

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            if (activity != null && activity instanceof Activity
                    && !((Activity) activity).isFinishing() && isShowLoading) {
                progressDialog.setMessage(loadingMsg);
                progressDialog.setCancelable(false);
                progressDialog.show();
            }
        }

        @Override
        protected String doInBackground(Void... params) {
            return chooseWebService();
        }

        @Override
        protected void onPostExecute(String result) {
            if (progressDialog != null && progressDialog.isShowing()) {
                progressDialog.dismiss();
            }
            checkResponse(result);
            super.onPostExecute(result);
        }
    }

    public String chooseWebService() {
        switch (httpMethod) {
            case POST:
                return callPOSTAPI();
            case GET:
                return callGETAPI();
            case DELETE:
                return callDeleteAPI();
            case PUT:
                return callPUTAPI();
            case FILEUPLOAD:
                map.put("url", strURL);
                return fileVideoAndImageUploading(map, 2);
            default:
                break;
        }
        return null;
    }


    public String callPOSTAPI() {
        try {
            Request.Builder builder = new Request.Builder();
            MultipartBody.Builder multipartBody = new MultipartBody.Builder();
            multipartBody.setType(MultipartBody.FORM);
            Log.e("STRURL", strURL);
            for (String key : map.keySet()) {
                Log.e("Params", key + " = " + map.get(key));
                multipartBody.addFormDataPart(key, map.get(key));
            }
            builder.url(strURL);
            builder.post(multipartBody.build());
            Request request = builder.build();
            Response response = configureHttpClient().newCall(request).execute();
            statusCode = response.code();
            return response.body().string();
        } catch (UnknownHostException ex) {
            ex.printStackTrace();
            return strInternet;
        } catch (SocketTimeoutException ex) {
            ex.printStackTrace();
            return strInternet;
        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        } catch (SocketException e) {
            e.printStackTrace();
            return strInternet;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public static String fileVideoAndImageUploading(Map<String, String> hashMap, int k) {
        try {
            Request.Builder builder = new Request.Builder();
            MultipartBody.Builder multipartBody = new MultipartBody.Builder();
            multipartBody.setType(MultipartBody.FORM);
            if (hashMap.containsKey("media")) {
                String strFilePath = hashMap.get("media");
                hashMap.remove("media");
                File file = new File(strFilePath);
                Log.d("file size", file.length() + "");
                if (file != null && file.exists()) {
                    Log.d("Params >> ", "file_data = " + strFilePath);
                    multipartBody
                            .addFormDataPart("profile_photo", file.getName(),
                                    RequestBody.create(MEDIA_TYPE_PNG, file));
                } else {
                    Log.d("file path error", "path not found: " + strFilePath);
                }
            }
            String strUrl = hashMap.get("url");
            hashMap.remove("url");
            Log.d("STRURL >> ", strUrl);
            for (String key : hashMap.keySet()) {
                Log.d("Params", key + " = " + hashMap.get(key));
                multipartBody.addFormDataPart(key, hashMap.get(key));
            }
            builder.url(strUrl);
            builder.post(multipartBody.build());
            Request request = builder.build();
            Response response = configureHttpClient().newCall(request).execute();
            return response.body().string();
        } catch (UnknownHostException ex) {
            ex.printStackTrace();
            return strInternet;
        } catch (SocketTimeoutException ex) {
            ex.printStackTrace();
            return strInternet;
        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        } catch (SocketException e) {
            e.printStackTrace();
            return strInternet;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public static String callPOSTAPI(Map<String, String> map1) {
        try {

            Request.Builder builder = new Request.Builder();

            MultipartBody.Builder multipartBody = new MultipartBody.Builder();
            multipartBody.setType(MultipartBody.FORM);


            String strUrl = map1.get("url");
            map1.remove("url");
            Log.e("STRURL >> ", strUrl);


            for (String key : map1.keySet()) {
                Log.e("Params", key + " = " + map1.get(key));

                multipartBody.addFormDataPart(key, map1.get(key));
            }


            builder.url(strUrl);
            builder.post(multipartBody.build());
            Request request = builder.build();


            Response response = configureHttpClient().newCall(request).execute();
            return response.body().string();

        } catch (UnknownHostException ex) {
            ex.printStackTrace();
            return strInternet;
        } catch (SocketTimeoutException ex) {
            ex.printStackTrace();
            return strInternet;
        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        } catch (SocketException e) {
            e.printStackTrace();
            return strInternet;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public String callPUTAPI() {
        try {
            Request.Builder builder = new Request.Builder();
            Log.e("Url in ParseController", strURL);
            FormBody.Builder formBody = new FormBody.Builder();
            for (String key : map.keySet()) {
                Log.e("Params", key + " = " + map.get(key));
                formBody.add(key, map.get(key));
            }
            builder.url(strURL);
            builder.put(formBody.build());
            Request request = builder.build();

            Response response = configureHttpClient().newCall(request).execute();
            statusCode = response.code();
            return response.body().string();

        } catch (UnknownHostException ex) {
            ex.printStackTrace();
            return strInternet;
        } catch (SocketTimeoutException ex) {
            ex.printStackTrace();
            return strInternet;
        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        } catch (SocketException e) {
            e.printStackTrace();
            return strInternet;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public String callGETAPI() {
        try {
            Log.d("STRURL", strURL);
            HttpUrl.Builder httpBuider = HttpUrl.parse(strURL).newBuilder();
            for (String key : map.keySet()) {
                Log.e("Params", key + " = " + map.get(key));
                httpBuider.addQueryParameter(key, map.get(key));
            }
            Request request = new Request.Builder().url(httpBuider.build()).build();
            Response response = configureHttpClient().newCall(request).execute();
            statusCode = response.code();
            return response.body().string();

        } catch (UnknownHostException ex) {
            ex.printStackTrace();
            return strInternet;
        } catch (SocketTimeoutException ex) {
            ex.printStackTrace();
            return strInternet;
        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        } catch (SocketException e) {
            e.printStackTrace();
            return strInternet;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public String callDeleteAPI() {
        try {
            Request.Builder builder = new Request.Builder();
            Log.d("STRURL", strURL);
            FormBody.Builder formBody = new FormBody.Builder();
            for (String key : map.keySet()) {
                Log.d("Params", key + " = " + map.get(key));
                formBody.add(key, map.get(key));
            }
            builder.url(strURL);
            builder.delete(formBody.build());
            Request request = builder.build();

            Response response = configureHttpClient().newCall(request).execute();
            statusCode = response.code();
            return response.body().string();

        } catch (UnknownHostException ex) {
            ex.printStackTrace();
            return strInternet;
        } catch (SocketTimeoutException ex) {
            ex.printStackTrace();
            return strInternet;
        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        } catch (SocketException e) {
            e.printStackTrace();
            return strInternet;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    // checking response is null or empty if not null call onTaskCompleted
    // method
    public void checkResponse(String response) {
        if (response == null || TextUtils.isEmpty(response.trim())
                || response.trim().equalsIgnoreCase("null")) {
            Log.e("Response in Controller", "Response is null");
            listener.onFailed(statusCode, "Response Is Empty Please Try Again Later");
        } else {
            Log.e("Response in Controller", response.trim());
            Log.e("=======", "=================================================================");
            listener.onSuccess(response);
        }
    }
}


SSLCertificateHandler

package com.zt.marvelapiintegration.restapicall;

import android.os.Build;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;

import okhttp3.ConnectionSpec;
import okhttp3.TlsVersion;


public class SSLCertificateHandler {

    protected static final String TAG = "NukeSSLCerts";

    /**
     * Enables https connections
     */
    public static void nuke() {
        if (Build.VERSION.SDK_INT >= 16 && Build.VERSION.SDK_INT < 22) {
            try {
                SSLContext sc = SSLContext.getInstance("sslv3");
                sc.init(null, null, null);
                HttpsURLConnection.setDefaultSSLSocketFactory(new Tls12SocketFactory(sc.getSocketFactory()));

                ConnectionSpec cs = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
                        .tlsVersions(TlsVersion.SSL_3_0)
                        .build();

                List<ConnectionSpec> specs = new ArrayList<>();
                specs.add(cs);
                specs.add(ConnectionSpec.COMPATIBLE_TLS);
                specs.add(ConnectionSpec.CLEARTEXT);

                HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
                    @Override
                    public boolean verify(String arg0, SSLSession arg1) {
                        return true;
                    }
                });
            } catch (Exception exc) {
                Log.e("OkHttpTLSCompat", "Error while setting TLS 1.2", exc);
            }
        }

    /*    try {
            TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
                public X509Certificate[] getAcceptedIssuers() {
                    X509Certificate[] myTrustedAnchors = new X509Certificate[0];
                    return myTrustedAnchors;
                }

                @Override
                public void checkClientTrusted(X509Certificate[] certs, String authType) {
                }

                @Override
                public void checkServerTrusted(X509Certificate[] certs, String authType) {
                }
            } };

            SSLContext sc = SSLContext.getInstance("TLSv1.2");
            sc.init(null, trustAllCerts, new SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
            HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String arg0, SSLSession arg1) {
                    return true;
                }
            });
        } catch (Exception e) {
        }*/
    }

}


Tls12SocketFactory

package com.zt.marvelapiintegration.restapicall;

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;

/**
 * Enables TLS v1.2 when creating SSLSockets.
 * <p/>
 * For some reason, android supports TLS v1.2 from API 16, but enables it by
 * default only from API 20.
 * @link https://developer.android.com/reference/javax/net/ssl/SSLSocket.html
 * @see SSLSocketFactory
 */
public class Tls12SocketFactory extends SSLSocketFactory {
    private static final String[] TLS_V12_ONLY = {"TLSv1.2"};

    final SSLSocketFactory delegate;

    public Tls12SocketFactory(SSLSocketFactory base) {
        this.delegate = base;
    }

    @Override
    public String[] getDefaultCipherSuites() {
        return delegate.getDefaultCipherSuites();
    }

    @Override
    public String[] getSupportedCipherSuites() {
        return delegate.getSupportedCipherSuites();
    }

    @Override
    public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException {
        return patch(delegate.createSocket(s, host, port, autoClose));
    }

    @Override
    public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
        return patch(delegate.createSocket(host, port));
    }

    @Override
    public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException {
        return patch(delegate.createSocket(host, port, localHost, localPort));
    }

    @Override
    public Socket createSocket(InetAddress host, int port) throws IOException {
        return patch(delegate.createSocket(host, port));
    }

    @Override
    public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
        return patch(delegate.createSocket(address, port, localAddress, localPort));
    }

    private Socket patch(Socket s) {
        if (s instanceof SSLSocket) {
            ((SSLSocket) s).setEnabledProtocols(TLS_V12_ONLY);
        }
        return s;
    }
}


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:fitsSystemWindows="true">

    <include layout="@layout/toolbar" />

    <GridView
        android:id="@+id/grid_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="16dp"
        android:horizontalSpacing="20dp"
        android:numColumns="3"
        android:verticalSpacing="20dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>


row_gridview.xml

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

    <ImageView
        android:id="@+id/img_row"
        android:layout_width="200dp"
        android:layout_height="300dp"
        android:scaleType="fitXY" />

</RelativeLayout>


toolbar.xml

<android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay">

        <TextView
            android:id="@+id/tv_title"
            style="@style/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/app_name" />
    </android.support.v7.widget.Toolbar>

</android.support.design.widget.AppBarLayout>


styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimary</item>
        <item name="colorAccent">@color/colorPrimary</item>
        <item name="windowActionBar">true</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

    <style name="tv_title">
        <item name="android:layout_gravity">center</item>
        <item name="android:textColor">@color/white</item>
        <item name="android:textSize">20sp</item>
        <item name="android:textAllCaps">false</item>
        <item name="android:textStyle">normal</item>
    </style>

</resources>


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.zt.marvelapiintegration">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:name=".global.GlobalApp"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".activity.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.zt.marvelapiintegration"
        minSdkVersion 16
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    /*Support Design */
    implementation 'com.android.support:design:27+'
    /* MultiDex */
    implementation 'com.android.support:multidex:1.0.1'
    /* HTTP call for webservice */
    implementation 'com.squareup.okhttp3:okhttp:3.9.0'
    /* Universal Image Loader */
    implementation 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
}






Wednesday 25 April 2018

Navigation Drawer In Android Example



    


Full Source Code Download Here:
https://github.com/sanjaymangaroliya/NavigationDrawerInAndroidExample.git


BaseNavigationActivity

package com.zt.navigationdrawer;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class BaseNavigationActivity extends AppCompatActivity {

    protected static final int NAV_DRAWER_ITEM_INVALID = -1;
    private DrawerLayout drawerLayout;
    private NavigationView navigationView;
    private Toolbar toolbar;
    private View view;

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN |
                WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

        getSetActionBarToolbar();
        setupNavDrawer();
    }

    private void setupNavDrawer() {
        drawerLayout = findViewById(R.id.drawer_layout);
        if (drawerLayout == null) {
            return;
        }

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawerLayout, toolbar,
                R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                changeInfo(view);
            }
        };
        drawerLayout.addDrawerListener(toggle);
        toggle.syncState();

        navigationView = findViewById(R.id.nav_view);
        if (navigationView != null) {
            setupDrawerSelectListener(navigationView);
            setSelectedItem(navigationView);
            view = navigationView.getHeaderView(0);
            changeInfo(view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (BaseNavigationActivity.this instanceof MainActivity) {
                        closeDrawer();
                        return;
                    }
                    goToNavDrawerItem(R.id.nav_android);
                }
            });
        }
    }

    private void setSelectedItem(NavigationView navigationView) {
        int selectedItem = getSelfNavDrawerItem();
        navigationView.setCheckedItem(selectedItem);
    }

    private void setupDrawerSelectListener(NavigationView navigationView) {
        navigationView.setNavigationItemSelectedListener(
                new NavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(MenuItem menuItem) {
                        drawerLayout.closeDrawers();
                        onNavigationItemClicked(menuItem.getItemId());
                        return true;
                    }

                });
    }

    private void onNavigationItemClicked(final int itemId) {
        if (itemId == getSelfNavDrawerItem()) {
            closeDrawer();
            return;
        }
        goToNavDrawerItem(itemId);
    }

    private void goToNavDrawerItem(int item) {
        switch (item) {
            case R.id.nav_android:
                Intent intent_android = new Intent(this, MainActivity.class);
                intent_android.putExtra("title", getString(R.string.android));
                startActivity(intent_android);
                this.finish();
                break;
            case R.id.nav_ios:
                Intent intent_ios = new Intent(this, MainActivity.class);
                intent_ios.putExtra("title", getString(R.string.ios));
                startActivity(intent_ios);
                this.finish();
                break;
            case R.id.nav_java:
                Intent intent_java = new Intent(this, MainActivity.class);
                intent_java.putExtra("title", getString(R.string.java));
                startActivity(intent_java);
                this.finish();
                break;
            case R.id.nav_php:
                Intent intent_php = new Intent(this, MainActivity.class);
                intent_php.putExtra("title", getString(R.string.php));
                startActivity(intent_php);
                this.finish();
                break;
            case R.id.nav_asp_net:
                Intent intent_asp_net = new Intent(this, MainActivity.class);
                intent_asp_net.putExtra("title", getString(R.string.asp_net));
                startActivity(intent_asp_net);
                this.finish();
                break;
        }
    }

    protected ActionBar getSetActionBarToolbar() {
        if (toolbar == null) {
            toolbar = findViewById(R.id.toolbar);
            if (toolbar != null) {
                setSupportActionBar(toolbar);
            }

        }
        return getSupportActionBar();
    }

    protected int getSelfNavDrawerItem() {
        return NAV_DRAWER_ITEM_INVALID;
    }

    protected void closeDrawer() {
        if (drawerLayout == null)
            return;
        drawerLayout.closeDrawer(GravityCompat.START);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // handle arrow click here
        if (item.getItemId() == android.R.id.home) {
            drawerLayout.openDrawer(Gravity.START);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void changeInfo(View view) {
        ImageView img_profile = view.findViewById(R.id.img_profile);
        TextView tv_name = view.findViewById(R.id.tv_name);
        TextView tv_email = view.findViewById(R.id.tv_email);
        //tv_name.setText("");
        //tv_email.setText("");
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exitByBackKey();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    protected void exitByBackKey() {
        new AlertDialog.Builder(this)
                .setTitle("Exit?")
                .setMessage("Sure, You want exit?")
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface arg0, int arg1) {
                        finishAffinity();
                    }
                })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface arg0, int arg1) {
                    }
                })
                .show();
    }
}


MainActivity

package com.zt.navigationdrawer;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.widget.TextView;

public class MainActivity extends BaseNavigationActivity {

    private TextView tvTitle, tvLanguages;
    private String strTitle = "ANDROID";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Toolbar
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        setTitle("");

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            strTitle = bundle.getString("title");
        }

        initUI();
    }

    private void initUI() {
        tvTitle = findViewById(R.id.tv_title);
        tvLanguages = findViewById(R.id.tv_languages);

        if (strTitle.equals(getResources().getString(R.string.android))) {
            tvTitle.setText(getString(R.string.android));
            tvLanguages.setText(getString(R.string.android));
        } else if (strTitle.equals(getResources().getString(R.string.ios))) {
            tvTitle.setText(getString(R.string.ios));
            tvLanguages.setText(getString(R.string.ios));
        } else if (strTitle.equals(getResources().getString(R.string.java))) {
            tvTitle.setText(getString(R.string.java));
            tvLanguages.setText(getString(R.string.java));
        } else if (strTitle.equals(getResources().getString(R.string.php))) {
            tvTitle.setText(getString(R.string.php));
            tvLanguages.setText(getString(R.string.php));
        } else {
            tvTitle.setText(getString(R.string.asp_net));
            tvLanguages.setText(getString(R.string.asp_net));
        }
    }

    @Override
    protected int getSelfNavDrawerItem() {
        if (strTitle.equals(getResources().getString(R.string.android))) {
            return R.id.nav_android;
        } else if (strTitle.equals(getResources().getString(R.string.ios))) {
            return R.id.nav_ios;
        } else if (strTitle.equals(getResources().getString(R.string.java))) {
            return R.id.nav_java;
        } else if (strTitle.equals(getResources().getString(R.string.php))) {
            return R.id.nav_php;
        } else {
            return R.id.nav_asp_net;
        }
    }

}


res/menu/activity_main_drawer.xml  

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_android"
            android:icon="@drawable/ic_android_black_24dp"
            android:title="@string/android" />
        <item
            android:id="@+id/nav_ios"
            android:icon="@drawable/ic_android_black_24dp"
            android:title="@string/ios" />
        <item
            android:id="@+id/nav_java"
            android:icon="@drawable/ic_android_black_24dp"
            android:title="@string/java" />
        <item
            android:id="@+id/nav_php"
            android:icon="@drawable/ic_web_black_24dp"
            android:title="@string/php" />
        <item
            android:id="@+id/nav_asp_net"
            android:icon="@drawable/ic_web_black_24dp"
            android:title="@string/asp_net" />
    </group>
</menu>


drawer_item_color.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/colorAccent" android:state_checked="true" />
    <item android:color="@color/colorPrimary" />
</selector>


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/white"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:itemBackground="@color/white"
        app:itemIconTint="@drawable/drawer_item_color"
        app:itemTextAppearance="@style/tv_navigation"
        app:itemTextColor="@drawable/drawer_item_color"
        app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>


app_bar_activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:fitsSystemWindows="true">

    <include layout="@layout/toolbar" />

    <ScrollView
        android:id="@+id/scrollview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <RelativeLayout
            android:id="@+id/rl_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/tv_languages"
                style="@style/tv_black"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="@dimen/margin_200"
                android:text="@string/app_name"
                android:textSize="25dp"
                android:textStyle="bold" />

        </RelativeLayout>
    </ScrollView>
</android.support.design.widget.CoordinatorLayout>


nav_header_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/rl_user_info"
    android:layout_width="match_parent"
    android:layout_height="160dp"
    android:background="@color/colorPrimary"
    android:orientation="vertical">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/img_profile"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_alignParentLeft="true"
        android:layout_centerInParent="true"
        android:layout_marginLeft="@dimen/margin_20"
        android:src="@drawable/default_user"
        app:civ_border_color="@color/white"
        app:civ_border_width="3dp" />

    <TextView
        android:id="@+id/tv_name"
        style="@style/tv_white"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/margin_15"
        android:layout_marginTop="@dimen/margin_60"
        android:layout_toRightOf="@+id/img_profile"
        android:text="@string/app_name"
        android:textSize="18dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tv_email"
        style="@style/tv_white"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tv_name"
        android:layout_below="@+id/tv_name"
        android:text="@string/email"
        android:textSize="14dp" />

    <View
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_alignParentBottom="true"
        android:background="@color/gray" />

</RelativeLayout>


toolbar.xml

<android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay">

        <TextView
            android:id="@+id/tv_title"
            style="@style/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/app_name" />
    </android.support.v7.widget.Toolbar>

</android.support.design.widget.AppBarLayout>


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.zt.navigationdrawer">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="stateHidden|adjustResize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


Monday 25 December 2017

Login With Google Plus In Android Example



 


Full Source Code Download Here:
https://github.com/sanjaymangaroliya/LoginWithGooglePlus


MainActivity.java

package com.sanjay.loginwithgoogleplus;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.auth.api.signin.GoogleSignInResult;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.OptionalPendingResult;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;

public class MainActivity extends AppCompatActivity implements
        GoogleApiClient.OnConnectionFailedListener {

    private Button btn_sign_in, btn_logout;
    private ImageView img_profile;
    private TextView tv_info;
    //private SignInButton btn_sign_in_default;
    private static final int RC_SIGN_IN = 100;
    private GoogleApiClient mGoogleApiClient;
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Toolbar
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestEmail()
                .build();
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .enableAutoManage(this, this)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .build();
        // Customizing G+ button
        //btn_sign_in_default.setSize(SignInButton.SIZE_STANDARD);
        //btn_sign_in_default.setScopes(gso.getScopeArray());

        initUI();
    }

    private void initUI() {
        TextView tvTitle = findViewById(R.id.tv_title);
        tvTitle.setText(getResources().getString(R.string.app_name));
        //btn_sign_in_default = findViewById(R.id.btn_sign_in_default);
        btn_sign_in = findViewById(R.id.btn_sign_in);
        btn_logout = findViewById(R.id.btn_logout);
        img_profile = findViewById(R.id.img_profile);
        tv_info = findViewById(R.id.tv_info);
    }

    public void onClickSignIn(View view) {
        if (Utils.isNetworkAvailable(this)) {
            Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
            startActivityForResult(signInIntent, RC_SIGN_IN);
        } else {
            Utils.showAlertDialog(this, getResources().getString(R.string.alert)
                    , getResources().getString(R.string.internet_error));
        }
    }

    private void handleSignInResult(GoogleSignInResult result) {
        if (result.isSuccess()) {
            updateUI(true);
            GoogleSignInAccount acct = result.getSignInAccount();
            String personName = acct.getDisplayName();
            String personEmail = acct.getEmail();
            Uri personPhotoUrl = acct.getPhotoUrl();

            StringBuilder stringBuilder = new StringBuilder();
            if (Utils.isNotEmptyString(personName)) {
                stringBuilder.append("Name: " + personName);
            }
            if (Utils.isNotEmptyString(personEmail)) {
                stringBuilder.append("\nEmail: " + personEmail);
            }
            tv_info.setText(stringBuilder.toString());
            //Profile Picture
            if (personPhotoUrl != null) {
                Glide.with(getApplicationContext()).load(personPhotoUrl)
                        .thumbnail(0.5f)
                        .crossFade()
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .into(img_profile);
            }
        } else {
            updateUI(false);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RC_SIGN_IN) {
            GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            handleSignInResult(result);
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
        if (opr.isDone()) {
            GoogleSignInResult result = opr.get();
            handleSignInResult(result);
        } else {
            showProgressDialog();
            opr.setResultCallback(new ResultCallback<GoogleSignInResult>() {
                @Override
                public void onResult(GoogleSignInResult googleSignInResult) {
                    hideProgressDialog();
                    handleSignInResult(googleSignInResult);
                }
            });
        }
    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    }

    private void showProgressDialog() {
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(this);
            progressDialog.setMessage("Loading...");
            progressDialog.setIndeterminate(true);
        }
        progressDialog.show();
    }

    private void hideProgressDialog() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.hide();
        }
    }

    private void updateUI(boolean isSignedIn) {
        if (isSignedIn) {
            btn_sign_in.setVisibility(View.GONE);
            btn_logout.setVisibility(View.VISIBLE);
            img_profile.setVisibility(View.VISIBLE);
            tv_info.setVisibility(View.VISIBLE);
        } else {
            btn_sign_in.setVisibility(View.VISIBLE);
            btn_logout.setVisibility(View.GONE);
            img_profile.setVisibility(View.GONE);
            tv_info.setVisibility(View.GONE);
        }
    }

    public void onClickLogout(View view) {
        if (mGoogleApiClient.isConnected()) {
            Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
                    new ResultCallback<Status>() {
                        @Override
                        public void onResult(Status status) {
                            updateUI(false);
                        }
                    });
        }
    }
}


Utils.java

package com.sanjay.loginwithgoogleplus;
import android.content.Context;
import android.content.DialogInterface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.AlertDialog;
import android.view.Gravity;
import android.widget.Toast;

public class Utils {

    public static boolean isNetworkAvailable(Context context) {
        if (context == null) {
            return false;
        }
        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 activity, String strMsg) {
        Toast toast = Toast.makeText(activity, strMsg, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
        toast.show();
    }

    public static void showAlertDialog(Context context, String title, String msg) {
        AlertDialog.Builder adb = new AlertDialog.Builder(context);
        adb.setTitle(title);
        adb.setMessage(msg);
        adb.setCancelable(false);
        adb.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        adb.create().show();
    }

    public static boolean isNotEmptyString(String str) {
        if (str != "" && str != null && str.trim().length() > 0
                && !str.equalsIgnoreCase("null")) {
            return true;
        }
        return false;
    }

}


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:fitsSystemWindows="true">

    <include layout="@layout/toolbar" />

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/nested_scrollview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <RelativeLayout
            android:id="@+id/rl_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="10dp">

            <!-- <com.google.android.gms.common.SignInButton
                 android:id="@+id/btn_sign_in_default"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:layout_marginTop="20dp"
                 android:onClick="onClickSignIn"
                 android:visibility="gone" />-->

            <Button
                android:id="@+id/btn_sign_in"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:background="@color/colorPrimary"
                android:gravity="center"
                android:onClick="onClickSignIn"
                android:text="@string/sign_in"
                android:textColor="@color/white"
                android:visibility="visible" />

            <Button
                android:id="@+id/btn_logout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/btn_sign_in"
                android:layout_marginTop="20dp"
                android:background="@color/colorPrimary"
                android:gravity="center"
                android:onClick="onClickLogout"
                android:text="@string/logout"
                android:textColor="@color/white"
                android:visibility="gone" />

            <ImageView
                android:id="@+id/img_profile"
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:layout_alignParentLeft="true"
                android:layout_below="@+id/btn_logout"
                android:layout_marginTop="20dp"
                android:src="@mipmap/ic_launcher"
                android:visibility="gone" />

            <TextView
                android:id="@+id/tv_info"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/btn_logout"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="20dp"
                android:layout_toRightOf="@+id/img_profile"
                android:text="@string/name"
                android:textColor="@color/colorPrimary"
                android:textSize="16dp"
                android:visibility="gone" />
        </RelativeLayout>
    </android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>


toolbar.xml

<android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay">

        <TextView
            android:id="@+id/tv_title"
            style="@style/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/app_name" />
    </android.support.v7.widget.Toolbar>

</android.support.design.widget.AppBarLayout>


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sanjay.loginwithgoogleplus">

    <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:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimary</item>
        <item name="colorAccent">@color/colorPrimary</item>
        <item name="windowActionBar">true</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

    <style name="tv_title">
        <item name="android:layout_gravity">center</item>
        <item name="android:textColor">@color/white</item>
        <item name="android:textSize">20sp</item>
        <item name="android:textAllCaps">false</item>
        <item name="android:textStyle">normal</item>
    </style>

    <style name="textView">
        <item name="android:singleLine">true</item>
        <item name="android:textAllCaps">false</item>
    </style>

</resources>


build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.sanjay.loginwithgoogleplus"
        minSdkVersion 16
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    //Support Design
    compile 'com.android.support:design:26+'
    //Google Auth
    compile 'com.google.android.gms:play-services-auth:11.8.0'
    //Glide Image
    compile 'com.github.bumptech.glide:glide:3.7.0'

}
apply plugin: 'com.google.gms.google-services'

Marvel Api Integration In Android

                              Full Source Code Download Here: https://github.com/sanjaymangaroliya/MarvelApiIntegration.git MainA...