
plugins {
id 'com.android.application'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
defaultConfig {
applicationId "com.example.retrofitapp2"
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.2'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.github.bumptech.glide:glide:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
}
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
item 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="wrap_content">
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="44dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="Title"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="128dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
tools:srcCompat="@tools:sample/avatars" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="duration"
app:layout_constraintStart_toStartOf="@+id/textView2"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="19dp"
android:layout_marginTop="8dp"
android:text="Category"
app:layout_constraintEnd_toEndOf="@+id/imageView"
app:layout_constraintStart_toStartOf="@+id/imageView"
app:layout_constraintTop_toBottomOf="@+id/imageView" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="19dp"
android:layout_marginBottom="16dp"
android:text="Release Date"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/textView"
app:layout_constraintStart_toStartOf="@+id/textView"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<RatingBar
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="57dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.407"
app:layout_constraintStart_toEndOf="@+id/imageView" />
</androidx.constraintlayout.widget.ConstraintLayout>
Adapter :-
package com.example.retrofitapp2;
import android.content.Context;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import java.util.List;
public class Adaptery extends RecyclerView.Adapter<Adaptery.MyViewHolder> {
private Context mContext;
private List<Movie> moviesList;
public Adaptery(Context mContext, List<Movie> moviesList) {
this.mContext = mContext;
this.moviesList = moviesList;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v ;
LayoutInflater layoutInflater = LayoutInflater.from(mContext);
v = layoutInflater.inflate(R.layout.movie_item, parent, false);
return new MyViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.title.setText(moviesList.get(position).getTitle());
// The Trick: Getting data from the nested json object
holder.duration.setText(moviesList.get(position).getMoreDetails().getDuration());
holder.category.setText(moviesList.get(position).getMoreDetails().getCategory());
holder.release.setText(moviesList.get(position).getMoreDetails().getRelease());
// Adding Glide library to display the images
Glide.with(mContext)
.load(moviesList.get(position).getPoster())
.into(holder.img);
// Setting the rating bar value
// Rating bar is 5 and ratings is over 10
holder.ratingBar.setRating((Float.parseFloat(String.valueOf(moviesList.get(position).getRating())))/2);
}
@Override
public int getItemCount() {
return moviesList.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
TextView title;
TextView duration;
ImageView img;
RatingBar ratingBar;
TextView category;
TextView release;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.textView2);
duration = itemView.findViewById(R.id.textView3);
img = itemView.findViewById(R.id.imageView);
ratingBar = itemView.findViewById(R.id.ratingBar);
category = itemView.findViewById(R.id.textView);
release = itemView.findViewById(R.id.textView4);
}
}
}
JSON Response :-
package com.example.retrofitapp2;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class JSONResponse {
private Movie[] moviz;
public Movie[] getMoviz() {
return moviz;
}
public void setMoviz(Movie[] moviz) {
this.moviz = moviz;
}
}
Model class :-
package com.example.retrofitapp2;
public class MoreDetails {
// Model class for details data
private String release;
private String category;
private String duration;
// Getters
public String getRelease() {
return release;
}
public String getCategory() {
return category;
}
public String getDuration() {
return duration;
}
// COnstructor
public MoreDetails(String release, String category, String duration) {
this.release = release;
this.category = category;
this.duration = duration;
}
}
Model class :-
package com.example.retrofitapp2;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Movie {
// Model Class
private int id;
private String title;
private float rating;
private String poster;
// we need to make object of class details
// Different name from json, so we need to serialize it
@SerializedName("Details")
private MoreDetails moreDetails;
// Constructor
public Movie(int id, String title, float rating, String poster, MoreDetails moreDetails) {
this.id = id;
this.title = title;
this.rating = rating;
this.poster = poster;
this.moreDetails = moreDetails;
}
// Getters
public String getTitle() {
return title;
}
public float getRating() {
return rating;
}
public String getPoster() {
return poster;
}
public MoreDetails getMoreDetails() {
return moreDetails;
}
}
Interface :-
package com.example.retrofitapp2;
import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;
public interface MovieApi {
@GET("v3/6360ab61-dfcf-4af1-b7b2-ee52d9081d7a")
Call<JSONResponse> getMovies();
}
MainActivity :-
package com.example.retrofitapp2;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class MainActivity extends AppCompatActivity {
// Fixed Part of url: https://run.mocky.io/
// Relative/Variable part of url: v3/6360ab61-dfcf-4af1-b7b2-ee52d9081d7a
RecyclerView recyclerView;
List<Movie> movieList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerView);
movieList = new ArrayList<>();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://run.mocky.io/")
.addConverterFactory(GsonConverterFactory.create())
.build();
MovieApi movieApi = retrofit.create(MovieApi.class);
Call<JSONResponse> call = movieApi.getMovies();
call.enqueue(new Callback<JSONResponse>() {
@Override
public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {
JSONResponse jsonResponse = response.body();
movieList = new ArrayList<>(Arrays.asList(jsonResponse.getMoviz()));
PutDataIntoRecyclerView(movieList);
}
@Override
public void onFailure(Call<JSONResponse> call, Throwable t) {
}
});
}
private void PutDataIntoRecyclerView(List<Movie> movieList) {
Adaptery adaptery = new Adaptery(this, movieList);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adaptery);
}
// Amazing GUYS!!!
// We are professional developers
// Now, we are going to make more complex APPS
// ;)
}
Comments
Post a Comment