Pick contacts from Android Contacts using runtime permission check

Hey Guys! In this post, we will see how to pick a contact from android's contacts app and then show it inside our app. As you might already know, the permission needed for reading contacts, i.e,  Manifest.permission.READ_CONTACTS is  categorised under dangerous permission. So for such category of permissions, it is made mandatory that we request the permission once more at runtime, even though the permission is already added in AndroidManifest.xml. Enough of the details, we will jump into the code right away.

Before we begin just keep in mind that i will be using (data binding/view binding) in this app

Next, fire open your android studio and create a new project with an "Empty Activity" template.

As discussed, enable data binding by heading over to your app level build.gradle and adding the following within android {..}

dataBinding {
    enabled = true}

Now that we are all set with binding, we will see how to setup our layout file. So within your layout folder, find your activity's layout(named as activity_main.xml if you kept the activity name as default during project setup) . Now make the following changes to it
You can see that i have converted the above layout to a data binding layout by wrapping the root layout with a "<layout>" tag. 
Also, you may choose some images to be set to your AppCompatImageButton, you can either get the one i used by downloading the source code below, or grab some free images from google. Now you will be able to reference the views defined in this layout, directly by using their id without using findviewbyid method. Also, you need to add two edittexts - one for contact name and another one for contact phone. Also create a button to pick the contact from your device contact app.

Now update your MainActivity.java file as shown below


As shown above, you can initialise data binding library by using 
binding= DataBindingUtil.setContentView(this,R.layout.activity_main); and then use it to reference views like binding.nameEdtbinding.phoneEdtbinding.pickContact etc

As shown above, you can check for runtime permissions when the button is clicked, then if the permission is already granted take the user to the contacts picking screen, otherwise show the permission dialog. If the user grants the contacts read permission through this dialog, take him to the contacts picking screen, otherwise if he rejects, then prevent him/her from picking the contact.

Now that you are all set, if you try running the project you will see a similar output as shown below.

Picking contacts after checking runtime permission


Checkout full project source code here

Understanding the android build process, what is Dalvik(JIT),ART(AOT),D8,R8?

Android Build Process Steps

Some of you might be already aware of JVMs, for those who are not aware, these are just runtime environments to execute java applications. These environments basically helps us in converting the compiled java code to machine code.

Coming to the android side, we can't use he JVM directly since JVMs are designed for machines with high computational power, storage etc. Android devices, when compared with these machines offers limitations in a lot of aspects like small RAM availability, limited storage etc.

In order to make the JVM compatible with android devices, necessary changes where made and hence Dalvik was born. Dalvik continued to be the default runtime environment for android versions till 4.4 kitkat. Dalvik followed JIT based compilation process, which means that code is complied Just - In - Time when the app is opened each time. Here the app gets compiled to machine code each time the app is opened.

Android Runtime replaces Dalvik

The JIT based compilation comes with a lot of disadvantages considering battery drain, app stuttering/lag etc. This lead to the development of a new JVM, which is the The Android Runtime(ART). ART follows Ahead - Of - Time (AOT) based compilation process. As the name suggests, here compilation happens before app start. Here the compilation process happens during the app installation process itself. Even though this lead to higher app installation time, it reduces app lag, increases battery usage efficiency etc. Also the ART environment demands very high app size/storage in comparison with dalvik. But above all ART offers great speed and performance, which is why it was made the default runtime environment for android starting from lollipop onwards.

Even though dalvik was replaced as the default runtime, dalvik bytecode format is still in use(.dex)

A new runtime was introduced in Android Version 7.0 Nougat, which offers a hybrid environment combining features from both a JIT compiler and ART.

Now that we have had a brief understanding of JVM, dalvik,JIT,ART,AOT etc, we will see more about what happens during an android build process.


java bytecode(*.class) 

Take a look at the above diagram, you can see that, at first the java source code is converted into java bytecode using the java compiler(javac). The java bytecode will be having .class extension. This java bytecode is stack based, ie, all variables are stored in a stack.


dex bytecode(*.dex)

Next, the java bytecode(*.class) is then converted into a dex bytecode. This conversion is done using the android dexer, which use to be the historic "dx". But starting from android studio 3.1 onwards, D8 was made the default compiler. D8 produces smaller dex files with better performance when compared with dx. dex files generated with d8 offers significantly higher runtime performance when compared with dx.


The hence obtained dex bytecode undergoes minifying using proguard. The result is still a dex file, but minified. Further this dex file is used to build the apk and finally deployed on an android device.

desugaring and the advent of D8 dexer

Before we go through the D8 dexer in detail we must know what caused its formation. You must have used the jack toolchain briefly which helped us in supporting java 8 language features on android, but it was deprecated because of its direct conversion of source code to dex bytecode.

As a result, android's default toolchain was updated with java 8 language features like the super cool lambdas. Wait.., but the android runtimes(dalvik or ART) won't support java 8 features. Yes, you guessed it correct - it won't, but the updated android toolchain supports a feature just to overcome this big hurdle - Desugaring. Since the android runtime won't support java 8 language features, desugaring can be used to convert java 8 bytecode to java 7 bytecode.

Hence we can now use java 8 features like lambda functions on android.

As i said before, D8 offers significantly higher runtime performance when compared with dx and it also helps in generating smaller apk files. Now we will see how to integrate desugaring with D8. As seen before, in default toolchain, desugaring is a separate step which runs before the android dexer is executed. With D8, we can integrate desugaring with along with it. The integrated desugaring
will further improve the build time

Finally,What is R8?

R8 basically provides all the features offered by D8, but with a lot of add on features. So R8 can be called a successor to D8, but not exactly. Confused? Don't worry, lets see what all features does R8 offers.

Apart from supporting java 8 language features like D8, R8 also enables further optimisation to the dex bytecode. As of now, proguard is run as a separate step in the build process, but R8 provides features like optimisation, obfuscation, remove unused classes etc(which proguard already offers) within itself. Even though R8 only offers a subset of features provided by proguard, now we can perform the entire java/kotlin bytecode to dex bytecode conversion process within a single transformation itself(further improves build time), which is - R8. Hence you must always keep in mind that R8 is not a complete replacement to proguard.

You can enable R8 by adding the following in your app's build.gradle

android {
 buildTypes {
                       release {
                                    useProguard false  
                                              minifyEnabled true
                                    proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
                                   }
                   }
 }


Android viewpager with tab layout example

In this post i will walk you through the setup of a tab layout with view pager, the final output will be something like the one shown below. We will also see how to pass data to the viewpager fragments from the activity.

viewpager with tab layout
Before we begin, couple of things that we will be following in this project are

  • The app will be built using androidx packages
  • The app will be using Java 8
  • The app will be using data binding (part of android jetpack components)

Now that we are all set to begin, let us open up android studio and create a brand new project with androidx artifacts.

androidx artifacts are configured by default when you create a new AS project
Now that your project have successfully synced with gradle, head over to your app level build.gradle and enable java 8 by adding the following code

compileOptions {
compileOptions {
targetCompatibility JavaVersion.VERSION_1_8 sourceCompatibility JavaVersion.VERSION_1_8 }
}
Also, you can enable data binding by adding the following to your app level build.gradle

dataBinding {
enabled = true}

Also add the following material dependency inorder to support tab layout

implementation 'com.google.android.material:material:1.0.0'

Now the resulting build.gradle will look like the following

Now that the initial setup is done, we will jump into the coding section right away.

First of all, in the project explorer, right click on your package directory, then create a blank fragment like shown below

create new blank fragment

Now that i have added the blank fragment, head over to your activity layout(acitvity_main.xml in my case) and convert it into a databinding layout by wrapping the root layout with <layout> tag, so that the resulting layout will be 


Now head over to your activity's java file (MainActivity.java in my case) and initialise binding variable by adding binding= DataBindingUtil.setContentView(this,R.layout.activity_main);, now you can get the binding variable for this initialisation by making slight modification to your layout file name, so in my case acticity_main becomes ActivityMainBinding. So initialise your bindining variable like this private ActivityMainBinding binding;

Now inorder to assign different fragments  in viewpager, we will need a viewpager adapter, for that go inside your MainActivity.java and add a new adapter class like shown below


If you check the activity_main.xml above, you can see that i have already added viewpager and tab layout inside it. I have also assigned the id's tab to tab layout and viewPager to the viewpager. Now is the part where view binding comes into play, can reference the viewpager and tab layout in our java code simply by using binding.viewPagerbinding.tab etc.

Now we can easily pass our viewpager to the SamplePagerAdapter and also setup our viewpager with the tablayout. Inside the SamplePagerAdapter's getItem() method you can return different fragments, which will be showing up inside your viewpager as different tabs. You can also return the titles for each tab in the getPageTitle() method

Now the complete code for MainActivity.java will be like
everything is now set and you will be able to see the tabbed viewpager if you hit the run button.
Download full source code below :

Android data binding tutorial for absolute beginners

Hi guys! In this post we will discuss how to get started with data binding in android for absolute beginners. As some of you are already aware of,  data binding is actually part of Android's Jetpack components. By implementing data binding, you can get red of a lot of boilerplate code in android development. For example, you can completely get rid of all the messy view declarations using findviewbyid, setting text using settext() method etc.

So without further ado, we will jump into the coding part right away. First of all open your android studio ide and create a brand new android studio project with an empty activity as initial setup.

Next thing you will need is to enable data binding in your android project, which can be done by adding the following in your build.gradle file.

dataBinding {
 enabled true
 }
now hit sync now button in top right corner like shown below



Now you will need to wrap the root layout of your activity(activity_main.xml) with the <layout> tag. You can do this either manually or just click the root layout of your activity's layout file to bring up the yellow bulb icon, clicking on which brings up the option "convert to data binding layout" and finally click on this option like shown below



As a result of selecting the above option, the layout will be converted like shown below



Now we can see that there is a <data> layout which is empty, so go ahead and add a <variable> tag inside it with some greeting data, say "Hello World!", so that the layout looks like :



Now the finishing touch to be made to our layout is to bind the greeting text variable to our textview, which can be done by adding @{greeting} to the textview's text attribute, also give an id to your textview, which we will later need in our layout's corresponding java file. So the final layout will look like the following :


Now that our layout setup for our data binding sample is done, let us head over to the java code. So, head over to your MainActivity.java file and initialise data binding by replacing setContentView(R.layout.activity_main); with binding= DataBindingUtil.setContentView(this,R.layout.activity_main);. As you are thinking right now, i have not yet declared the binding variable yet. Yes, I will show you how to do that.

Basically binding should be declared using a class that automatically gets generated as soon as we change the root layout of our layout file to <layout>. The so generated binding class will help us to identify various view items in our layout using the view's id.

For each layout with <layout> tag, the binding class will be generated in such a way that its name is generated from your layout file's name itself. It will have its name as all underscores removed and converting the letters succeeding the underscores as upper case, so if your layout file has a name like activity_main.xml ,then its corresponding binding file will be ActivityMainBinding . Now inside your onCreate(), below data binding initialisation, add this binding.setGreeting("Hello World"); to complete data binding process , see below for complete code,

You can see that the setter setGreeting() has got generated automatically since we have previously declared the variable "greeting" inside out layout file . Finally run the app to see the "Hello World" text printed on your screen.

Easily share text from your android phone to pc and from pc to phone

Hi guys, in this post i would like to introduce you to one of my apps which could help you in easily transferring text from your android phone to a pc or any other device which has a web browser. So without further ado, we will checkout the app.


The app is called Reader'S Checkpoints (RSC) and you can download it for free from playstore by clicking the below button
Get it on Google Play

SENDING TEXT FROM ANDROID PHONE TO PC

Once you have finished installing the app, you can select a random text from any other app like google chrome, notes app or any other app which allows you to copy text.

"SEND TO PC" option appears after installing Reader's Checkpoints(RSC)


Now, once you select the text, you will see one more option along with copy, paste options. This new new option will be "SEND TO PC" as shown in above image. This option comes as part of Reader'S Checkpoints (RSC) app.



Once you click this option the QR code scanner will open. Now open the website http://rscweb.in and scan the QR code that appears on this website using the scanner that just appeared on your smartphone.




As a result of the scan, the selected text will be instantly shared to your pc's web browser



SENDING TEXT FROM PC TO ANDROID PHONE

First of all copy some random text on your pc and then head over to the website http://rscweb.in. In the website click on "SEND TEXT TO PHONE"

Now you will see a field to enter the text which needs to be send to your android phone, paste the copied random text here and then click on generate QR, a random QR code will be generated which we will need to scan from our Reader'S Checkpoints (RSC) app, as a result of which the copied text will be available in our android phone instantly.



Inorder to scan the qr code from our app, first of all fire open Reader'S Checkpoints (RSC) app,  then click on add new note "+" icon on the bottom, and the in the further screen that appears click on the pc icon that is present on the top right corner to fire open the qr code scanner as shown below.



You can see that the text from your pc has been share to your android phone as soon as the scan is complete.

Android Recyclerview Tutorial with SQLite ROOM : using MVVM, Dagger 2

What we will be building ?







A simple note taking app, which will have option to add notes and delete it. This will be a minimal design tutorial and hence we won't be spending much time on the app UI. This tutorial will be more focused on the MVVM, dagger2 and building an overall app architecture. This app will be improved in upcoming tutorials with more features, so make sure to stay tuned for that as well.

What are we will be covering ?

We will be using Java 8 to get access to various features like lambda functions, so we will need to enable this in app level build.gradle

We will be making use of data binding, so we will need to enable this also on your app level build.gradle

We will be using Dagger2 for building our dependencies

We will be having a base activity class which will be responsible for performing redundant operations like dependency injection, data binding, setting viewmodels for activities etc. So basically this app structure is developed with more activities to be incorporated in future.

The main reason for choosing MVVM and live data is that, with this pattern database operations like insert, delete gets effortlessly simple. We just need to observe to the database changes and as a result all the UI changes is reflected on the go. There are more advantages to this pattern, we will discuss these while moving forward.

Getting started with project building :

First of all, open your android studio, in file go to new>> new project and then create a project with empty activity

Dependencies Required ?

First of all update project level build.gradle as show below. Here we have set the versions for some dependencies that we will be using in our app :

Now go ahead and update your app level build.gradle with  dependencies for recyclerview,cardview,dagger,room,livedata etc as shown below :


As shown above we have enabled databinding by adding this

dataBinding {
enabled = true}

Also enable java 8 as shown below

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8}


Now let us jump into the coding session right away. Now, since we are making a notes app we will need to make a note model class. So in terms of SQLite ROOM, we will call this an entity. Entities in room are just another java class which is annotated with @Entity annotation. Annotating a class like this will help room in identifying it as an SQLite table. So now our room entity/table will have an notesId field which will be auto incremented on insertion, a notesTitle field and a notesDesc filed. We will be naming our table as notes_model which will be provided in brackets along with the @Entity annotation. So create a class like the following and name it NotesModel.java

Now go ahead and create a DAO class, by annotating it with @Dao. This class will basically act as an interface which will query the sqlite database as per our request. It will contain the various sql queries like insert,delete etc, now name it as NotesDao.java


Now it is time to create our database class, which should be annotated with @Database. Inside the annotation arguments, we can provide an array of entities where we can provide a list of tables/entities that we need inside our database, but for now we will be giving a single entity/table name which is NotesModel and we will keep the database version as 1 . So create the database class as follows :

It is now time to create our NotesRepository which will provide us with the live/updated database changes. It is where we define our live data methods from our db which will be later observed in activity. So create the repository as shown below and name it as NotesRepository.java

You might have noted the @Inject in the constructor which means that we will be getting these dependencies using dagger2, we will see how to create these dependencies using dagger2 later in this tutorial

Now create an abstract class BaseViewModel from which we will be extending all the viewmodel classes

Now we will need tocreate a viewmodel class for our MainActivity, so create MainActivityVM.java  like below

Now we will be creating the BaseActivity.java and make it abstract from which all our activties will be extending from. Also our BaseActivity.java will accept generic params for ViewDataBinding,BaseViewModel etc which means that the respective activities will be supplying our base activity with these params as required so that all databinding, setting viewmodels etc happens in the baseactivity itself.

We will also be using abstract methods for getting binding variable,activity layout, and viewmodel from respective activities. These abstract methods are getBindingVariable(),getLayoutId(),getViewModel() respectively.
now that we have all required attributes for databinding and dependency injection, we can create BaseActivity.java like following :


Now update your layout file which is present in res>> layout>>activity_main.xml , as shown below :

Now add a new layout file for the add new note dialog and name it custom_dialog_with_btns_view.xml


Now goto the project explorer on left hand side of your screen and right click on your package directory and create a new package named "di". This is to organize all your dagger files in this directory, it is optional to create a directory like this but it is highly recommended so that the code becomes more understandable.

Now right click on the di directory, and create a new abstract class and name it ActivityModule.java

In the above class you might have noticed the annotation @Module  which means that this class is a module and in this case this module will be used to provide various activities in our app like MainActivity. All the future generated activities must be defined here.

Now in the same directory - di, we will need the custom annotation ViewModelKey to generate our ViewModelModule.java . So first create interface ViewModelKey.java as follows

Now in the di directory, create another abstract class with @Module annotation and name it as ViewModelModule.java . This module class will provide all the ViewModels in the project. All the ViewModels should be defined here


Create another @Module annotated class AppModule.java which has included module as ViewModelModule.class as follows

Now you will need to design the view for note item, so go to res>>layout, right click on layout directory and click create new layout resource file and name it notes_item_view and add below contents to it

In the above layout for the recyclerview adapter, we have used data binding to set text into the textview. Basically, inorder to perform data binding, we need to make the root layout as "<layout>", also you will need to import the NotesModel and assign it to a variable as shown below.
<data>
<import type="com.example.recyclerviewsqliteroomcrud.persistance.NotesModel"/>
<variable
name="itemModel"
type="NotesModel" />
</data>
Now create a recyclerview adapter like shown below, also you will need to bind the above layout with this adapter in the viewholder

Now update your MainActivity.java  like show below



As shown above we have injected the MainActivityVM. Also we have returned the viewmodel(MainActivityVM) and layout file(activity_main) to the BaseActivity so that we can perform databinding and viewmodel setup inside the BaseActivity itself. This process will apply to all future activities that may be created in the app.
Now we will need to create a component class which will be used along with the earlier created module classes to generate the dependencies using dagger. So create AppComponent.java as shown below :


Now as a final step,we will need to generate the application class which will initialize dagger to build the necessary dependencies. So create BaseApplication.java

As soon as you copy the above file you may notice that DaggerAppComponent is given in red and shows error. This is because dagger has not yet generated the dependencies, inorder to do that click on File>>Sync project with gradle files . If everything goes well, the error will be gone and you may then organize the project structure as shown below(optional)


Finally, go to AndroidManifest.xml and update the application name as .BaseApplication as shown below



How to create github repository from android studio without using a web browser or command line ?


Hi guys in this tutorial we will see how to create a github repository from Android Studio. For this purpose we will not be required to open command line or the web browser. Entire process from repository creation to your first push will be completely carried out on your android studio IDE, provided that you already have git installed on your pc and that you have a valid github account.

It significantly reduces the hassle and also saves a lot of time since we won't be spending much time on writing the commands or be going to the github website to create new repository. All the hassles will be handled by android studio.

So Android Studio will help us in creating the repository, adding all the files for commit, commit the files to be pushed and finally push the project to the github repository created.

Before you begin :

1) Download and install git if you haven't already done.
for mac OS : https://git-scm.com/download/mac
for windows : https://git-scm.com/download/win

2) go here and create a github account >> https://github.com/

Now follow the below steps to create a github repository in the above created account from android studio :

1) First of all open up android studio and create a brand new android studio project. Once the project has finished the building process, goto the VCS option on the top menu as shown below

VCS option appears in the top menu

2) From the above VCS option navigate VCS > Import into version control > Share project on Github like shown in the image below

Share project on Github
3) Now we will be asked to login with your github credentials before sharing the project on github. We will be required to perform this only once since we will be generating an API token during our first login and then will be using this saved token value to login further. So first of all click on "Create API token" to generate one.

Click on "Create API Token"
4) When "Create API Token" is clicked, it will bring up the following screen to login into github. Enter the credentials for github and click login, it will generate the API Token like shown below.

Login here with github credentilas
Generates token on successfully login

5) In the above login screen click the checkbox "Save Credentials" so that we won't have to do this process each time we try create a repository. Now click on login button again. You will be successfully logged in and taken to the following screen
Entering repository details
As shown above you will be prompted to enter a name and description for your repository, keep the remote name as remote. You can also make your repository private, which was earlier a paid feature in github. Now click on share button.

6) As soon as we hit share button, we will be asked what all files are needed to be added to git, since this is our first commit we can select all files like shown below, doing which will make all your files ready for commit

Initial commit to git

As shown above select all files (already selected by default) and enter a commit message. Now click OK button. As soon as we hit OK button we can see that all the files get committed and finally pushed to the remote github repository, you will also be presented with a link to your repository at the bottom of android studio like shown below

Github repository created
Click on the link shown in the image above and you will be taken to your repository in gihub like shown below

Github repository opened in browser