Dedicated Server

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 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

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 ''

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 ( 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 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 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 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 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


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 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


First of all copy some random text on your pc and then head over to the website 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

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

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

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  like below

Now we will be creating the and make it abstract from which all our activties will be extending from. Also our 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 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

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 . So first create interface as follows

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

Create another @Module annotated class 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.
<import type="com.example.recyclerviewsqliteroomcrud.persistance.NotesModel"/>
type="NotesModel" />
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  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 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

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

You may also checkout the below video tutorial which shows how and example with recyclerview,mvvm,dagger2,retrofit2

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 :
for windows :

2) go here and create a github account >>

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

Android Studio git tutorial using github, bitbucket

Hi guys! In this tutorial we will see how to work with git in android studio. We will be showing how to setup git using both github and bitbucket.

Git is basically a distributed version control system for tracking changes in source code during software development. Repository hosting services like github, bitbucket etc are used for version control using git.

We will be breaking down the tutorial into the following steps :

1) Downloading and Installing git
2) Create an account on github/bitbucket and then save the credentials to your git configuration
3) Create a remote git repository using github/bitbucket.
4) Create an android studio project and initialise git within the project.
5) Add the remote repository as the origin of your android studio project.
6) Commit and push your code into remote repository(github/bitbucket)

1) Downloading and Installing git

  • For mac users first of all go to your terminal and check if git is already installed by typing the command git --version , if you get a version value like shown below you already have git installed and you are good to go. If you don't have git installed, you will be getting a message like "command not found", in that case go here >> install git on your mac
git version check

  • For windows users check if git is already installed by typing git --version in the command prompt, if you are able to get a proper version, then you already have git installed. If you don't have git installed, go here >> and complete the installation

2) Create an account on github/bitbucket and then save the credentials to your git configuration

  • If you are working with github, go here >> and create an account(signup)
github signup

  • If you are working with bitbucket, go here >> and create an account(no need to signup for both github and bitbucket, choose only one which is convenient to you)
bitbucket signup

  • Once you have created the account in one of the repository hosting services, you will now need your local git version control system to be connected with it. For that, go to your terminal and type git --version to ensure that git is properly installed. It should be displaying proper git version number, if not try installing git properly as discussed above. Now, execute the following command in your terminal(mac) or cmd(windows) to connect your remote repo with your local git.
git config --global "" 
git config --global "your_github_or_bitbucket_username"
  • As shown above you will have to specify your registered email and username in above commands. Now when you make commits and other activities to your remote repo(github/bitbucket), your identity will be shown based on these credentials

3) Create a remote git repository using github/bitbucket.

  • Now that you have properly setup your remote repo and then configured it with your git version control system, you will need to go ahead and create a new repository in github/bitbucket.
Now you will need to choose which git repository hosting service you are going to use. You don't need both, choose any one - github or bitbucket ?

  • For github, login with your account and click on the plus button at the top right corner which brings up an option "new repository".
github - create new repository
  • Click on "new repository" and enter the details to complete repository creation. You can either make your repository "public" or "private" as well. When made public, it can be accessed by anyone across the globe whereas private repositories can be accessed only by select individuals whom you choose to share it with. Private repositories were a paid feature in github before, but now it has been made free, thanks to github!
  • Once the repository has been created successfully, it will bring up a screen like shown below
unique link to your github remote repo(highlighted with red marker)
  • Copy the command that starts with "git remote add origin ..", which is the unique link to your remote repo. This command will be used in our android studio terminal to connect your local repo with the remote repo that we just created.

  • For Bitbucket, repository creation is similar to github and is straight forward(If you are using github, you are free to skip this step). After logging in to your bitbucket dashboard, you can see a '+' button on the left sidebar, click on it and then select repository from next window to create a new bitbucket repository.
create new bitbucket repository
  • Give a name to your repository and similar to github here also you have option to make it public or private. Choose private if you want it be accessed by people whom you give access only. Now complete repository creation
  • Once the repository has been created you will see the following window from which you will need to copy the remote repo url at the bottom of the screen as shown below
unique link to your bitbucket remote repo(highlighted with red marker)
  • The above copied url will be used in android studio terminal to connect local git repo with the bitbucket repo that we now created

4) Create an android studio project and initialise git within the project.

  • Now start up your android studio. First of all create a new android studio project, finally when your project gets properly build click to open the terminal window at the bottom of the Android Studio IDE.
android studio terminal
  • Now type the following command in your android studio terminal to initialise git within your android studio peoject and hit enter
git init
git init command executed in android studio terminal

5) Add the remote repository as the origin of your android studio project.

  • git is now successfully initialised in your project. Now you will need to get the repository url that we copied earlier while we created our repository. For me it is 
    git remote add origin
  • This url will be different for your repository, so make sure to copy the right url from your repository. Now paste this inside your android studio terminal and hit enter. If everything is fine, we won't see any error and hence we have successfully linked our local repo with the remote repo.
If you are asked to enter the password, please enter the password for your github/bitbucket account, you wont be shown a password preview, but keep typing and then press enter
  • Now we will see how to move your repository from your local machine to the remote github/bitbucket repository. For that, first of all we will need to add all the files that we need to move to your remote github repository. Since we are starting with a new project let us add entire project( all the files) to github/bitbucket repo. So type in the following command and hit enter
git add .
  • As a result of the above command the entire project gets added(i.e, gets prepared for commit). Here '.' means you are going to add all files to remote repo. Instead if you need to add only a single file, you will need to execute something like this git add . For now we will be doing git add . since we will have to move entire project to our remote repo(github/bitbucket)
  • Before we push our project to github/bitbucket, we will need to commit the changes with a commit message. Since we are making an initial commit for our project we can keep the commit message as "initial commit"(of course, you may set a different message of your choice!). Always make sure to keep a meaningful commit message to all your commits. This helps a lot since it becomes a lot easier to identify changes in each commit at a later point of time. Now execute the following command to commit all the files that have been added.
git commit -m "initial commit"
committing all the added files to git

  • As soon as the commit command is executed, all the files gets saved in your local git. Now we are ready to "push" our changes into our remote github/bitbucket repo. So execute the following push command to push all the committed changes into the remote repository's master branch
git push origin master
pushing committed changes to github/bitbucket remote repo 

  • Now if we go ahead and check our repo in github, we can see that all the files have been successfully pushed like shown below
files pushed successfully

  • Now if we check the "commit" tab you can see your "initial commit" is shown there with the person who did the commit and the date in which the commit has been made. Hence it becomes helpful in identifying a commit very easily.
  • Now go inside one of your files and make a small change for testing purpose (below i'm adding a comment inside file)
added a test comment to commit
  • Now go ahead and commit this change by running the following command in android studio terminal
git commit -a -m "test commit"
  • As a result of the above command all the changes get added and committed with the commit message "test commit". This command is basically a shortcut to both add and commit the changes in a single command. (You can alternatively type git add . and git commit -m "test commit" separately also)
  • Now again execute the push command like shown below
git push origin master
  • Now if you check your repository in github, you can see that the changes have been reflected there also.
test commit successfully pushed
  • Now if you check the commit tab you can see that our new commit has appeared there as well, see image below

all commit messages
  • Thats all for this tutorial guys, i will try to make more tutorials on git like this later