fbpx

Retrofit basics – Preparation

Reading Time: 11 minutes

We are going to start the Retrofit basics tutorial with the preparation of the sample application. In every case, when you start a new project, you have to make shorter or longer preparations before you can start implementing the business logic.

In this chapter first, we will start a new project in Android Studio, then we will add the needed dependencies. Thenafter, we will create the four fragments, the toolbar and the navigation graph, and some more smaller things 😊. The last thing will be a base class for our app.

Jump

In this chapter we won’t talk about Retrofit, we will work only on the preparation of the sample application. If you want to start the tutorial immediately with Retrofit, then jump to the next chapter.

There you can download the project from GitHub , what we are going to prepare in this chapter.

BUT, we highly recommend you to do this chapter as well. Why? Because, if you want to be a good programmer, then you have to practice all the time, and this chapter is a good opportunity for this.

Step 1 – Create new project

After a short introduction, our first step is to create a whole new project. For this, launch Android Studio and create a new project. If you see the “Welcome page” of Android Studio, then click on the “Start a new Android Studio project”.

If you have an open project, then from the “File” menu select “New”, then “New Project”. After thet, this window will popup.

Here select the “Empty Activity” option.

In the next window, we should provide some information about the app. We name the new project as “RetrofitDemo”.

As always, this tutorial will be written in Kotlin. So, select from the dropdown list the Kotlin language.

From the next list of the “Minimum SDK” select API 21. In our case API 21 gonna be enough.

There is a link, called “Help me choose”. If you click on it, then a colorful figure will show you the different Android versions and their imported innovations.

If you are done, click on the “Finish” button. The build can take few minutes. Be patient! 😉

When the build has been finished, then you should see the open MainActivity:class and next to this the activity_main.xml files. For now on you can close both of them.

Step 2 – Add the needed dependencies

Next step is to enable for the project the dependencies, what we are going to use.

So, to add them to the project, go to the top left corner of Android Studio. There will be the tab called “Project”. Click on it and change the view to Android. Then open the Gradle Scripts menu and double click on the build.gradle (Module: app) file.
Set the target JVM

Now, scroll down a bit, until the end of the android {} section. There paste the below lines.


Target JVM

We need to use JVM 1.8, otherwise we can’t use use the Navigation Componenets library.

Enable Data Binding

Also here, after the JVM lines, paste the below lines also. These will enable in the project Data Binding.


Enable Data Binding
Dependencies

Next is to add the dependencies. They are going to the dependencies {} section of the build.gradle (Module: app) file.

Copy and paste the below lines at the end of the dependencies.


Dependencies
The versions
The last step before we can synchronize the app is to define the versions of the above mentioned dependencies. We are going to do it in one place, which is located in the build.gradle (Project: app) file. You can find it next the build.gradle (Module: app) file.

At the top of the file, inside of the buildscript{} section you should have the version of Kotlin.

After it paste the needed versions. Then the versions should look like below.


Versions
You can find the current version of Retrofit under its official website: Retrofit
Sync the project

Finally, it is time to finish this step. So, click on the “Sync Now” text, what you can find at the top right corner of Android Studio.
Depending on your computer, it can take some minutes, so be patient. 😊

Step 3 – Create the fragments

As we have talked about in the intro of this chapter, we will have four fragments in our sample app. Currently they won’t have to much code, only its bindings.

Download the assets

The first thing what you should do, is to download some files for this app. The next .ZIP file contains

      • the logo of WordPress
      • the needed strings in the string.xml file
      • the color in the colors.xml
      • the dimens in the dimen.xml
      • the menu for the main screen
      • the layout of the item for the RecyclerView
      • the Toolbar for the app
      • the navigation graph, which is responsible to navigate between the fragments

So, using the below link download the file and extract it into your project’s res folder.

Download assets

New package

Before the creation of the fragments, we are going ot create a new package for the fragments, to have a clean design of our app.

We can do it, by clicking on the package of the project. You can find it in the Project tab, at the left side of Android Studio.


In the popup window name the new package as “fragments”. and click on the OK button.
The fragments

Inside of the new fragments package, we will create the four fragments. By name:

      • LoginFragment
      • MainFragment
      • ProfileFragment
      • RegisterFragment
LoginFragment

First, we will start with the LoginFragment. So, click with the right mouse button on the fragments package and from the popup menu select New, then Fragment and from the submenu Fragment (Blank). In the popup window name the fragment as “LoginFragment”.

After Android Studio finishes the creation, you will have lots of lines of code inside of our new fragment… we don’t need them, so remove all the unnecessary code. Finally, it should look like below.


LoginFragment
fragment_login.xml

To start using Data Binding for the LoginFragment, we have to convert its layout. So, open the fragment_login.xml file. If you closed it, then you can find it in the res->layout folders.

Change the current FrameLayout to ConstraintLayout. Then, move your cursor to layout and press there on you keyboard the Alt+Enter hot keys on Windows or Control/Option+Enter on Mac. This will open up a quick menu, the Show Context Actions menu. There, select the first option, which is “Convert to data binding layout”.

Now, the layout should look like below.


fragment_login.xml

The next step is to add the views to the layout. In this tutorial we won’t go into the detailes of the layout. What you should note is the Guideline, what we use to align the WordPress logo and the other views.

If you would like to see deeply how does ConstraintLayout work, then check out our tutorial.

ConstraintLayout – SplashScreen

The layout of the fragment_login.xml file looks like below. Copy and paste it into your project.


fragment_login.xml

The last thing, what we have to do with the login fragment is to add the Data Binding to the LoginFragment::class as well. So, open the file from the fragments package and add the binding variable into the class, and change the call of the view.

In the login screen, we don’t need the Toolbar, so in the onStart() method we need to hide it.

Finally the class looks like below.


LoginFragment::class
RegisterFragment

The rest fragments won’t be too different from the LoginFragment, so just follow the same instructions from the creation of the login screen and paste the below code into the right files.

The below code goes into the fragment_register.xml file.


AndroidManifest.xml

The next lines goes into to RegisterFragment::class.


RegisterFragment
ProfileFragment

The next fragment is the ProfileFragment. It will be very simple, because it will contain only a TextView for the name of the logged in user and a Button to log out from the app.

As before, first create a new fragment and paste the next lines into its layout and class.

The below code goes into the profile_register.xml file.


fragment_profile.xml

… and the code for the ProfileFragment::class


ProfileFragment::class
The main screen

After the creation of the previous screens, we can add the last fragment as well. So, as before, create a new fragment into the fragments package with the name of MainFragment.

The below lines goes into the fragment_main.xml file.


fragment_main.xml

The next lines will be the body of the MainFragment::class.


MainFragment::class

Note that

      • in the onStart() method we show again the Toolbar
      • in the onCreate() method after the binding, we hide the navigation icon
      • the onViewCreated() method will set the menu and the RecyclerView
      • and finally the setProgressBarVisibility() method will show or hide the loading spinner when the app does some background work, eg fetch the list of the users.

Currently you should have some errors, but don’t worry, we are going to solve them soon.

Step 4 – The adapter for the RecyclerView

On the main screen, we will list the all the resitered user from the database of the WordPress website. To have all of them visible, we are going to create a RecyclerView, which needs an adapter.

So, just create a new package called “adapter” and inside of it a new Kotlin file with the name of “UserAdapter”.

The code of the adapter looks like below.


UserAdapter::class

If you would like get to know more about RecyclerView, then check out our basics tutorial under the link

RecyclerView basics tutorial

Step 5 – The User data class

In this application we are going to use a simple data holder, which will be a User data class.

If you would like to know more about data class, then check out our tutorial.

Data Class

So, create a new package called “model”, then there a new Kotlin file with the name of “User” and paste the below code into it.


User data class
Step 6 – The main activity

In this tutorial we follow the one activity approach. It means, thet we have only one activity, and there a fragment container, where we will add the fragmetns based on the navigation of the user.

To enable this functionality for our app, we have to add there also some lines of code. So, open the MainActivity::class and paste there the below lines.


MainActivity::class
Step 7 – The class of the application

Later on, we are going to use a ViewModel to handle the data for our app. This ViewModel will hold the list of the users and will notify the user interface about the changes in the data. When does it happen, then a Toast will show for the user the result and for this the text comes from the strings.xml file.

To get the text from this file, we need a context. In our app we will use a singleton application context, what we will define in the MyApp::class. This class will be inherited from the Application::class.

So, create a new file with this name in the package of the MainActivity::class and paste there the below code.


MyApp::class

Before we can use this class in the whole app, we have to add it to the AndroidManifest.xml file, what you can find in the manifest folder of your sample app.

Open the xml file, and add the below line into the <application> tag.

android:name=“.MyApp”

Run the app

Finally, we can run the app, but still you won’t see to much things on it. When the app succesfully build and run on your device, then the login screen should be visible.

GitHub

In case, if you have some errors, then you can download the sample app from our GitHub repository.

GitHub – Retrofit

Questions

I hope the description was understandable and clear. But, if you have still questions, then leave me comments below! 😉

Have a nice a day! 🙂

 


 

Click to rate this post!
[Total: 0 Average: 0]
Follow and like us:

Leave a comment

stay informed!

Subscribe to receive exclusive content and notifications