Retrofit with image in recycle View

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>




xml layout:-  

<?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:text="TextView"
android:textSize="30sp"
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="TextView"
app:layout_constraintStart_toStartOf="@+id/textView2"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
</androidx.constraintlayout.widget.ConstraintLayout>






Adapter Class :-

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.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.id.setText(""+ moviesList.get(position).getId());
holder.title.setText(moviesList.get(position).getName());


// Adding Glide library to display the images
Glide.with(mContext)
.load(moviesList.get(position).getImage())
.into(holder.img);


}

@Override
public int getItemCount() {
return moviesList.size();
}


public static class MyViewHolder extends RecyclerView.ViewHolder{

TextView title;
TextView id;
ImageView img;

public MyViewHolder(@NonNull View itemView) {
super(itemView);

title = itemView.findViewById(R.id.textView2);
id = itemView.findViewById(R.id.textView3);
img = itemView.findViewById(R.id.imageView);


}
}
}





Model Class :-


package com.example.retrofitapp2;

public class Movie {
// Model Class
private int id;
private String name;
private String image;

public int getId() {
return id;
}

public String getName() {
return name;
}

public String getImage() {
return image;
}
}




Interface Class :-


package com.example.retrofitapp2;

import java.util.List;

import retrofit2.Call;
import retrofit2.http.GET;

public interface MovieApi {

@GET("v3/c38ef967-0c43-4cbb-b4a0-1f330e2d33b7")
Call<List<Movie>> 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.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {

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<List<Movie>> call = movieApi.getMovies();

call.enqueue(new Callback<List<Movie>>() {
@Override
public void onResponse(Call<List<Movie>> call, Response<List<Movie>> response) {



List<Movie> movies = response.body();


for (Movie movie : movies){
movieList.add(movie);
}

PutDataIntoRecyclerView(movieList);

}

@Override
public void onFailure(Call<List<Movie>> 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

Popular posts from this blog

Gride View in Firebase

Register in kotlin

Check Permission in Android Studio