
- Android Basics
- Android - Home
- Android - Overview
- Android - Environment Setup
- Android - Architecture
- Android - Application Components
- Android - Hello World Example
- Android - Resources
- Android - Activities
- Android - Services
- Android - Broadcast Receivers
- Android - Content Providers
- Android - Fragments
- Android - Intents/Filters
- Android - User Interface
- Android - UI Layouts
- Android - UI Controls
- Android - Event Handling
- Android - Styles and Themes
- Android - Custom Components
- Android Advanced Concepts
- Android - Drag and Drop
- Android - Notifications
- Location Based Services
- Android - Sending Email
- Android - Sending SMS
- Android - Phone Calls
- Publishing Android Application
- Android Useful Examples
- Android - Alert Dialoges
- Android - Animations
- Android - Audio Capture
- Android - AudioManager
- Android - Auto Complete
- Android - Best Practices
- Android - Bluetooth
- Android - Camera
- Android - Clipboard
- Android - Custom Fonts
- Android - Data Backup
- Android - Developer Tools
- Android - Emulator
- Android - Facebook Integration
- Android - Gestures
- Android - Google Maps
- Android - Image Effects
- Android - ImageSwitcher
- Android - Internal Storage
- Android - JetPlayer
- Android - JSON Parser
- Android - Linkedin Integration
- Android - Loading Spinner
- Android - Localization
- Android - Login Screen
- Android - MediaPlayer
- Android - Multitouch
- Android - Navigation
- Android - Network Connection
- Android - NFC Guide
- Android - PHP/MySQL
- Android - Progress Circle
- Android - ProgressBar
- Android - Push Notification
- Android - RenderScript
- Android - RSS Reader
- Android - Screen Cast
- Android - SDK Manager
- Android - Sensors
- Android - Session Management
- Android - Shared Preferences
- Android - SIP Protocol
- Android - Spelling Checker
- Android - SQLite Database
- Android - Support Library
- Android - Testing
- Android - Text to Speech
- Android - TextureView
- Android - Twitter Integration
- Android - UI Design
- Android - UI Patterns
- Android - UI Testing
- Android - WebView Layout
- Android - Wi-Fi
- Android - Widgets
- Android - XML Parsers
- Android Useful Resources
- Android - Questions and Answers
- Android - Useful Resources
- Android - Discussion
Working with Recycler View in Android App
This example demonstrates about Working with Recycler View in Android App
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.
Step 2 − Add the following code to res/layout/activity_main.xml.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rlMain" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="16dp" android:orientation="vertical"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
Step 3 − Add the following code to src/MainActivity.java
package app.com.sample; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.DefaultItemAnimator; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private List<MovieModel> movieList = new ArrayList<>(); private MoviesAdapter mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RecyclerView recyclerView = findViewById(R.id.recyclerView); mAdapter = new MoviesAdapter(movieList); LinearLayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext()); recyclerView.setLayoutManager(mLayoutManager); recyclerView.setItemAnimator(new DefaultItemAnimator()); recyclerView.setAdapter(mAdapter); prepareMovieData(); } private void prepareMovieData() { MovieModel movie = new MovieModel("Mad Max: Fury Road", "Action & Adventure", "2015"); movieList.add(movie); movie = new MovieModel("Inside Out", "Animation, Kids & Family", "2015"); movieList.add(movie); movie = new MovieModel("Star Wars: Episode VII - The Force Awakens", "Action", "2015"); movieList.add(movie); movie = new MovieModel("Shaun the Sheep", "Animation", "2015"); movieList.add(movie); movie = new MovieModel("The Martian", "Science Fiction & Fantasy", "2015"); movieList.add(movie); movie = new MovieModel("Mission: Impossible Rogue Nation", "Action", "2015"); movieList.add(movie); movie = new MovieModel("Up", "Animation", "2009"); movieList.add(movie); movie = new MovieModel("Star Trek", "Science Fiction", "2009"); movieList.add(movie); movie = new MovieModel("The LEGO MovieModel", "Animation", "2014"); movieList.add(movie); movie = new MovieModel("Iron Man", "Action & Adventure", "2008"); movieList.add(movie); movie = new MovieModel("Aliens", "Science Fiction", "1986"); movieList.add(movie); movie = new MovieModel("Chicken Run", "Animation", "2000"); movieList.add(movie); movie = new MovieModel("Back to the Future", "Science Fiction", "1985"); movieList.add(movie); movie = new MovieModel("Raiders of the Lost Ark", "Action & Adventure", "1981"); movieList.add(movie); movie = new MovieModel("Goldfinger", "Action & Adventure", "1965"); movieList.add(movie); movie = new MovieModel("Guardians of the Galaxy", "Science Fiction & Fantasy", "2014"); movieList.add(movie); mAdapter.notifyDataSetChanged(); } }
Step 4 − Add the following code to src/MovieModel.java
package app.com.sample; public class MovieModel { private String title, genre, year; public MovieModel() { } public MovieModel(String title, String genre, String year) { this.title = title; this.genre = genre; this.year = year; } public String getTitle() { return title; } public void setTitle(String name) { this.title = name; } public String getYear() { return year; } public void setYear(String year) { this.year = year; } public String getGenre() { return genre; } public void setGenre(String genre) { this.genre = genre; } }
Step 5 − Add the following code to res/layout/movies_list.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="100dp" android:layout_margin="8dp"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:padding="8dp"> <TextView android:id="@+id/title" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_toStartOf="@+id/year" android:textColor="#222222" android:textSize="16sp" android:textStyle="bold" /> <TextView android:id="@+id/year" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:textColor="#999999" /> <TextView android:id="@+id/genre" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" /> </RelativeLayout> </androidx.cardview.widget.CardView>
Step 6 − Add the following code to src/MoviesAdapter.java
package app.com.sample; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import java.util.List; public class MoviesAdapter extends RecyclerView.Adapter<MoviesAdapter.MyViewHolder> { private List<MovieModel> moviesList; class MyViewHolder extends RecyclerView.ViewHolder { TextView title, year, genre; MyViewHolder(View view) { super(view); title = view.findViewById(R.id.title); genre = view.findViewById(R.id.genre); year = view.findViewById(R.id.year); } } public MoviesAdapter(List<MovieModel> moviesList) { this.moviesList = moviesList; } @NonNull @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View itemView = LayoutInflater.from(parent.getContext()) .inflate(R.layout.movie_list, parent, false); return new MyViewHolder(itemView); } @Override public void onBindViewHolder(MyViewHolder holder, int position) { MovieModel movie = moviesList.get(position); holder.title.setText(movie.getTitle()); holder.genre.setText(movie.getGenre()); holder.year.setText(movie.getYear()); } @Override public int getItemCount() { return moviesList.size(); } }
Step 7 − Add the following code to androidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="app.com.sample"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and Click Run Icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen –
Click here to download the project code.
- Related Articles
- Android Working with Recycler View
- Android working with Card View and Recycler View
- Recycler view item animate when scrolling in Android
- Working with RecyclerView in an Android App using Kotlin?
- How to set background color of a view in Android App
- How to get fling gesture detection working in an android app?
- How to get fling gesture detection working in an Android App using Kotlin?
- How to work with Camera in an Android App?
- Can I make an Android app with Python?
- How to share app data with other applications in android?
- Android app Vulnerability Scanner
- How to work with Camera in an Android App using Kotlin?
- What is search view in android?
- How to make custom dialog with custom dialog view actions in android?
- How to view video transcript on YouTube Mobile App?
