Movie Database Android Application
Last Updated :
23 Jul, 2025
Movies are an integral part of our entertainment lives. As a developer, making an application that displays information about movies can be a rewarding project idea! In this article we will learn about making an application that displays basic information about movies like Posters, Title, Runtime, Taglines, Revenue, Production companies, etc. We will be making use of TMBD API (The Movie Database API) which is completely free for development and learning purposes.
What is TMDB API?
TMDB or The Movie Database is a popular database platform that provides API's to access the information of requested movies. The TMDb API provides developers with the ability to access data like movie details, genres, trailers, ratings, and much more. With a simple request, you can search for movies, explore trending titles, or access in-depth information about specific films and cast members.
Sign Up in TMDB
To use TMDB API, you need to log in to your account and use the authorization token provided after logging in:
- Go to the TMDB Website.
- SignUp using your account details
- Apply for a new Authorization Token
- Fill in your details
- Submit.
After completing the above steps you will get an authorization token. Save it somewhere because we will need to use it in the application.
Mak
Working on the Application
When you have successfully Signed Up on TMDB we can process to build the application.
What will the Application look like?
- There will be a total 2 screens in the Application
- List of movies Display Screen ( The Launcher Activity ).
- The list will be of Searched Movie, Trending Movies, Popular Movies, and Saved Movies.
- More Details about the Selected Movie.
- For Saving the favorite movies, we will use SQLite Database in Android.
Doing Pre-tasks
Before diving straight into the code, we must first have all the resources, Images, drawable, etc
colors.xml
Below is the colors.xml file. It defines all the colors used in the application:
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="yellow">#da9100</color>
<color name="card_background_color">#2E2E2E</color>
<color name="overview_background_color">#3E3E3E</color>
<color name="dark_background">#121212</color>
<color name="dark_toolbar">#1F1F1F</color>
<color name="dark_text">#FFFFFF</color>
<color name="dark_subtext">#B0B0B0</color>
<color name="dark_accent">#FF4081</color>
<color name="colorAccentDark">#BB86FC</color>
<color name="buttonDarkBackground">#333333</color>
</resources>
Drawable Files
Drawable files can be Drawable resource layout files or any images. We are making use of both.
1. Create a search_background.xml file. This is the Background of the EditText where users can search for movies
search_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/white"/>
<corners android:radius="15dp"/>
<padding android:top="10dp" android:bottom="10dp" android:right="10dp" android:left="10dp"/>
</shape>
2. Create filter_button_background.xml file. This is the background of the filter buttons present on the home screen. Filter buttons are nothing but filtering Saved, Trending, Popular, and searched Movies.
XML
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selected state -->
<item android:state_selected="true">
<shape android:shape="rectangle">
<solid android:color="@color/colorAccentDark" /> <!-- Highlight color when pressed -->
<corners android:radius="16dp" />
</shape>
</item>
<!-- Default state -->
<item>
<shape android:shape="rectangle">
<solid android:color="@color/buttonDarkBackground" /> <!-- Default button background -->
<corners android:radius="16dp" /> <!-- Rounded corners -->
<stroke android:width="2dp" android:color="@color/colorAccentDark" /> <!-- Optional border -->
</shape>
</item>
</selector>
3. Download and place two images in your Drawable file. These two images are very easily available on the internet
- A Placeholder Image. Name It placeholder.
- A Search icon. Name it search.
Adding Necessary Dependencies
Go to your Gradle Scripts > build.gradle.kts (Module: app) file. Add the following dependencies into it and sync the changes.
Kotlin
implementation("com.github.bumptech.glide:glide:4.11.0")
implementation("com.android.volley:volley:1.2.1")
implementation("com.google.android.material:material:1.9.0")
implementation("com.google.android.flexbox:flexbox:3.0.0")
With this, the resources for the application res completed. Let's dive into the code
Database Helper Class
To save the favorite movies of the user, we will implement a SQLite database. To implement the database, we will need a helper class that will communicate with the database and help our application to store and retrieve values from the database.
- Go into your app > java > com > PACKAGE > PACKAGE.
- Make a new Package Database.
- Inside the new package, make a new Java class called SqLiteHelper.java
Java
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.ishaanbhela.geeksformovies.productionCompany.productionCompanyModel;
import java.util.ArrayList;
import java.util.List;
public class SqLiteHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "movies_db";
private static final int DATABASE_VERSION = 1;
public SqLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// Create the movies table
String CREATE_MOVIES_TABLE = "CREATE TABLE IF NOT EXISTS movies (" +
"id INTEGER PRIMARY KEY, " +
"title TEXT, " +
"poster_path TEXT, " +
"tagline TEXT, " +
"release_date TEXT, " +
"runtime INTEGER, " +
"rating REAL, " +
"overview TEXT, " +
"budget INTEGER, " +
"revenue INTEGER, " +
"genres TEXT, " +
"languages TEXT)";
db.execSQL(CREATE_MOVIES_TABLE);
// Create the production_companies table
String CREATE_PRODUCTION_COMPANIES_TABLE = "CREATE TABLE IF NOT EXISTS production_companies (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"movie_id INTEGER, " +
"company_name TEXT, " +
"logo_path TEXT, " +
"FOREIGN KEY(movie_id) REFERENCES movies(id) ON DELETE CASCADE)";
db.execSQL(CREATE_PRODUCTION_COMPANIES_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS movies");
db.execSQL("DROP TABLE IF EXISTS production_companies");
onCreate(db);
}
// Check if the movie exists in the DB
public boolean isMovieSaved(int movieId) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query("movies", new String[]{"id"}, "id=?", new String[]{String.valueOf(movieId)}, null, null, null);
boolean exists = cursor.getCount() > 0;
cursor.close();
return exists;
}
// Insert movie and production companies
public void insertMovie(int id, String title, String posterPath, String tagline, String releaseDate, int runtime,
double rating, String overview, long budget, long revenue, String genres, String languages,
List<productionCompanyModel> productionCompanies) {
SQLiteDatabase db = this.getWritableDatabase();
// Insert movie
ContentValues movieValues = new ContentValues();
movieValues.put("id", id);
movieValues.put("title", title);
movieValues.put("poster_path", posterPath);
movieValues.put("tagline", tagline);
movieValues.put("release_date", releaseDate);
movieValues.put("runtime", runtime);
movieValues.put("rating", rating);
movieValues.put("overview", overview);
movieValues.put("budget", budget);
movieValues.put("revenue", revenue);
movieValues.put("genres", genres);
movieValues.put("languages", languages);
db.insert("movies", null, movieValues);
// Insert production companies associated with this movie
for (int i = 0; i < productionCompanies.size(); i++) {
productionCompanyModel company = productionCompanies.get(i);
String companyName = company.getName();
String logoPath = company.getLogo_path();
ContentValues companyValues = new ContentValues();
companyValues.put("movie_id", id);
companyValues.put("company_name", companyName);
companyValues.put("logo_path", logoPath);
db.insert("production_companies", null, companyValues);
}
//db.close();
}
// Delete movie
public void deleteMovie(int movieId) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete("movies", "id=?", new String[]{String.valueOf(movieId)});
db.delete("production_companies", "movie_id=?", new String[]{String.valueOf(movieId)});
//db.close();
}
// Get movie details
public Cursor getMovieDetails(int movieId) {
SQLiteDatabase db = this.getReadableDatabase();
return db.query("movies", null, "id=?", new String[]{String.valueOf(movieId)}, null, null, null);
}
// Get production companies for a movie
public List<productionCompanyModel> getProductionCompanies(int movieId) {
List<productionCompanyModel> companies = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query("production_companies", null, "movie_id=?", new String[]{String.valueOf(movieId)}, null, null, null);
if (cursor.moveToFirst()) {
do {
int companyNameIndex = cursor.getColumnIndex("company_name");
int logoPathIndex = cursor.getColumnIndex("logo_path");
if(companyNameIndex > 0 && logoPathIndex > 0){
String companyName = cursor.getString(companyNameIndex);
String logoPath = cursor.getString(logoPathIndex);
companies.add(new productionCompanyModel(logoPath, companyName));
}
else{
System.out.println("SOME ERRORR OCCURRED");
}
} while (cursor.moveToNext());
}
cursor.close();
return companies;
}
}
With this our Database Helper class is successfully made.
Frontend: XML layouts
Our application will have two main layout files and a few RecyclerView layout files. Lets look at them one by one
activity_main.xml
This is the launcher activity of out application. This activity will display all the popular, trending and searched movies. We will have a search bar and some filter buttons and the main content Recycler View. The recycler View will display the resultant movies for the applied filter.
activity_main.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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!-- Search Bar -->
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/searchBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="30dp"
android:layout_marginTop="20dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toTopOf="@id/buttonContainer">
<EditText
android:id="@+id/editText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Search for Movies"
android:textColor="@color/black"
android:textColorHint="@color/black"
android:background="@drawable/search_background"
android:paddingEnd="40dp"
app:layout_constraintEnd_toStartOf="@id/searchIcon"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
<ImageView
android:id="@+id/searchIcon"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginHorizontal="10dp"
android:src="@drawable/search"
android:contentDescription="Search Icon"
android:background="@drawable/search_background"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toEndOf="@id/editText"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.flexbox.FlexboxLayout
android:id="@+id/buttonContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="10dp"
android:layout_marginHorizontal="30dp"
app:flexDirection="row"
app:flexWrap="wrap"
app:justifyContent="flex_start"
app:layout_constraintBottom_toTopOf="@id/recyclerView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/searchBar">
<TextView
android:id="@+id/searched_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/filter_button_background"
android:text="Searched"
android:layout_margin="2dp"
android:paddingHorizontal="10dp"
android:paddingVertical="2dp"
android:textColor="@color/white"
android:visibility="gone"
app:backgroundTint="@null" />
<TextView
android:id="@+id/trending_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/filter_button_background"
android:text="Trending"
android:layout_margin="2dp"
android:paddingHorizontal="10dp"
android:paddingVertical="2dp"
android:textColor="@color/white"
app:backgroundTint="@null" />
<TextView
android:id="@+id/popular_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/filter_button_background"
android:text="Popular"
android:layout_margin="2dp"
android:paddingHorizontal="10dp"
android:paddingVertical="2dp"
android:textColor="@color/white"
app:backgroundTint="@null" />
<TextView
android:id="@+id/saved_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/filter_button_background"
android:text="Saved Movies"
android:layout_margin="2dp"
android:paddingHorizontal="10dp"
android:paddingVertical="2dp"
android:textColor="@color/white"
app:backgroundTint="@null" />
</com.google.android.flexbox.FlexboxLayout>
<!-- RecyclerView for displaying movie search results -->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginHorizontal="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/buttonContainer"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
This is what the preview layout should look like
Layout File for Recycler View in Main Activity
Create a new layout resource names search_layout.xml file in the same directory as your main_activity.xml file.
search_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_margin="8dp"
android:elevation="4dp"
app:cardCornerRadius="8dp"
app:cardBackgroundColor="@color/card_background_color">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp">
<!-- Movie Poster -->
<ImageView
android:id="@+id/movie_poster"
android:layout_width="match_parent"
android:layout_height="200dp"
android:src="@drawable/placeholder"
android:scaleType="fitCenter"
android:contentDescription="Movie Poster" />
<!-- Movie Title -->
<TextView
android:id="@+id/movie_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title"
android:gravity="center"
android:textSize="18sp"
android:textColor="@android:color/white"
android:textStyle="bold"
android:paddingTop="8dp"
android:paddingBottom="4dp"
android:maxLines="2"
android:ellipsize="end" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="4dp">
<!-- Movie Release Date -->
<TextView
android:id="@+id/movie_release_date"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Release Date"
android:textSize="14sp"
android:textColor="@android:color/darker_gray" />
<!-- Movie Rating -->
<TextView
android:id="@+id/movie_rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rating"
android:textSize="14sp"
android:textColor="@android:color/holo_orange_light" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/overview_background_color"
android:padding="8dp"
android:orientation="vertical">
<!-- Movie Overview -->
<TextView
android:id="@+id/movie_overview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Overview"
android:textSize="14sp"
android:textColor="@android:color/white"
android:maxLines="2"
android:ellipsize="end" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
Layout:
activity_movie_details.xml
Make a new Activity in your project. This activity will display more detailed information about Selected movie in your application. Users can enter this activity simply by selecting a movie from main_activity.xml file.
main_activity.xml
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:fitsSystemWindows="true"
android:background="@color/dark_background">
<!-- AppBarLayout for collapsing toolbar -->
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<!-- CollapsingToolbarLayout to collapse the movie poster -->
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/collapsingToolbar"
android:layout_width="match_parent"
android:layout_height="450dp"
android:fitsSystemWindows="true"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<!-- Movie Poster Image that collapses -->
<ImageView
android:id="@+id/movie_poster"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:fitsSystemWindows="true"
app:layout_collapseMode="parallax" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<!-- The main content (details) in a NestedScrollView for scrolling -->
<androidx.core.widget.NestedScrollView
android:id="@+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/dark_background"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<!-- Movie Title (below collapsed poster) -->
<TextView
android:id="@+id/movie_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Movie Title"
android:textSize="24sp"
android:textStyle="bold"
android:paddingBottom="8dp"
android:textColor="@android:color/white" />
<!-- Tagline -->
<TextView
android:id="@+id/movie_tagline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tagline"
android:paddingBottom="4dp"
android:textColor="@android:color/darker_gray" />
<!-- Genres -->
<TextView
android:id="@+id/movie_genres"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Action, Adventure, Comedy"
android:paddingBottom="8dp"
android:textColor="@android:color/white" />
<!-- Release Date -->
<TextView
android:id="@+id/movie_release_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Release Date: 2016-02-09"
android:paddingBottom="4dp"
android:textColor="@android:color/darker_gray" />
<!-- Runtime -->
<TextView
android:id="@+id/movie_runtime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Runtime: 108 min"
android:paddingBottom="4dp"
android:textColor="@android:color/darker_gray" />
<!-- Rating -->
<TextView
android:id="@+id/movie_rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rating: 7.6/10"
android:paddingBottom="4dp"
android:textColor="@android:color/holo_orange_light" />
<!-- Overview (Movie description) -->
<TextView
android:id="@+id/movie_overview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="The origin story of former Special Forces operative turned mercenary Wade Wilson..."
android:paddingTop="8dp"
android:paddingBottom="16dp"
android:textColor="@android:color/white" />
<!-- Budget -->
<TextView
android:id="@+id/movie_budget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Budget: $58,000,000"
android:paddingBottom="4dp"
android:textColor="@android:color/darker_gray" />
<!-- Revenue -->
<TextView
android:id="@+id/movie_revenue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Revenue: $782,837,347"
android:paddingBottom="16dp"
android:textColor="@android:color/darker_gray" />
<!-- Production Companies RecyclerView -->
<TextView
android:id="@+id/production_companies_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Production Companies"
android:textStyle="bold"
android:textSize="18sp"
android:layout_marginBottom="8dp"
android:textColor="@android:color/white" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/production_companies_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="16dp" />
<!-- Spoken Languages -->
<TextView
android:id="@+id/movie_languages"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Languages: English"
android:paddingBottom="16dp"
android:textColor="@android:color/white" />
<Button
android:id="@+id/saveUnsave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Save"
android:backgroundTint="@android:color/holo_orange_dark"
android:textColor="@color/white"/>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Layout of the above XML file:
Recycler View layout file for Production Companies
Again, make a new layout resource file production_company_layout.xml in the same directory as your activity_movie_details.xml file. This file is to display the logos of production companies involved in making of the movie.
production_company_layout.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"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp">
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/white"
app:cardCornerRadius="25dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="5dp"
android:orientation="vertical">
<ImageView
android:id="@+id/productionLogo"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/placeholder" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
Layout of the above xml file:
Backend: Calling API's and displaying in the Application
MainActivity.java
This is the backend file attached to the launcher activity. This file calls the TMDB API and displays the necessary information on the screen. MainActivity contains one Recycler View, so first we need to make an adapter and one model class for it. The Recycler View is for displaying short information about the filtered movies.
- searchedMovieModel.java: This is the model file that stores the filtered movie information in a structured way
searchedMovieModel.java
package com.ishaanbhela.geeksformovies.searchedMovies;
public class searchedMoviesModel {
private int id;
private String title;
private String overview;
private String releaseDate;
private String posterPath;
private double Rating;
// Constructor
public searchedMoviesModel(int id, String title, String overview, String releaseDate, String posterPath, double Rating) {
this.id = id;
this.title = title;
this.overview = overview;
this.releaseDate = releaseDate;
this.posterPath = posterPath;
this.Rating = Rating;
}
// Getter and Setter Methods
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getOverview() {
return overview;
}
public void setOverview(String overview) {
this.overview = overview;
}
public String getReleaseDate() {
return releaseDate;
}
public void setReleaseDate(String releaseDate) {
this.releaseDate = releaseDate;
}
public String getPosterPath() {
return posterPath;
}
public void setPosterPath(String posterPath) {
this.posterPath = posterPath;
}
public double getVoteAverage() {
return Rating;
}
public void setVoteAverage(double Rating) {
this.Rating = Rating;
}
}
- searchedMovieAdapter.java: This file binds the search result with the RecyclerView.
searchedMovieAdapter.java
package com.ishaanbhela.geeksformovies.searchedMovies;
import static androidx.core.content.ContextCompat.startActivity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.ishaanbhela.geeksformovies.Database.SqLiteHelper;
import com.ishaanbhela.geeksformovies.R;
import com.ishaanbhela.geeksformovies.movieDetails;
import com.ishaanbhela.geeksformovies.savedMovieDetails;
import java.util.List;
public class searchedMoviesAdapter extends RecyclerView.Adapter<searchedMoviesAdapter.searchedMoviesHolder> {
private Context context;
private List<searchedMoviesModel> movieList;
private SqLiteHelper dbHelper;
public searchedMoviesAdapter(List<searchedMoviesModel> movies, Context context){
movieList = movies;
this.context = context;
}
@NonNull
@Override
public searchedMoviesHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.search_layout, parent, false);
return new searchedMoviesHolder(view);
}
// Binds the content with the components
@Override
public void onBindViewHolder(@NonNull searchedMoviesHolder holder, int position) {
searchedMoviesModel movie = movieList.get(position);
holder.title.setText(movie.getTitle());
holder.release.setText("Release Date: " + movie.getReleaseDate());
holder.rating.setText("Rating: " + movie.getVoteAverage());
holder.overview.setText(movie.getOverview());
String posterUrl = "https://image.tmdb.org/t/p/w500" + movie.getPosterPath(); // TMDb URL for posters
Glide.with(context)
.load(posterUrl)
.placeholder(R.drawable.placeholder) // Placeholder image while loading
.error(R.drawable.placeholder)
.into(holder.poster);
holder.itemView.setOnClickListener(v -> {
dbHelper = new SqLiteHelper(context);
boolean isSaved = dbHelper.isMovieSaved(movie.getId());
Intent intent;
if (isSaved) {
// If movie is saved, open the saved movie details
intent = new Intent(context, savedMovieDetails.class);
intent.putExtra("MOVIE_ID", movie.getId());
} else {
// If movie is not saved, open the regular movie details
intent = new Intent(context, movieDetails.class);
intent.putExtra("MOVIE_ID", movie.getId());
}
context.startActivity(intent);
});
}
@Override
public int getItemCount() {
return movieList.size();
}
// Holds the components of the layout file for Recycler View
public class searchedMoviesHolder extends RecyclerView.ViewHolder {
ImageView poster;
TextView title, overview, rating, release;
public searchedMoviesHolder(@NonNull View itemView) {
super(itemView);
poster = itemView.findViewById(R.id.movie_poster);
title = itemView.findViewById(R.id.movie_title);
overview = itemView.findViewById(R.id.movie_overview);
rating = itemView.findViewById(R.id.movie_rating);
release = itemView.findViewById(R.id.movie_release_date);
}
}
}
When both, the model file and adapter is successfully made, lets implement the MainActivity.java.
MainActivity.java
package com.ishaanbhela.geeksformovies;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.android.volley.Request;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.ishaanbhela.geeksformovies.Database.SqLiteHelper;
import com.ishaanbhela.geeksformovies.searchedMovies.searchedMoviesAdapter;
import com.ishaanbhela.geeksformovies.searchedMovies.searchedMoviesModel;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private searchedMoviesAdapter movieAdapter;
private List<searchedMoviesModel> trendingMovieList;
private List<searchedMoviesModel> popularMovieList;
private List<searchedMoviesModel> savedMovieList;
private List<searchedMoviesModel> searchedMovieList;
private TextView searched, trending, popular, saved;
private String SearchText;
private ImageView searchIcon;
private EditText searchEditText;
String api = "YOUR_AUTH_TOKEN";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
trendingMovieList = new ArrayList<>();
recyclerView = findViewById(R.id.recyclerView);
searched = findViewById(R.id.searched_button);
trending = findViewById(R.id.trending_button);
popular = findViewById(R.id.popular_button);
saved = findViewById(R.id.saved_button);
searchIcon = findViewById(R.id.searchIcon);
searchEditText = findViewById(R.id.editText);
popularMovieList = new ArrayList<>();
savedMovieList = new ArrayList<>();
searchedMovieList = new ArrayList<>();
trendingMovieList = new ArrayList<>();
recyclerView.setLayoutManager(new LinearLayoutManager(this));
loadTrending();
searched.setOnClickListener(v -> {
loadSearched(searched.getText().toString());
});
trending.setOnClickListener(v -> {
loadTrending();
});
popular.setOnClickListener(v -> {
loadPopular();
});
saved.setOnClickListener(v -> {
loadSaved();
});
searchIcon.setOnClickListener(v -> {
loadSearched(searchEditText.getText().toString());
});
}
private void loadSaved() {
saved.setSelected(true);
popular.setSelected(false);
searched.setSelected(false);
trending.setSelected(false);
SqLiteHelper dbHelper = new SqLiteHelper(this); // Initialize the SQLite helper
List<searchedMoviesModel> savedMoviesList = new ArrayList<>();
// Fetch saved movies from the database
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM movies", null);
// If there are saved movies, iterate over the results and add them to the list
if (cursor.moveToFirst()) {
do {
// Extract data from the cursor
int idIndex = cursor.getColumnIndex("id");
int titleIndex = cursor.getColumnIndex("title");
int overviewIndex = cursor.getColumnIndex("overview");
int releaseDateIndex = cursor.getColumnIndex("release_date");
int posterPathIndex = cursor.getColumnIndex("poster_path");
int ratingIndex = cursor.getColumnIndex("rating");
// Ensure all indices are valid
if (idIndex != -1 && titleIndex != -1 && overviewIndex != -1 && releaseDateIndex != -1 && posterPathIndex != -1 && ratingIndex != -1) {
int id = cursor.getInt(idIndex);
String title = cursor.getString(titleIndex);
String overview = cursor.getString(overviewIndex);
String releaseDate = cursor.getString(releaseDateIndex);
String posterPath = cursor.getString(posterPathIndex);
double rating = cursor.getDouble(ratingIndex);
// Create a movie model object and add it to the list
searchedMoviesModel movie = new searchedMoviesModel(id, title, overview, releaseDate, posterPath, rating);
savedMoviesList.add(movie);
}
} while (cursor.moveToNext());
}
cursor.close(); // Close cursor after use
// Set up the RecyclerView with the saved movies
movieAdapter = new searchedMoviesAdapter(savedMoviesList, this);
recyclerView.setAdapter(movieAdapter);
}
private void loadPopular() {
saved.setSelected(false);
popular.setSelected(true);
searched.setSelected(false);
trending.setSelected(false);
// If the list is already populated, no need to call the API again
if(!popularMovieList.isEmpty()){
movieAdapter = new searchedMoviesAdapter(popularMovieList, this);
recyclerView.setAdapter(movieAdapter);
}
// Making an API Request String popularMoviesUrl = "https://api.themoviedb.org/3/movie/popular?language=en-US";
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
Request.Method.GET,
popularMoviesUrl,
null,
response -> {
try {
JSONArray results = response.getJSONArray("results");
addMovie(results, popularMovieList);
movieAdapter = new searchedMoviesAdapter(popularMovieList, this);
recyclerView.setAdapter(movieAdapter);
} catch (JSONException e) {
e.printStackTrace();
Log.e("MovieFetcher", "JSON parsing error: " + e.getMessage());
}
},
error -> Log.e("MovieFetcher", "Error: " + error.getMessage())
) {
@Override
public Map<String, String> getHeaders() {
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", "Bearer " + api); // Use your API key here
headers.put("Content-Type", "application/json;charset=utf-8");
headers.put("Accept", "application/json");
return headers;
}
};
Volley.newRequestQueue(this).add(jsonObjectRequest);
}
private void loadSearched(String search) {
saved.setSelected(false);
popular.setSelected(false);
searched.setSelected(true);
trending.setSelected(false);
if(search.equals("") || search.isEmpty()){
Toast.makeText(this, "No search value provided", Toast.LENGTH_SHORT).show();
return;
}
else if(search.equals(searched.getText().toString())){
movieAdapter = new searchedMoviesAdapter(searchedMovieList, this);
recyclerView.setAdapter(movieAdapter);
return;
}
searched.setText(search);
searched.setVisibility(View.VISIBLE);
searchedMovieList.clear();
String url = "https://api.themoviedb.org/3/search/movie?query="+search+"&include_adult=false&language=en-US&page=1";
// Create a new request to fetch movie data from the API
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null,
response -> {
try {
// Parse the results from the API response
JSONArray results = response.getJSONArray("results");
addMovie(results, searchedMovieList);
// Update RecyclerView with the new data
movieAdapter = new searchedMoviesAdapter(searchedMovieList, this);
recyclerView.setAdapter(movieAdapter);
} catch (JSONException e) {
e.printStackTrace();
}
},
error -> {
// Handle error
error.printStackTrace();
}) {
@Override
public Map<String, String> getHeaders() {
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", "Bearer " + api);
headers.put("Content-Type", "application/json;charset=utf-8");
return headers;
}
};
// Add the request to the Volley request queue
Volley.newRequestQueue(this).add(jsonObjectRequest);
}
private void loadTrending() {
saved.setSelected(false);
popular.setSelected(false);
searched.setSelected(false);
trending.setSelected(true);
if(!trendingMovieList.isEmpty()){
movieAdapter = new searchedMoviesAdapter(trendingMovieList, this);
recyclerView.setAdapter(movieAdapter);
return;
}
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
Request.Method.GET,
"https://api.themoviedb.org/3/trending/movie/day?language=en-US",
null,
response -> {
try {
JSONArray results = response.getJSONArray("results");
addMovie(results, trendingMovieList);
movieAdapter = new searchedMoviesAdapter(trendingMovieList, this);
recyclerView.setAdapter(movieAdapter);
} catch (JSONException e) {
e.printStackTrace();
Log.e("MovieFetcher", "JSON parsing error: " + e.getMessage());
}
},
error -> Log.e("MovieFetcher", "Error: " + error.getMessage())
){
@Override
public Map<String, String> getHeaders() {
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", "Bearer " + api); // Use your auth token here
headers.put("Content-Type", "application/json;charset=utf-8");
headers.put("Accept", "application/json");
return headers;
}
};
Volley.newRequestQueue(this).add(jsonObjectRequest);
}
private void addMovie(JSONArray results, List<searchedMoviesModel> movieList){
try{
for (int i = 0; i < results.length(); i++) {
JSONObject movieJson = results.getJSONObject(i);
// Extracting the data
int id = movieJson.getInt("id");
String title = movieJson.getString("title");
String overview = movieJson.getString("overview");
String releaseDate = movieJson.getString("release_date");
String posterPath = movieJson.getString("poster_path");
double rating = movieJson.getDouble("vote_average");
// Creating a new movie model instance and adding it to the list
searchedMoviesModel movie = new searchedMoviesModel(id, title, overview, releaseDate, posterPath, rating);
movieList.add(movie);
}
}catch (Exception e){
System.out.println("ERRORR");
}
}
}
Detailed Information about Movies
After the Main Activity work is finished, we can move onto making a new activity MovieDetails, which will show detailed information about movies. Whenever a particular movie is clicked on the MainActivity, a movieId is passed through the Intent.putExtra() function. This activity will get the movieId and use it to call API which will give more detailed information about the movie, like its production cost, runtime, revenue earned etc.
The Production companies will be displayed in a Horizontal Recycler view format. So, lets first make a new Model and Adapter class for the Production Companies.
Make a new package, productionCompany.
- productionCompanyModel.java: This class will hold the images and name of the production companies returned by the API:
productionCompanyModel.java
package com.ishaanbhela.geeksformovies.productionCompany;
public class productionCompanyModel {
String name;
String logo_path;
public productionCompanyModel(String logo_path, String name) {
this.logo_path = logo_path;
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLogo_path() {
return logo_path;
}
public void setLogo_path(String logo_path) {
this.logo_path = logo_path;
}
}
- productionCompanyAdapter.java: This class will bind all the object data from productionCompanyModel.java into the recyclerView.
productionCompanyAdapter.java
package com.ishaanbhela.geeksformovies.productionCompany;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.ishaanbhela.geeksformovies.R;
import java.util.List;
public class productionCompanyAdapter extends RecyclerView.Adapter<productionCompanyAdapter.productionCompanyHolder> {
List<productionCompanyModel> companies;
Context context;
public productionCompanyAdapter(List<productionCompanyModel> companies, Context context){
this.companies = companies;
this.context = context;
}
@NonNull
@Override
public productionCompanyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.production_company_layout, parent, false);
return new productionCompanyHolder(view);
}
@Override
public void onBindViewHolder(@NonNull productionCompanyHolder holder, int position) {
String url = "https://image.tmdb.org/t/p/w500" + companies.get(position).logo_path;
Glide.with(context)
.load(url)
.placeholder(R.drawable.placeholder) // Placeholder image while loading
.error(R.drawable.placeholder)
.into(holder.logo);
}
@Override
public int getItemCount() {
return companies.size();
}
public class productionCompanyHolder extends RecyclerView.ViewHolder{
ImageView logo;
public productionCompanyHolder(@NonNull View itemView) {
super(itemView);
logo = itemView.findViewById(R.id.productionLogo);
}
}
}
- movieDetails.java: This is the main code for the movieDetails activity. It will make use of the movieId provided by the Intent from previous activity and call the API to get more detailed information about the movies and use the productionCompany Adapter and Model class to display all the production companies
movieDetails.java
package com.ishaanbhela.geeksformovies;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import com.ishaanbhela.geeksformovies.Database.SqLiteHelper;
import com.ishaanbhela.geeksformovies.productionCompany.productionCompanyAdapter;
import com.ishaanbhela.geeksformovies.productionCompany.productionCompanyModel;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class movieDetails extends AppCompatActivity {
private TextView movieTitle, movieTagline, movieGenres, movieReleaseDate, movieRuntime, movieRating, movieOverview, movieBudget, movieRevenue, movieLanguages;
private ImageView moviePoster;
private RecyclerView productionCompaniesRecyclerView;
private List<productionCompanyModel> productionCompanyList;
private productionCompanyAdapter productionCompaniesAdapter;
private Button saveUnsave;
private int movieId;
private String title, posterPath, tagline, releaseDate, overview, genres, languages;
private int runtime;
private long budget, revenue;
private Double rating;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_movie_details);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
movieTitle = findViewById(R.id.movie_title);
movieTagline = findViewById(R.id.movie_tagline);
movieGenres = findViewById(R.id.movie_genres);
movieReleaseDate = findViewById(R.id.movie_release_date);
movieRuntime = findViewById(R.id.movie_runtime);
movieRating = findViewById(R.id.movie_rating);
movieOverview = findViewById(R.id.movie_overview);
movieBudget = findViewById(R.id.movie_budget);
movieRevenue = findViewById(R.id.movie_revenue);
movieLanguages = findViewById(R.id.movie_languages);
moviePoster = findViewById(R.id.movie_poster);
productionCompaniesRecyclerView = findViewById(R.id.production_companies_recycler);
saveUnsave = findViewById(R.id.saveUnsave);
productionCompanyList = new ArrayList<>();
SqLiteHelper dbHelper = new SqLiteHelper(this);
movieId = getIntent().getIntExtra("MOVIE_ID", -1);
// Call the API to get movie details
getMovieDetails(movieId);
if(dbHelper.isMovieSaved(movieId)){
saveUnsave.setText("Delete from Saved Movies");
}
saveUnsave.setOnClickListener(v -> {
if (saveUnsave.getText().equals("Save")) {
// Save movie and production companies to the database
dbHelper.insertMovie(movieId, title, posterPath, tagline, releaseDate, runtime, rating,
overview, budget, revenue, genres, languages, productionCompanyList);
saveUnsave.setText("Delete from Saved Movies");
} else {
// Delete movie and its associated production companies
dbHelper.deleteMovie(movieId);
saveUnsave.setText("Save");
}
});
}
private void getMovieDetails(int movieId) {
String url = "https://api.themoviedb.org/3/movie/" + movieId + "?append_to_response=genres&language=en-US";
RequestQueue queue = Volley.newRequestQueue(this);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null,
response -> {
try {
// Populate UI with data
title = response.getString("title");
movieTitle.setText(title);
tagline = response.getString("tagline");
movieTagline.setText(tagline);
releaseDate = response.getString("release_date");
movieReleaseDate.setText(releaseDate);
runtime = response.getInt("runtime");
movieRuntime.setText("Runtime: " + runtime + " min");
rating = response.getDouble("vote_average");
movieRating.setText("Rating: " + rating + "/10");
overview = response.getString("overview");
movieOverview.setText(overview);
posterPath = response.getString("poster_path");
// Load Poster Image using Picasso
Glide.with(movieDetails.this).load("https://image.tmdb.org/t/p/w500" + posterPath)
.error(R.drawable.placeholder)
.placeholder(R.drawable.placeholder)
.into(moviePoster);
// Budget and Revenue
budget = response.getLong("budget");
movieBudget.setText("Budget: $" + budget);
revenue = response.getLong("revenue");
movieRevenue.setText("Revenue: $" + revenue);
// Genres
JSONArray genresArray = response.getJSONArray("genres");
StringBuilder genres = new StringBuilder();
for (int i = 0; i < genresArray.length(); i++) {
JSONObject genre = genresArray.getJSONObject(i);
genres.append(genre.getString("name"));
if (i != genresArray.length() - 1) {
genres.append(", ");
}
}
this.genres = genres.toString();
movieGenres.setText(this.genres);
// Spoken Languages
JSONArray languagesArray = response.getJSONArray("spoken_languages");
StringBuilder languages = new StringBuilder();
for (int i = 0; i < languagesArray.length(); i++) {
JSONObject language = languagesArray.getJSONObject(i);
languages.append(language.getString("english_name"));
if (i != languagesArray.length() - 1) {
languages.append(", ");
}
}
this.languages = languages.toString();
movieLanguages.setText("Languages: " + this.languages);
// Production Companies
JSONArray productionCompaniesArray = response.getJSONArray("production_companies");
productionCompanyList.clear();
for (int i = 0; i < productionCompaniesArray.length(); i++) {
JSONObject company = productionCompaniesArray.getJSONObject(i);
String companyName = company.getString("name");
String logoPath = company.optString("logo_path", null); // Sometimes logo_path can be null
if(!logoPath.equals("null")){
productionCompanyList.add(new productionCompanyModel(logoPath, companyName));
}
}
productionCompaniesRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
productionCompaniesAdapter = new productionCompanyAdapter(productionCompanyList, this);
productionCompaniesRecyclerView.setAdapter(productionCompaniesAdapter);
} catch (JSONException e) {
e.printStackTrace();
}
}, error -> {
// Handle error
error.printStackTrace();
}){
@Override
public Map<String, String> getHeaders() {
String api = "YOUR_API_KEY";
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", "Bearer " + api); // Use your auth token here
headers.put("Content-Type", "application/json;charset=utf-8");
headers.put("Accept", "application/json");
return headers;
}
};
queue.add(jsonObjectRequest);
}
}
Finally, we need to make one more java class, an activity class but with the same layout of activity_movie_details.xml. This is because, if a movie is saved in the database, and the user clicks on the movie, then we don't need to call the API again as we already have information about that movie saved in the database. You can skip this step and update your intent function from the MainActivity.java file to call the API anyways.
- savedMovieDetails.java: This class will be called to populate the activity_movie_details.xml file to display detailed movie information when the movie is already saved in the application, by avoiding unnecessary calling of the API.
savedMovieDetails.java
package com.ishaanbhela.geeksformovies;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.ishaanbhela.geeksformovies.Database.SqLiteHelper;
import com.ishaanbhela.geeksformovies.productionCompany.productionCompanyAdapter;
import com.ishaanbhela.geeksformovies.productionCompany.productionCompanyModel;
import java.util.ArrayList;
import java.util.List;
public class savedMovieDetails extends AppCompatActivity {
private TextView movieTitle, movieTagline, movieGenres, movieReleaseDate, movieRuntime, movieRating, movieOverview, movieBudget, movieRevenue, movieLanguages;
private ImageView moviePoster;
private RecyclerView productionCompaniesRecyclerView;
private List<productionCompanyModel> productionCompanyList;
private productionCompanyAdapter productionCompaniesAdapter;
private Button deleteMovie;
private int movieId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_movie_details); // Use the same XML layout
// Initialize UI elements
movieTitle = findViewById(R.id.movie_title);
movieTagline = findViewById(R.id.movie_tagline);
movieGenres = findViewById(R.id.movie_genres);
movieReleaseDate = findViewById(R.id.movie_release_date);
movieRuntime = findViewById(R.id.movie_runtime);
movieRating = findViewById(R.id.movie_rating);
movieOverview = findViewById(R.id.movie_overview);
movieBudget = findViewById(R.id.movie_budget);
movieRevenue = findViewById(R.id.movie_revenue);
movieLanguages = findViewById(R.id.movie_languages);
moviePoster = findViewById(R.id.movie_poster);
productionCompaniesRecyclerView = findViewById(R.id.production_companies_recycler);
deleteMovie = findViewById(R.id.saveUnsave);
deleteMovie.setText("Delete from Saved Movies");
productionCompanyList = new ArrayList<>();
// Get movie ID from intent
movieId = getIntent().getIntExtra("MOVIE_ID", -1);
loadMovieDetails(movieId);
deleteMovie.setOnClickListener(v -> {
new SqLiteHelper(this).deleteMovie(movieId);
finish();
});
}
private void loadMovieDetails(int movieId) {
SqLiteHelper dbHelper = new SqLiteHelper(this);
Cursor cursor = dbHelper.getMovieDetails(movieId);
if (cursor != null && cursor.moveToFirst()) {
// Check and get column indices
int titleIndex = cursor.getColumnIndex("title");
int taglineIndex = cursor.getColumnIndex("tagline");
int releaseDateIndex = cursor.getColumnIndex("release_date");
int runtimeIndex = cursor.getColumnIndex("runtime");
int ratingIndex = cursor.getColumnIndex("rating");
int overviewIndex = cursor.getColumnIndex("overview");
int budgetIndex = cursor.getColumnIndex("budget");
int revenueIndex = cursor.getColumnIndex("revenue");
int posterPathIndex = cursor.getColumnIndex("poster_path");
int genresIndex = cursor.getColumnIndex("genres");
int languagesIndex = cursor.getColumnIndex("languages");
// Now check if the indices are valid (not -1)
if (titleIndex != -1) {
String title = cursor.getString(titleIndex);
movieTitle.setText(title);
}
if (taglineIndex != -1) {
String tagline = cursor.getString(taglineIndex);
movieTagline.setText(tagline);
}
if (releaseDateIndex != -1) {
String releaseDate = cursor.getString(releaseDateIndex);
movieReleaseDate.setText(releaseDate);
}
if (runtimeIndex != -1) {
int runtime = cursor.getInt(runtimeIndex);
movieRuntime.setText("Runtime: " + runtime + " min");
}
if (ratingIndex != -1) {
double rating = cursor.getDouble(ratingIndex);
movieRating.setText("Rating: " + rating + "/10");
}
if (overviewIndex != -1) {
String overview = cursor.getString(overviewIndex);
movieOverview.setText(overview);
}
if (budgetIndex != -1) {
long budget = cursor.getLong(budgetIndex);
movieBudget.setText("Budget: $" + budget);
}
if (revenueIndex != -1) {
long revenue = cursor.getLong(revenueIndex);
movieRevenue.setText("Revenue: $" + revenue);
}
if (posterPathIndex != -1) {
String posterPath = cursor.getString(posterPathIndex);
// Load Poster Image using Glide
Glide.with(savedMovieDetails.this).load("https://image.tmdb.org/t/p/w500" + posterPath)
.error(R.drawable.placeholder)
.placeholder(R.drawable.placeholder)
.into(moviePoster);
}
if (genresIndex != -1) {
String genres = cursor.getString(genresIndex);
movieGenres.setText("Genres: " + genres);
}
if (languagesIndex != -1) {
String languages = cursor.getString(languagesIndex);
movieLanguages.setText("Languages: " + languages);
}
productionCompanyList = dbHelper.getProductionCompanies(movieId);
productionCompaniesAdapter = new productionCompanyAdapter(productionCompanyList, this);
productionCompaniesRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
productionCompaniesRecyclerView.setAdapter(productionCompaniesAdapter);
cursor.close(); // Close the cursor when done
} else {
// Handle the case when the movie is not found
movieTitle.setText("Movie not found");
}
}
}
Note: This application is only catered for DarkMode. So you can either switch your mobile device to DarkMode to use this application correctly or you can follow this article to see how to force the application to always run on Dark Mode even when the mobile device is in Light Mode.
Output of the Application:
Similar Reads
Android Tutorial In this Android Tutorial, we cover both basic and advanced concepts. So whether you are a fresher (graduate) or an experienced candidate with several years of Android Development experience, you can follow this Android tutorial to kick-start your journey in Android app development. Our Android Tutor
15+ min read
Basics
Introduction to Android DevelopmentAndroid operating system is the largest installed base among various mobile platforms across the globe. Hundreds of millions of mobile devices are powered by Android in more than 190 countries of the world. It conquered around 71% of the global market share by the end of 2021, and this trend is grow
5 min read
History of AndroidAndroid devices form a very essential part of a huge section of mobile phone users in today's world. With the global turmoil in the COVID-19 eras, the population has now entered a digital sphere. Android is the most used OS in smartphones during these days of transformation. But when did something l
15+ min read
Best Way to Become Android Developer â A Complete RoadmapAndroid is an open-source operating system, based on the Linux kernel and used in mobile devices like smartphones, tablets, etc. Further, it was developed for smartwatches and Android TV. Each of them has a specialized interface. Android has been one of the best-selling OS for smartphones. Android O
7 min read
Android Development Prerequisites [2025] - Things to Learn Before Android DevelopmentHave you ever wondered how your phone's appsâsuch as those for making calls, playing games, or checking the weatherâare created? Android Development is the procedure used to generate these apps. The Android operating system powers the majority of phones, just like an engine powers a car. To ensure t
8 min read
Android App Development Fundamentals for BeginnersAndroid is an operating system that is built basically for Mobile phones. It is based on the Linux Kernel and other open-source software and is developed by Google. It is used for touchscreen mobile devices such as smartphones and tablets. But nowadays these are used in Android Auto cars, TV, watche
6 min read
Android ArchitectureAndroid architecture contains a different number of components to support any Android device's needs. Android software contains an open-source Linux Kernel having a collection of a number of C/C++ libraries which are exposed through application framework services. Among all the components Linux Kern
5 min read
Android System ArchitectureThe Android software stack generally consists of a Linux kernel and a collection of C/C++ libraries that are exposed through an application framework that provides services, and management of the applications and run time. Linux KernelAndroid was created on the open-source kernel of Linux. One main
3 min read
Android Boot ProcessBooting Process In computing, booting is starting up a computer or computer appliance until it can be used. It can be initiated by hardware such as a button press, or by software command. After the power is switched on the computer is relatively dumb, and can read only part of its storage called Rea
4 min read
Difference between Java and Kotlin in Android with ExamplesKotlin KOTLIN is a cross platform, statically types, general purpose programming language with type inference. KOTLIN is designed to interoperate fully with java but type inference allows its syntax to be more concise.KOTLIN is sponsored by JetBrains and Google through the Kotlin Foundation. Java JA
3 min read
Interesting Facts About AndroidAndroid is a Mobile Operating System that was released on 23, September 2008. Android is free, open-source operating system and is based on modified version of Linux kernel. Open Handset Alliance (OHA) developed the Android and Google commercially sponsored it. It is mainly designed for touchscreen
3 min read
Software Setup and Configuration
Android Studio Tutorial It is stated that "If you give me six hours to chop down a tree then I will spend the first four hours in sharpening the axe". So in the Android Development World if we consider Android Development as the tree then Android Studio should be the axe. Yes, if you are starting Android Development then y
9 min read
File Structure & Components
Core Topics
How Does Android App Work?Developing an android application involves several processes that happen in a sequential manner. After writing the source code files, when developers click the Run button on the Android studio, plenty of operations and process starts at the backend. Every operation happening in the background is a c
7 min read
Activity Lifecycle in Android with Demo AppIn Android, an activity is referred to as one screen in an application. It is very similar to a single window of any desktop application. An Android app consists of one or more screens or activities. Each activity goes through various stages or a lifecycle and is managed by activity stacks. So when
9 min read
Introduction to GradleGradle is an open-source construction tool that is capable of controlling the development tasks with compilation and packaging, including testing, deployment and publishing. It is an automation tool that is based on Apache Ant and Apache Maven. This tool is capable of developing applications with in
4 min read
What is Context in Android?Android Applications are popular for a long time and it is evolving to a greater level as users' expectations are that they need to view the data that they want in an easier smoother view. Hence, the android developers must know the important terminologies before developing the app. In Android Progr
9 min read
Bundle in Android with ExampleIt is known that Intents are used in Android to pass to the data from one activity to another. But there is one another way, that can be used to pass the data from one activity to another in a better way and less code space ie by using Bundles in Android. Android Bundles are generally used for passi
6 min read
Activity State Changes In Android with ExamplePrerequisites: Activity lifecycle in android As it is known that every Android app has at least one activity associated with it. When the application begins to execute and runs, there are various state changes that activity goes through. Different events some user-triggered and some system triggered
6 min read
Processes and Application Lifecycle in AndroidAs an android developer, if one does not know the application lifecycle of android application or does not have in-depth knowledge about it, there are very high chances that the application will not have a good user experience. Not having proper knowledge of the application lifecycle will not affect
7 min read
Desugaring in AndroidGoogle has officially announced Kotlin as a recommended language for Android Development and that's why so many developers are switching from Java to Kotlin for Android development. So day by day new APIs are been introduced in Android by the Google Team and which are available in newer versions of
4 min read
Difference Between AndroidX and Android Support LibrariesSupport library packages in Android are a set of code libraries whose prime purpose is to provide backward-compatibility to the code and Android API framework. In the real world, there is a strong possibility that an application that is developed on the latest Android version is used on an older ver
3 min read
Memory Leaks in AndroidA memory leak is basically a failure of releasing unused objects from the memory. As a developer one does not need to think about memory allocation, memory deallocation, and garbage collection. All of these are the automatic process that the garbage collector does by itself, but the situation become
7 min read
Layout & View
Layouts in Android UI DesignLayout Managers (or simply layouts) are said to be extensions of the ViewGroup class. They are used to set the position of child Views within the UI we are building. We can nest the layouts, and therefore we can create arbitrarily complex UIs using a combination of layouts.There is a number of layou
3 min read
Android UI LayoutsLayouts in Android define the user interface and hold UI controls or widgets that appear on the screen of an application. Every Android application consists of View and ViewGroup elements. Since an application contains multiple activitiesâeach representing a separate screenâevery activity has multip
5 min read
LinearLayout and its Important Attributes with Examples in AndroidLinearLayout is one of the most basic layouts in android studio, that arranges multiple sub-views (UI elements) sequentially in a single direction i.e. horizontal or vertical manner by specifying the android:orientation attribute. If one applies android:orientation="vertical" then elements will be a
3 min read
Android LinearLayout in KotlinLinearLayout in Android is a ViewGroup subclass, used to arrange child view elements one by one in a singular direction either horizontally or vertically based on the orientation attribute. We can specify the linear layout orientation using the android:orientation attribute. All the child elements a
2 min read
Android RelativeLayout in KotlinRelativeLayout in Android is a ViewGroup subclass, that allows users to position child views relative to each other (e.g., view A to the right of view B) or relative to the parent (e.g., aligned to the top of the parent). Instead of using LinearLayout, we have to use RelativeLayout to design the use
4 min read
ConstraintLayout in AndroidConstraintLayout is the most advanced layout in Android that lets you create complex and responsive UIs while minimizing nested views due to its flat view hierarchy. ConstraintLayout is similar to that of other View Groups which we have seen in Android such as RelativeLayout, LinearLayout, and many
6 min read
TextView widget in Android with ExamplesWidget refers to the elements of the UI (User Interface) that help the user interact with the Android App. TextView is one of many such widgets which can be used to improve the UI of the app. TextView refers to the widget which displays some text on the screen based on the layout, size, colour, etc
5 min read
TextView in KotlinAndroid TextView is simply a view that are used to display the text to the user and optionally allow us to modify or edit it. First of all, open Kotlin project in Android Studio. Following steps are used to create Steps to Implement TextViewSteps by Step implementation for creating an application wh
3 min read
Working With the TextView in AndroidTextView in Android is one of the basic and important UI elements. This plays a very important role in the UI experience and depends on how the information is displayed to the user. This TextView widget in Android can be dynamized in various contexts. For example, if the important part of the inform
7 min read
Autosizing TextView in AndroidIf the user is giving the input and the input needs to be shown as TextView and if the user inputs the stuff which can go out of the screen, then in this case the font TextView should be decreased gradually. So, in this article, it has been discussed how the developer can reduce the size of TextView
6 min read
Button
Intent and Intent Filters
What is Intent in Android?In Android, it is quite usual for users to witness a jump from one application to another as a part of the whole process, for example, searching for a location on the browser and witnessing a direct jump into Google Maps or receiving payment links in Messages Application (SMS) and on clicking jumpin
4 min read
Implicit and Explicit Intents in Android with ExamplesPre-requisites: Android App Development Fundamentals for Beginners Guide to Install and Set up Android Studio Android | Starting with the first app/android project Android | Running your first Android app This article aims to tell about the Implicit and Explicit intents and how to use them in an and
6 min read
How to Send Data From One Activity to Second Activity in Android?This article aims to tell and show how to "Send the data from one activity to second activity using Intent" . In this example, we have two activities, activity_first which are the source activity, and activity_second which is the destination activity. We can send the data using the putExtra() method
7 min read
How to open dialer in Android through Intent?The phone dialer is an activity available with the Android operating system to call a number. Usually, such activity may or may not have an EditText, for taking the number as input, and a Call button. When the user presses the Call button, it invokes the dialer app activity. Use of 'tel:' prefix is
3 min read
Creating Multiple Screen Applications in AndroidThis article shows how to create an android application to move from one activity to another using the concept of Explicit Intents. Below are the steps for Creating a Simple Android Application to move from one activity to another activity.Step By Step ImplementationStep 1: Create a New Project in A
6 min read
How to Open Camera Through Intent and Display Captured Image in Android?The purpose of this article is to show how to open a Camera from inside an App and click the image and then display this image inside the same app. An android application has been developed in this article to achieve this. The opening of the Camera from inside our app is achieved with the help of th
6 min read
Toast & RecyclerView
Toasts for Android StudioA toast provides a simple popup message that is displayed on the current activity UI screen (e.g. Main Activity). Example: Syntax:// To get ContextContext context = getApplicationContext(); // Message to displayString text = "Toast message"; // Toast time duration, can also set manual value int dura
2 min read
What is Toast and How to Use it in Android with Examples?Pre-requisites: Android App Development Fundamentals for BeginnersGuide to Install and Set up Android StudioAndroid | Starting with the first app/android projectAndroid | Running your first Android appWhat is Toast in Android? A Toast is a feedback message. It takes a very little space for displayin
6 min read
Android Toast in KotlinA Toast is a short alert message shown on the Android screen for a short interval of time. Android Toast is a short popup notification which is used to display information when we perform any operation in our app. In this tutorial, we shall not just limit ourselves by creating a lame toast but also
3 min read
How to Change Toast font in Android?A Toast is a feedback message. It takes a very little space for displaying while overall activity is interactive and visible to the user. It disappears after a few seconds. It disappears automatically. If user wants permanent visible message, Notification can be used. Note: Toast disappears automati
3 min read
How to add a custom styled Toast in AndroidA Toast is a feedback message. It takes very little space for displaying and it is displayed on top of the main content of an activity, and only remains visible for a short time period. This article explains how to create Custom Toast messages, which has custom background, image, icon, etc, which ar
4 min read
RecyclerView in Android with ExampleRecyclerView is a ViewGroup added to the android studio as a successor of the GridView and ListView. It is an improvement on both of them and can be found in the latest v-7 support packages. It has been created to make possible construction of any lists with XML layouts as an item which can be custo
7 min read
Android | Horizontal RecyclerView with ExamplesRecycler View is a ViewGroup added to Android Studio as a successor of the GridView and ListView. It is an improvement on both of them and can be found in the latest v-7 support packages. It has been created to make possible construction of any lists with XML layouts as an item which can be customiz
4 min read
How to create a nested RecyclerView in AndroidA nested RecyclerView is an implementation of a RecyclerView within a RecyclerView. An example of such a layout can be seen in a variety of apps such as the Play Store, where the outer (parent) RecyclerView is of vertical orientation, whereas the inner (child) RecyclerViews are of horizontal orienta
5 min read
How to Create RecyclerView with Multiple ViewType in Android?RecyclerView forms a crucial part of the UI in Android app development. It is especially important to optimize memory consumption when displaying a long list of items. A RecyclerView inflates a customized list of items, which can have either similar layouts or multiple distinct layouts. Here, we dev
6 min read
RecyclerView using ListView in Android With ExampleRecyclerView is a more flexible and advanced version of ListView and GridView. RecyclerView is used for providing a limited window to a large data set, which means it is used to display a large amount of data that can be scrolled very efficiently by maintaining a limited number of Views. In Recycler
5 min read