Saturday, March 16, 2019

Integrating Google Sign In in Android Application

Configuration of your project in Google API Console:

Following steps you have to follow before you start coding,

Create a new project in android studio. Now, in your app-level build.gradle file, add 


implementation 'com.google.android.gms:play-services-auth:16.0.1'
  
Now sign in to your google account from browser and open configure your project

At this point, you have already dependency in your gradle. So jump to the next step i.e.

Click on the Create a project




Now, if you have already created a project in google API Console and want to integrate google Sign In in that project then open drag down list and select existing project. Otherwise you can create a new project.




After clicking next, it will ask you the product name. Name your product whatever you want and then it will ask you where you are calling from. Here, we are creating an android application so we will opt Android.




So, next console will require details regarding your application package and SHA - 1 key.
You can copy your package name from your AndroidManifest.xml file.



Now, to generate SHA - 1 key for your project, do the following,

Go to Gradle that is present at right side in your android studio window, click on it. So it will show a tree structure. Select ApplicationName → ApplicationName (root) → Tasks → android → signinreport Now, run that signinreport by double clicking on it. Now you can see that SHA - 1 key is generated in Run windnow. Copy that key and paste in google API Console. 





Click on next and then download configuration file. Now paste that credentials.json file into your app directory. 


So, now prerequisites steps are completed Now you can jump to coding portionn. Before that include below dependency to your app-level build.gradle file, which is used to load image from given url.

implementation 'com.github.bumptech.glide:glide:3.6.0'  

Coding part:

activity_main.xml:



MainActivity.java:




activity_profile.xml:




ProfileAcitivity.java:




Now, add <uses-permission android:name="android.permission.INTERNET"/> in your AndroidManifest.xml file.

Run the application now. It will look like this,



Well, I hope you understand this and let me know if you have any doubts :D

Monday, March 4, 2019

Create Splash Activity in Android Studio


What is Splash activity

Splash Activity is nothing but an activity which pops up whenever an android application starts running i.e. whenever you open the application. For example, apps like Facebook, Twitter, etc are having this kind of activities. I am hereby, attaching screenshots of splash activities of both Facebook and twitter so you can get the exact idea.


facebook splash activity
Facebook Splash Activity

Twitter Splash Activity
Twitter Splash Activity (Night Mode)

Well you might be wondering that apart from these two apps, many applications are not having such kind of activity. To be clear, an activity is something like whatever user sees in application i.e. User Interface. It includes background process too, which are obviously invisible to user. So talking about Splash activity, it is not mandatory that every android application should have activity like this. It depends on developer and applications's requirements.

How we can create Splash activity

Let's assume that you are working on a project and now you want to add splash activity in your project. So you have to replace the very first activity of your app with your current MainActivity. You can do the same by editing AndroidManifest.xml file, which we will do later onward. Now, create layout resource file using following steps: Go to layoutNew Layout resource file.

Now, create the design for splash activity whatever you want, just like any normal layout xml file. Now create a java file which will set the content of your newly created xml file for splash activity and after sometime it will launch the main activity of your application.

splash_activity.xml:


MainActivity.java:



You can go through the java code which contains comments regarding functionalities of respective code snippets. Here, you might be wondering that why I have used thread in the code. As you know that, threads are used so that application keeps running multiple threads (threads - individual execution of a program) concurrently. So here, we want Splash Activity to run for some particular amount of time or duration. Here it is provided by a static variable, SPLASH_ACTIVITY_TIMER. Which is 9000 i.e. 9000 milliseconds. i.e Splash Activity will be visible for 9 seconds and after that Main Activity will be launched.

Another thing, if you have noticed that most of the applications' splash activity doesn't contain title bar or action bar. So, to do that we are using requestWindowFeature() method and parameters of WindowManager class. 

One thing to keep in mind that don't forget to finish the thread which runs the Splash Activity, after launching the MainActivity.

Most crucial part of this (AndroidManifest.xml), make changes as per shown below:


These changes are required because before this your application would show the MainActivity directly as initially MainActivity was your main activity. But now it's replaced with your Splash Activity. So the final output will look like this,

Splash Activity 

Hope you get this. If you have any doubts or queries then let me know in the comment section. :D  


Tuesday, February 19, 2019

Gradients in android

How to create gradient in Android Studio?


Here, we are talking about a drawable with a gradient for buttons, backgrounds,etc. So, it is basically an XML document which defines background - color effects for buttons and backgrounds. First of all, you will need android studio installed on your system. Then, create a new project in which you want to provide background - color effect to any particular button. Here, I have already created a project. So I will directly show you required coding snippets. 

First of all, in your project structure, create a new drawable resource file (XML file) in following path: res → drawable by right clicking on drawable and then select New → Drawable resource file as shown below.







After creating new resource file, replace root element i.e. <selector> with <shape>. shape element is having several attributes like, startColor, endColor, centerColor, angle, etc. You can find more information about it on https://developer.android.com/ Now, in your newly XML file apply changes as following,






Now, to apply this background effect, in activity_main.xml set  the background property as "@drawable/filename". For example,





So, it will generate following effect to background of your activity_main.xml






Same, procedure one will have to follow if he/she wants to apply background effects to buttons or image buttons.

Hope you like it!!! :D