Movie Streaming App Part 1: Setup Model and Adapter Class | Android Studio Tutorial
3 min read
1 hour ago
Published on Oct 28, 2024
This response is partially generated with the help of AI. It may contain inaccuracies.
Table of Contents
Introduction
In this tutorial, we will set up the model and adapter class for a Movie Streaming App in Android Studio. This project aims to create a user interface similar to popular streaming services like Amazon Prime Video. By the end of this guide, you will have a foundational structure for displaying movie data in your app.
Step 1: Create a New Android Project
- Open Android Studio and select "Start a new Android Studio project."
- Choose an "Empty Activity" template and click "Next."
- Fill in the application name (e.g., "MovieStreamingApp").
- Set the package name (e.g., "com.example.moviestreamingapp").
- Choose the appropriate save location and click "Finish."
Step 2: Set Up Dependencies
To work with RecyclerView and other components, you need to add dependencies:
- Open
build.gradle (Module: app)
file. - Under the
dependencies
section, add the following lines:implementation 'androidx.recyclerview:recyclerview:1.2.0' implementation 'com.squareup.picasso:picasso:2.71828'
- Click "Sync Now" to download the dependencies.
Step 3: Create Model Class for Movie Data
- Right-click on the package name in the
java
folder and selectNew > Java Class
. - Name it
Movie
. This class will represent the movie data. - Include the following attributes and methods:
public class Movie { private String title; private String imageUrl; public Movie(String title, String imageUrl) { this.title = title; this.imageUrl = imageUrl; } public String getTitle() { return title; } public String getImageUrl() { return imageUrl; } }
- This class will help manage movie details.
Step 4: Create Adapter Class for RecyclerView
- Right-click on your package name again and create a new Java class called
MovieAdapter
. - Extend the class from
RecyclerView.Adapter<MovieAdapter.MovieViewHolder>
. - Implement the necessary methods:
onCreateViewHolder
,onBindViewHolder
, andgetItemCount
. - Inside
MovieAdapter
, create a static inner classMovieViewHolder
to hold references to the views. - Example structure for
MovieAdapter
:public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.MovieViewHolder> { private List<Movie> movieList; public MovieAdapter(List<Movie> movieList) { this.movieList = movieList; } @Override public MovieViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.movie_item, parent, false); return new MovieViewHolder(view); } @Override public void onBindViewHolder(MovieViewHolder holder, int position) { Movie movie = movieList.get(position); holder.title.setText(movie.getTitle()); Picasso.get().load(movie.getImageUrl()).into(holder.image); } @Override public int getItemCount() { return movieList.size(); } public static class MovieViewHolder extends RecyclerView.ViewHolder { TextView title; ImageView image; public MovieViewHolder(View itemView) { super(itemView); title = itemView.findViewById(R.id.movie_title); image = itemView.findViewById(R.id.movie_image); } } }
Step 5: Design Movie Item Layout
- Create a new XML layout file named
movie_item.xml
in theres/layout
directory. - Define the layout for displaying individual movie items:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/movie_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:padding="16dp" /> <ImageView android:id="@+id/movie_image" android:layout_width="match_parent" android:layout_height="wrap_content" android:scaleType="centerCrop" /> </LinearLayout>
Conclusion
You have successfully set up the model and adapter class for your Movie Streaming App in Android Studio. You can now move on to implementing the RecyclerView in your main activity to display the list of movies. Ensure to follow the next parts of the tutorial for further enhancements and functionalities. Happy coding!