Category: Android

Android is an open source and Linux-based operating system for mobile devices such as smartphones and tablet computers. It was developed by the Open Handset Alliance, led by Google, and other companies.

Android programming is based on Java programming language so if you have basic understanding on Java programming then it will be a fun to learn its application development. Its applications are usually developed in the Java language using the Android Software Development Kit. Once developed, the applications can be packaged easily and sold out either through a store such as Google Play, SlideME, Opera Mobile Store, Mobango, F-droid and the Amazon Appstore.

Android Distribution

It powers hundreds of millions of mobile devices in more than 190 countries around the world. It’s the largest installed base of any mobile platform and growing fast. Every day more than 1 million new devices are activated worldwide.

This category will help you learn all the basics and advanced areas to enable you become a programming ninja.

Google Login in Android

Google Login in Android

Hello guys, in our previous articles we looked at Facebook and Twitter login in Android. In this article, we are now going to try doing the same using Google.

Nowadays everyone has a Gmail account, and we could make it easy for our app user’s to use Gmail login to simplify their authentication process in our app.

Google Login in Android

Before you can start integrating Google Sign-In in your own app, you must configure a Google API Console project and set up your Android Studio project. We are going to cover the steps involved, step by step.

Today Almost all web and mobile apps allow you to login with Google and Facebook Login. Android Login with Google is a really useful feature for both the app developer and the user, since almost everybody tend to have a Google and Facebook account and moreover with android login with Google you don’t need to remember your UserId and password.

In this Android login with Google tutorial we will describe how to integrate Google Login to your Android Application, We will be creating a sample Google Login app describing how to integrate google login and retrieve user profile. The user can login using their existing Google Account. Integration of Google Login to the App allows the app developers to perform actions on user behalf.


Google Sign-In for Android has the following requirements:

  • A compatible Android device that runs Android 2.3 or newer and includes the Google Play Store or an emulator with an AVD that runs the Google APIs platform based on Android 4.2.2 or newer and has Google Play Services version 9.8.0 or newer.
  • The latest version of the Android SDK, including the SDK Tools component. The SDK is available from the Android SDK Manager in Android Studio.
  • A project configured to compile against Android 2.3 (Gingerbread) or newer.
  • The Google Play Services SDK:
    1. In Android Studio, select Tools > Android > SDK Manager.
    2. Scroll to the bottom of the package list and select Extras > Google Repository. The package is downloaded to your computer and installed in your SDK environment at android-sdk-folder/extras/google/google_play_services.

This guide is written for users of Android Studio, which is the recommended development environment.

Get a Configuration File

The configuration file provides service-specific information for your app. Go to Google Developer’s Page, and create a New Project or choose an Existing Project. You will also need to provide a package name for your app.

Before this, we MUST have an Android Project that we are using.  If you don’t have one, create a new project in android studio project, Name the project Demo Login and give it a package name. Choose the activity name as LoginActivity.

Now add app name and package name on Google Developers page as shown below.

Click on Choose and configure services button. Choose Google Sign-In the service page.

We will continue on this page, but first, we have to generate digitally signed a public certificate which will be.

Generate SHA-1 fingerprint

In order to consume google plus services, first we need to enable the Google Plus API on google console and we need to register our digitally signed .apk file’s public certificate in the Google APIs Console.

Java key tool an be used to generate the SHA-1 fingerprint. We had used this earlier, in our Login with Facebook Tutorial, so you should already be familiar with the process.

Generating a Development Key Hash

You’ll have a unique development key hash for each Android development environment. To generate a development key hash, on Mac, run the following command:

On Windows, run this command:

This command will generate a 28-character key hash unique to your development environment.

Generating a Release Key Hash

Android apps must be digitally signed with a release key before you can upload them to the store. To generate a hash of your release key, run the following command on Mac or Windows substituting your release key alias and the path to your keystore:

This will generate a 28-character string.

Copy the SHA-1 ID generated in your terminal as image below

Enter the SHA-1 ID to the Google developer’s page.

Click on ENABLE SIGN IN button.

Click on CONTINUE TO GENERATE CONFIGURATION FILE button. This will open download and install configuration page, click on Download google-services.json button

Copy the google-services.json file you just downloaded into the app directory of your Android Studio project.

Add Google Services Plugins and Google Play Services

The Google Services plugin for Gradle parses configuration information from the google-services.json file. Add the plugin to your project by updating your top-level build.gradle and your app-level build.gradle files as follows:

Add the dependency to your project-level build.gradle:

Add the plugin to your app-level build.gradle:

In your app-level build.gradle file, declare Google Play Services as a dependency:

We also need to add Glide, to be able to load our user’s profile image with ease.

Remember to add the Internet permission in your Manifest File!

Integrating Google Sign-In

To integrate Google Sign-In into your Android app, configure Google Sign-In and add a button to your app’s layout that starts the sign-in flow. Create a simple User Interface that looks like this:

Lets go ahead and start doing some magic to make our UI lively.

Configure Google Sign-In and the GoogleApiClient object

In your sign-in activity’s onCreate method, configure Google Sign-In to request the user data required by your app. For example, to configure Google Sign-In to request users’ ID and basic profile information, create aGoogleSignInOptions object with the DEFAULT_SIGN_IN parameter. To request users’ email addresses as well, create the GoogleSignInOptions object with the requestEmail option.

Then, also in your sign-in activity’s onCreate method, create a GoogleApiClient object with access to the Google Sign-In API and the options you specified.

Note: To use enableAutoManage, your activity must extend FragmentActivity or AppCompatActivity (a subclass of FragmentActivity), both of which are part of the Android Support Library. You can use GoogleApiClient in a Fragment; however, the fragment’s parent activity must be a FragmentActivity. If you can’t extendFragmentActivity, you must manually manage the GoogleApiClient connection lifecycle.

Start the sign-in flow

In the activity’s onClick method, handle sign-in button taps by creating a sign-in intent with the getSignInIntent method, and starting the intent with startActivityForResult.

Starting the intent prompts the user to select a Google account to sign in with. If you requested scopes beyond profileemail, and openid, the user is also prompted to grant access to the requested resources.

In the activity’s onActivityResult method, retrieve the sign-in result with getSignInResultFromIntent.

After you retrieve the sign-in result, you can check if sign-in succeeded with the isSuccess method. If sign-in succeeded, you can call the getSignInAccount method to get a GoogleSignInAccount object that contains information about the signed-in user, such as the user’s name.

You can also get the user’s email address with getEmail, the user’s Google ID (for client-side use) with getId, and an ID token for the user with with getIdToken. If you need to pass the currently signed-in user to a backend server, send the ID token to your backend server and validate the token on the server.

This is how our app will look like initially.

After clicking the Sign In button, the user is requested to choose the account they would like to use:

Retrieve profile information for a signed-in user

Use the GoogleSignInResult.getSignInAccount method to request profile information for the currently signed in user.

You can call the getSignInAccount method after the sign-in intent succeeds.

Note: A Google account’s email address can change, so don’t use it to identify a user. Instead, use the account’s ID, which you can get on the client with GoogleSignInAccount.getId, and on the backend from the sub claim of the ID token.

For additional profile data that might be available, see GoogleSignInAccount. Note that any of the profile fields can be null, depending on which scopes you requested and what information the user’s profile includes.

This is the full source code for the demo app, Login Activity java class.

Sign out users

You can enable your users to sign out of your app, and to disconnect their accounts from your app entirely. To add a sign out button to your app, first create a button in your app to act as your sign out button. Then, attach an onClickListener to the button and configure the onClick method to call signOut.

This code clears which account is connected to the app. To sign in again, the user must choose their account again. Try this in your app, to see how much you have learnt in this article.


Your app is now complete. When you build it and deploy it on your Android device, you will see the Google login button. Tapping the login button prompts you to choose and authorize which account to use, if you have multiple accounts signed in the device. After successfully logging in, the user’s name and profile image will be displayed.

Remember to subscribe to get updates instantly and also leave questions or comments below, and I will be glad to help you.

Happy Coding!

Twitter Login in Android

Twitter Login in Android

Integrating Twitter in your Android application will make user easily login into your app using their Twitter account which avoids filling a long registration forms.

Traditionally, authentication has required the user to complete a registration form, usually creating a username and password in the process. However, with the rise of social networks there’s now a much quicker and easier way of getting users signed into your app: using an account they’ve already created with an external authentication provider, such as Facebook or Twitter.

Twitter Login in Android

In this tutorial, I’m going to show you how to replace the time-consuming and frustrating user registration form, with a simple ‘Sign in with Twitter’ button.

Users who have just downloaded your Android app are going to be a lot happier if you allow them to log in using a popular social networking service instead of asking them to fill out a registration form. In this quick tutorial, you are going to learn how to enable your users to sign into your app with their Twitter accounts.

To follow along, you should have the following set up:

Any app that needs to interact with Twitter’s APIs should be registered using the Twitter Application Management console. Use your Twitter account to log in.

Click on the Create New App button, and fill out the form. Enter meaningful values in the Name and Description fields, because these will be shown to your app’s users. We won’t be needing the Website and Callback URL for this tutorial, but you can’t leave them blank. If you have your own website, you can use its address here. If you don’t,  you can simply type in

Accept the agreement and submit the form. Your app is now registered.

Click on the Keys and Access Tokens tab and make note of your Consumer Key and Consumer Secret. We’ll be needing them later.

Getting Started

The Android SDK is a collection of individual libraries designed to make interacting with Twitter seamless and efficient. This includes:

  • TwitterCore for Twitter login and API access.
  • TweetUi for displaying Tweets and Timelines.
  • TweetComposer for creating Tweets.
  • Twitter + MoPub for monetizing Twitter timelines with MoPub.

Twitter requires that all API requests be authenticated with tokens that can be traced back to an individual Twitter App.

Add Twitter Kit Dependencies

To get started you will need to add the following dependencies to your application’s Gradle config (usually app/build.gradle).

All the Twitter Kit artifacts are distributed through Bintray jcenter. Make sure jcenter is included in your repositories scope. This is default in all new versions of Android Studio.

Initialize Twitter Kit

Once you’ve included the Twitter Kit dependencies you must initialize Twitter Kit. If using a custom Application class you can initialize Twitter Kit in the onCreate() method and initialize with custom configuration. (Create a new Java Class and name it App).

Replace the Consumer Key and Consumer Secret with the ones generated earlier on. Then add it to your Manifest file as shown below.

We initialize it using the “name” namespace, inside the Application Tag as shown above. Remember to also add the INTERNET Permission before you proceed.

Once Twitter kit has been initialized TwitterCore, TweetUi, and TweetComposer can be accessed through their singleton accessor.

  • TwitterCore.getInstance()
  • TweetUi.getInstance()
  • TweetComposer.getInstance()

Log In with Twitter

TwitterCore provides Log In with Twitter. This feature enables application users to authenticate with Twitter.

Before using this feature, ensure that “Sign in with Twitter” is enabled for your Twitter app (

When attempting to obtain an authentication token, TwitterCore will use the locally installed Twitter app to offer a single sign-on experience. If TwitterCore is unable to access the authentication token through the Twitter app, it falls back to using a web view to finish the OAuth process.

The simplest way to authenticate a user is using TwitterLoginButton. Inside your layout, add a Login button with the following code:

The User Interface will look like this: The red widget represents an ImageView, to display the logged in user Profile Image.


In the Activity or Fragment that displays the button, you need to create and attach a Callback to the Login Button.

Next, pass the result of the authentication Activity back to the button:

Using Twitter Button Inside a Fragment

Inside the Activity hosting the Fragment, pass the result from the Activity to the Fragment.


If login completes successfully, a TwitterSession is provided in the success result. This TwitterSession will contain a token, secret, username, and user ID of the user and becomes the active session and is automatically persisted. If you need to retrieve the TwitterSession at a later time, you may do so using the SessionManager.

Request User Email Address

To request a user’s email, call the TwitterAuthClient#requestEmail method, passing in a valid TwitterSessionand Callback.

If the email address is available, the success method is called with the email address in the result. It is not guaranteed you will get an email address.

Before using this feature, ensure that “Request email addresses from users” is checked for your Twitter app.

For example, if someone signed up for Twitter with a phone number instead of an email address, the email field may be empty. When this happens, the failure method will be called because there is no email address available.

Show Twitter Names and Profile Picture

In this example, I will show you how to show the User’s Twitter Name and display their Profile Image in our ImageView, as shown below.


We then click the Login with Twitter Button, we are prompted to Authorize the app to have access to your Twitter account as shown below:


After successful login, we can set the Twitter Username to our TextView as shown below:

The complete code, for the MainActivity looks like this:


You need to add Glide for this code to work, but you can also choose to work with Picasso or whichever Image Library suits you. I would however recommend using Glide. We are using our Twitter Session to get the User object, i.e The Logged In User data and then use it accordingly. In the above example, I am only using the name and the profile picture of the user. You can look at the body object to see what is packed as our response.

This article has been updated, thanks to a request by Raja 🙂 Thanks Buddy! Appreciated


Your app is now complete. When you build it and deploy it on your Android device, you will see the Twitter login button. Tapping the login button takes you to a Twitter page that asks you to log in and authorize the app. After successfully logging in, the user’s name and profile image will be displayed.

In this article, you learned how to use the Twitter SDK to add Twitter Login to your Android app. In the next article, we will look at Google Sign In option, available for Android. Remember to subscribe to get updates instantly and also leave questions or comments below, and I will be glad to help you.

Happy Coding!

Facebook Login in Android

Facebook Login in Android

Now a days almost every web and mobile app provides an option to Login with Facebook. This is because most of the people have a Facebook account and Facebook has provide an easy to integrate SDK to add Facebook Login to your mobile app.

Facebook SDK takes care of user’s privacy as well, When people log into your app with Facebook they can grant permissions to your app so you can retrieve information or perform actions on Facebook on their behalf. Most of the permissions require your review before granting it to the android app.

Facebook Login in Android

Facebook Login in Android provides a convenient and secure way for people to log in to an app without having to go through a sign-up process first. Using the latest version of Facebook’s SDK for Android, it takes only a few minutes to add this feature to your app.

In this quick tutorial, you will learn how to add a Facebook login button to an Android app and handle the events to log a user in using Facebook Login.

Before you begin, make sure you have access to the following:

Open your Android Studio, and create a New Project, or if you have some experience working with Android open your existing Project. I will be creating a new Project in this article and name it Facebook Login, and choose a Minimum API Level of 15, Android 4.0.3 (IceCreamSandwich) or higher and create your new project.

In your project, open your_app | Gradle Scripts | build.gradle (Project) and add the following repository to the buildscript { repositories {}} section to download the SDK from the Maven Central Repository:

In your project, open your_app | Gradle Scripts | build.gradle (Module: app) and add the following compile statement to the dependencies{} section to compile the latest version of the SDK:

Then build your project.

All apps that use the Facebook SDK must be registered with Facebook. Log in to the Facebook Developers website and click Add Product in the bottom left. (Register as a developer, if new). Select Facebook Login option.

Select the Android option and proceed. Go to option 3, Tell Us about Your Android Project option. Enter your package name and enter MainActivity as your default Activity. Save and proceed, you may get an error or rather a warning saying Facebook was unable to verify your package name in the Google Play Store, just click on Use this Package name.

Add Your Development and Release Key Hashes

To ensure the authenticity of the interactions between your app and Facebook, you need to supply us with the Android key hash for your development environment. If your app has already been published, you should add your release key hash too.

Generating a Development Key Hash

You’ll have a unique development key hash for each Android development environment. To generate a development key hash, on Mac, run the following command:

On Windows, run this command:

This command will generate a 28-character key hash unique to your development environment. Copy and paste it into the field below. You will need to provide a development key hash for the development environment of each person who works on your app.

Generating a Release Key Hash

Android apps must be digitally signed with a release key before you can upload them to the store. To generate a hash of your release key, run the following command on Mac or Windows substituting your release key alias and the path to your keystore:

This will generate a 28-character string, copy it and paste it in this section, in your Facebook Developer Page.

Edit Your Manifest File

Create strings for your Facebook app ID and to enable Chrome Custom Tabs, and add them along with the FacebookActivity to your Android manifest.

Open your /app/src/main/res/values/strings.xml file.

Add the following:

Open the /app/manifest/AndroidManifest.xml file.

Add a uses-permission element to the manifest:

Add the following meta-data element, an activity for Facebook, and an activity and intent filter for Chrome Custom Tabs after the application element. Replace @string/appname with the name of your Facebook App:

Create a new layout named main_activity.xml in res/layout. This is going to be a very simple layout with only three widgets:

  • LoginButton to allow the user to log in to Facebook
  • TextView to display the result of the latest login attempt
  • an ImageView to display the Image of the User’s Facebook Profile

You can place them inside a LinearLayout. After including attributes for padding and positioning the widgets, the layout’s XML will look something like this:

The SDK needs to be initialized before using any of its methods. You can do so by calling sdkInitializeand passing the application’s context to it. Add the following code to the onCreate method of your Activity:

Initialize your widgets, as shown below. Declare a CallbackManager as another field. The CallbackManager, as its name suggests, is used to manage the callbacks used in the app and initialize your instance of CallbackManager using the CallbackManager.Factory.create method.

It’s time to create a callback to handle the results of the login attempts and register it with the CallbackManager. Custom callbacks should implement FacebookCallback. The interface has methods to handle each possible outcome of a login attempt:

  • If the login attempt is successful, onSuccess is called.
  • If the user cancels the login attempt, onCancel is called.
  • If an error occurs, onError is called.

To register the custom callback, use the registerCallback method. The code to create and register the callback should look like this:

Tapping the login button starts off a new Activity, which returns a result. To receive and handle the result, override the onActivityResult method of your Activity and pass its parameters to the onActivityResult method of CallbackManager.

The final MainActivity should look like this:

Now, we need to extract the User’s info and show it, that is there Name and their Profile Image. We will add Glide, in our dependencies, to easily enable us load images. Add the following in your app’s build.gradle file:

Add the following to your repositories also, in the project level build.gradle file

Then hit sync gradle option. Lets now get our user’s info from Facebook and display it. We are going to use Facebook Graph API to get the User’s Data. Modify your login button code to look like this:

We then  create a loadUserData method, as shown below:

We are requesting the User’s information from Facebook using the Graph API here. The final code should look like this:

The above code only works after we sign in the first time. We are not storing the User Info in SharedPrefrences here, so if you navigate away from the app, the User data will be cleared on re-opening the app. You can save the data locally using either SharedPrefs or the amazing Android ORM’s available.

This is how the app will look like, when signing in

After successful sign in, the User data is displayed as shown below.

This was meant to show you the basics, you could try integrating the above code in your existing Android Apps to give your users an amazing experience when logging in to your applications. If you get any errors or get stuck, kindly leave a question below and I will be glad to help you out.


Your app is now ready, but we need to make it live, switch from development mode. Open the ‘App Review‘ Tab : change

Do you want to make this app and all its live features available to the general public? Yes. Select the Education category in the drop down options.

And you app will be live now.


Your app is now complete. When you build it and deploy it on your Android device, you will see the Facebook login button. Tapping the login button takes you to a Facebook page that asks you to log in and authorize the app. After successfully logging in, the user’s profile will be displayed.

In this article, you learned how to use the Facebook SDK to add Facebook Login to your Android app. You also learned how to handle the possible outcomes of a login attempt. To learn more about Facebook Login, you can go through the reference for the Facebook SDK for Android.

Happy Coding!

Android Adaptive Icons

Android Adaptive Icons

One of the new features in the Android O developer preview is Adaptive Icons. While it will be a long time before may of us will be in a position to specify minSdkVersion 26, this is actually one of those feature that we can implement now and it will benefit users as and when they get Android O on their device, whilst having no impact on legacy users.

Moreover there are some new icon creation tools in Android Studio 3.0 which actually make developers’ lives a lot easier not only for creating Adaptive Icons, but also for creating legacy versions of those icons as well. In this article we’ll take a look at these new tools.

Android Adaptive Icons

There is already a good explanation of Adaptive Icons in the official documentation which give some explanations of why Adaptive Icons have been introduced, and hint at some of the ways that they will be used going forward. While the documentation already provides an explanation of the icon tools within Android Studio, it is these that we’ll focus on to see how they can provide assistance even for legacy icons.

Android Adaptive Icons, introduced in Android 8.0 (API level 26) can display a variety of shapes across different device models.

Before we continue it is worth pointing out that there a currently really big pain point with backwards compatibility which we’ll get to at the end. I would therefore suggest a quick read through of this article to decide whether it is worth your while implementing these just yet.

The main concept of adaptive icons is that the foreground and background layers are separate. This enables the launcher to do some funky effects. While the launcher doesn’t do anything yet, there may be some interesting stuff coming soon.

For the foreground of the adaptive icon I’m using the default Android Icon, in Android Studio 3.0. Next we have the background, and we will be using the ic_launcher_background.xml file in the drawable folder, in Android Studio 3.0 by default. You can however provide a different Icon and a background, if need be. I am keeping this simple for the sake of this tutorial.

To create the adaptive icon we right click on the res folder in the project view in Android Studio, and select New | Image Asset:

This brings up the all new image creation tools:

The icon type dropdown enables us to specify different types of icon that we may want to create, including ActionBar and notification icons. We’re interested in launcher icons so we’ll stick with the default selection of “Launcher Icons (Adaptive and Legacy)”. Next we’ll change the asset type to “Image” and select our foreground layer in the “Foreground Layer” tab:

You can change the Foreground Layer and the Background Layer to match your desired output. Feel free! Next we switch to the “Legacy” tab which allows us to control the legacy icon types which will be generated:

This is quite interesting because we can actually generate static legacy icons from our adaptive assets. That really simplifies the task of generating both square and round icons after round icons were introduced in Nougat 7.1. In this case I have no need for a Play Store icon (as I won’t be publishing this app to the play store) but I have still generated it. It is however optional.

The final stage is a check of which output files will be generated. The files shown in red are those that already exist and will be overwritten:

The default ic_launcher.png and ic_launcher_round.png icons which were the default from the project creation are those that will be overwritten.

That’s it. All of our assets have been generated. That’s it!


In this article, we have learnt how to easily generate our Adaptive Icons using Android Studio 3.0 (Not supported in earlier versions of Android Studio, consider updating first) and how the IDE helps us package the icons instantly without sweating.

This is one powerful feature of Android Studio, that I love using. Try this in your projects and leave comments or questions below if you get stuck!

Happy Coding!

Working with SVG/Vector Drawables

Working with SVG/Vector Drawables

While developing Android Applications, supporting multiple resolutions are sometime nightmare to developers. Including multiple images for different resolutions also increases the project size.

The solution is to use Vector Graphics such as SVG images. While Android does not support SVGs (Scalable Vector Graphics) directly, with the launch of Lollipop a new class was introduced called VectorDrawable, which allows designers and developers to draw assets in a similar fashion using only code.

SVG/Vector Drawables

Simply explained, vector graphics are a way of describing graphical elements using geometric shapes. They are particularly well suited to graphical elements created in packages such as Adobe Illustrator or Inkscape where simple geometric shapes may be combined in to much more complex elements.

As the name implies, vector drawables are based on vector graphics, as opposed to raster graphics, vector graphics are a way of describing graphical elements using geometric shapes. It is similar to a SVG file. In Android Vector Drawable are created as XML files.

Now there is no need to create different size image for mdpihdpixhdpixxhdpixxxhdpi etc. We need only one vector file for multiple screen size devices. Also for older versions of Android that don’t support vector drawables, Vector Asset Studio can turn your vector drawables into different bitmap sizes for each screen density at runtime.

Open a new or existing Android Studio Project and right click on Res Folder, and select new then Vector Asset as shown below:

This gives you two options, use a Material Icon or a Local PSD or SVG Image

Material Icon

This option allows you to use Android Studio inbuilt icon library, which has a ton of icons categorized for easy selection.  To select a new Icon, just select the Android Icon which is selected by default, then rename the image, with a unique name. You can provide additional dimensions, or rather specify desired icon dimensions.

Android Studio allows you to alter the opacity of the image, using a slider, that you can adjust to obtain the desired opacity. The image is then stored in the drawable folder.

Local File (PSD or SVG)

If you have done some web development, you have possibly used SVG images before. You can download some amazing SVG images here, then try to import using the Local File (SVG, PSD) option in Android Studio

The Local File option gives you the same option as the Material Icon, and is stored in the drawable folder. However the SVG file is basically a vector file that looks like the code sample below:

The vector file is loaded to an Image, as shown below:

To use the Vector Image, add it to an ImageView or as a layout background. Let me show you a simple example, using an Imageview. You can scale the ImageView without loosing quality.

The sample code is shown below, with the above output

You can try playing with some SVG Images and see how your app behaves or works.


In this article, I showed you how to play with SVG Images and Material Icons. In the next article, we will look at the Image Asset using Android Studio 3.0, which is currently in Beta version. Remember to subscribe and leave comments or questions below.

Happy Coding!