How to Build a Rick and Morty App in Android?
Last Updated :
28 Apr, 2025
Rick and Morty is an American animated science fiction sitcom created by Justin Roiland and Dan Harmon. In this article, we will build an application that will display the name and image of all rick and Morty characters using this API. In order to build this application we will be using the Retrofit library. Retrofit is a third-party library that helps us to make a network request. From the json respoanse we will be fetching names and images of characters.
To get started, we will first set up our project and create an interface that defines the endpoints of the Rick and Morty API. Then, we will implement an Activity that makes a GET request to the API and displays the list of characters in a RecyclerView. Finally, we will create an adapter for the RecyclerView that binds the character data to the layout for each item in the list. A sample video is given below to get an idea about what we are going to do in this article.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.
Step 2: Add these dependencies in Build.gradle(app) file
// retrofit
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
// circular image view
implementation 'de.hdodenhof:circleimageview:3.1.0'
// glide library for image processing
implementation 'com.github.bumptech.glide:glide:4.13.2'
Add internet permission in the Android Manifest XML file
XML
<uses-permission android:name="android.permission.INTERNET"/>
Step 3: Generate data classes
Copy the response from the above API. Right-click on the root package and select New->Kotlin-data class from JSON. If you don’t have this plugin, go to File -> Settings -> Plugin and install JSON to Kotlin Plugin. Copy the JSON result and paste it. Give this file a suitable name after that two data classes will be generated.
Kotlin
data class RickMorty(
val results: List<Result>
)
Kotlin
data class Result(
val image: String,
val name: String,
)
Step 4: Create API interface and retrofit instance
This is an interface in Kotlin called RickApi that defines a single method called getDetails. This method is annotated with the @GET annotation, which specifies that it should perform an HTTP GET request to the specified endpoint ("character" in this case).
Kotlin
// Interface that defines the
// endpoints of the Rick and Morty API
interface RickApi {
// HTTP GET request to the character endpoint
// https://rickandmortyapi.com/api/character
@GET("character")
fun getDetails() : Call<RickMorty>
}
The api property is initialized by building a Retrofit instance using the Retrofit.Builder class, setting the base URL for the API, adding a converter factory to parse the API's response data (using Gson in this case), and then creating an implementation of the RickApi interface using the create method of the Retrofit instance.
Kotlin
// Object that holds a single
// instance of the RickApi interface,
// initialized using Retrofit
object RetrofitInstance {
// Property that lazily initializes
// the RickApi implementation
val api : RickApi by lazy {
// Build the Retrofit instance with
// a base URL and a Gson converter factory
Retrofit.Builder()
.baseUrl("https://rickandmortyapi.com/api/")
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(RickApi::class.java)
}
}
Step 5: Design layout Files
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.
activity_main.xml
XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
recycler_layout.xml
Since we are using recycler view we need to create a new layout file for that go to res->layout and create a new Layout resource file named recycler_layout.
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="10dp">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/characterImage"
android:layout_width="75dp"
android:layout_height="60dp">
</de.hdodenhof.circleimageview.CircleImageView>
<TextView
android:id="@+id/showName"
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="Name"
android:padding="18dp"
android:textSize="20dp">
</TextView>
</LinearLayout>
Step 6: Create an Adapter class for RecyclerView
This is the code for the RickRecycler class, which is an adapter for a RecyclerView that displays a list of Rick and Morty characters. The adapter holds a list of Result objects, which represent the character data, and updates the RecyclerView when the data is changed.
Kotlin
class RickRecycler : RecyclerView.Adapter<RickRecycler.ViewHolder>() {
// List of characters to display
var list = ArrayList<Result>()
// Set the data for the adapter and
// notify the RecyclerView of the change
fun setData(list : List<Result>){
this.list = list as ArrayList<Result>
notifyDataSetChanged()
}
// Initialise single item using view holder class
class ViewHolder(view : View) : RecyclerView.ViewHolder(view){
val characterImage = view.characterImage
val name = view.showName
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.recycler_layout,parent,false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
// loading image using glide library
Glide.with(holder.itemView).load(list[position].image).into(holder.characterImage)
// loading character name in text View
holder.name.text = list[position].name
}
override fun getItemCount(): Int {
return list.size
}
}
Step 7: Working with the MainActivity.kt file
This is the code for the MainActivity class, which is the main Activity of the app. The MainActivity sets up the RecyclerView and the RickRecycler adapter and makes a GET request to the Rick and Morty API to retrieve the list of characters.
Kotlin
class MainActivity : AppCompatActivity() {
// Reference of Adapter Class
private lateinit var adapter: RickRecycler
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// initializing recycler view
adapter = RickRecycler()
recyclerView.layoutManager = LinearLayoutManager(applicationContext)
recyclerView.adapter = adapter
RetrofitInstance.api.getDetails().enqueue(object :Callback<RickMorty>{
override fun onResponse(call: Call<RickMorty>, response: Response<RickMorty>) {
if (response.body()!=null){
adapter.setData(response.body()!!.results)
}
else{
return
}
}
override fun onFailure(call: Call<RickMorty>, t: Throwable) {
Log.d("TAG",t.message.toString())
}
})
}
}
Output:
Similar Reads
How to Build a Pomodoro App in Android?
The Pomodoro Technique is a time management method developed by Francesco Cirillo in the late 1980s. The technique uses a timer to break down work into intervals, traditionally 25 minutes in length, separated by short breaks of 5 minutes. These intervals are known as "pomodoros". The method is based
4 min read
How to Build a Simple Notes App in Android?
Notes app is used for making short text notes, updating when you need them, and trashing when you are done. It can be used for various functions as you can add your to-do list in this app, some important notes for future reference, etc. The app is very useful in some cases like when you want quick a
9 min read
How to Build a Simple Alarm Setter App in Android?
In this article, we are going to see how to build a much interesting app named Alarm Setter. Alarm plays a vital role in our day-to-day life. Nowadays alarm has become our wake-up assistant. Every mobile phone is associated with an alarm app. We will create this app using android studio. Android Stu
5 min read
How to Build a Number Shapes Android App in Android Studio?
Android is a vast field to learn and explore and to make this learning journey more interesting and productive we should keep trying new problems with new logic. So, today we are going to develop an application in android studio which tells us about Numbers that have shapes. We will be covering two
4 min read
How to Build a Weather App in Android?
In this project, we will be building a weather application. This application will show the temperature of a location. To fetch weather information we will need an API. An API(Application Programming Interface) is a function that allows applications to interact and share data using various components
6 min read
How to Build a Simple Magic 8 Ball App in Android?
In this article, we will be building a Magic 8 Ball App Project using Java and XML in Android. The application is based on a decision making application. In this app, a user can ask the ball what decision they should make. The ball will randomly answer Yes, No, Not Sure, and other such answers. Ther
3 min read
How to Build a Sticky Notes Application in Android Studio?
We as humans tend to forget some small but important things, and to resolve this we try to write those things up and paste them somewhere, we often have eyes on. And in this digital era, the things we are most likely to see are laptop, computer, or mobile phone screens. For this, we all have once us
11 min read
How to Build a Tic Tac Toe Game in Android?
In this article, we will be building a Tic Tac Toe Game Project using Java and XML in Android. The Tic Tac Toe Game is based on a two-player game. Each player chooses between X and O. The Player plays one move at a time simultaneously. In a move, a player can choose any position from a 3x3 grid. The
8 min read
How To Make An Android App
Have you always wanted to make an Android app but got lost in the complexity of the Android Studio or the technical jargon used in the development process? Don't worry, you have come to the right place. We are going to help you get started with your first Android app.The app that we are going to mak
7 min read
How to Create a Basic Intro Slider of an Android App?
When we download any app and use that app for the very first time. Then we will get to see the intro slider inside our app. With the help of this slider, we educate our users on how they can use that app and it tells in detail about the app. In this article, we will take a look at the implementation
4 min read