Animal App
ANIMAL APP
Recycler View and Adapter
OUPUT
Set the Colour in Resources
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#095922</color>
<color name="colorPrimaryDark">#002f11</color>
<color name="colorAccent">#79119900</color>
</resources>
<resources>
<color name="colorPrimary">#095922</color>
<color name="colorPrimaryDark">#002f11</color>
<color name="colorAccent">#79119900</color>
</resources>
Crete Layout Activity - animal_row
Download CardView
<?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"
android:elevation="8dp"
app:cardCornerRadius="9dp"
android:layout_margin="10dp"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:padding="10dp"
android:orientation="horizontal">
<androidx.cardview.widget.CardView
android:layout_width="85dp"
android:layout_height="75dp"
android:elevation="5dp"
app:cardCornerRadius="9dp">
<ImageView
android:id="@+id/img_animal"
android:layout_width="match_parent"
android:scaleType="centerCrop"
android:layout_height="match_parent" />
</androidx.cardview.widget.CardView>
<TextView
android:id="@+id/txt_animal_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="95dp"
android:fontFamily="casual"
android:text="Text"
android:layout_gravity="center"
android:textStyle="bold"
android:textSize="22dp"
android:textColor="@android:color/white"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
activity_main.xml
Download Recycler View
<?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"
android:background="@color/colorAccent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Animals"
android:textColor="@android:color/black"
android:textSize="25dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
</androidx.constraintlayout.widget.ConstraintLayout>
Create New java Class
Animal.java
public class Animal {
private String name;
private int image;
public Animal(String name, int image){
this.name = name;
this.image = image;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
}
Create New Class
AnimalAdapter.java
import android.content.Context;
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 java.util.ArrayList;
public class AnimalAdapter extends RecyclerView.Adapter<AnimalAdapter.AnimalRowHolder> {
ArrayList<Animal> animalData;
Context context;
MyClickInterface myClickInterface;
public AnimalAdapter(ArrayList<Animal> animalData, Context context, MyClickInterface myClickInterface){
this.context = context;
this.animalData = animalData;
this.myClickInterface = myClickInterface;
}
@NonNull
@Override
public AnimalRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.animal_row,parent,false);
return new AnimalRowHolder(view);
}
@Override
public void onBindViewHolder(@NonNull AnimalRowHolder holder, int position) {
holder.txtAnimalName.setText(animalData.get(position).getName());
holder.imgAnimal.setImageResource(animalData.get(position).getImage());
}
@Override
public int getItemCount() {
return animalData.size();
}
class AnimalRowHolder extends RecyclerView.ViewHolder{
TextView txtAnimalName;
ImageView imgAnimal;
public AnimalRowHolder(@NonNull View itemView) {
super(itemView);
txtAnimalName = itemView.findViewById(R.id.txt_animal_name);
imgAnimal = itemView.findViewById(R.id.img_animal);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
myClickInterface.onItemClick(getAdapterPosition());
}
});
}
}
interface MyClickInterface{
void onItemClick(int postionOfTheAnimal);
}
}
Main_Activity.java
public class MainActivity extends AppCompatActivity implements AnimalAdapter.MyClickInterface {
RecyclerView recyclerView;
ArrayList<Animal> animals;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recycler_view);
animals = new ArrayList<>();
animals.add(new Animal("Dolphin",R.drawable.dolphin));
animals.add(new Animal("Lion",R.drawable.lion));
animals.add(new Animal("Owl",R.drawable.owl));
animals.add(new Animal("Parrot",R.drawable.parrot));
animals.add(new Animal("Rabbit",R.drawable.rabbit));
animals.add(new Animal("Tiger",R.drawable.tiger));
animals.add(new Animal("Turtle",R.drawable.turtle));
animals.add(new Animal("Wolf",R.drawable.wolf));
animals.add(new Animal("Wolf",R.drawable.wolf));
animals.add(new Animal("Wolf",R.drawable.wolf));
animals.add(new Animal("Wolf",R.drawable.wolf));
animals.add(new Animal("Wolf",R.drawable.wolf));
animals.add(new Animal("Wolf",R.drawable.wolf));
AnimalAdapter animalAdapter = new AnimalAdapter(animals,this,this);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(animalAdapter);
}
@Override
public void onItemClick(int postionOfTheAnimal) {
Toast.makeText(this, "Clicked "+animals.get(postionOfTheAnimal).getName(), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this,AnimalInfo.class);
intent.putExtra("image",animals.get(postionOfTheAnimal).getImage());
intent.putExtra("name",animals.get(postionOfTheAnimal).getName());
startActivity(intent);
}
}
Create New Activity
activity_animal_info.xml
<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"
android:background="@color/colorAccent"
tools:context=".AnimalInfo">
<androidx.cardview.widget.CardView
android:id="@+id/cardView2"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_marginTop="52dp"
app:cardCornerRadius="125dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" >
<ImageView
android:id="@+id/imgCircled"
android:layout_width="match_parent"
android:scaleType="centerCrop"
android:layout_height="match_parent"/>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="350dp"
android:layout_marginTop="15dp"
app:cardCornerRadius="9dp"
app:layout_constraintEnd_toEndOf="@+id/cardView2"
app:cardBackgroundColor="@android:color/transparent"
app:layout_constraintStart_toStartOf="@+id/cardView2"
app:layout_constraintTop_toBottomOf="@+id/cardView2"
android:layout_height="250dp">
<TextView
android:id="@+id/animal"
android:layout_width="match_parent"
tools:text="SOme text"
android:background="@color/colorPrimary"
android:textAlignment="center"
android:layout_height="match_parent"
android:padding="10dp" />
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
AnimalInfo.java
public class AnimalInfo extends AppCompatActivity {
ImageView img;
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_animal_info);
img = findViewById(R.id.imgCircled);
txt = findViewById(R.id.animal);
Intent intent = getIntent();
img.setImageResource(intent.getIntExtra("image",R.drawable.ic_launcher_foreground));
txt.setText(intent.getStringExtra("name"));
}
}
Comments
Post a Comment