Value Added (Cardview) Aim:-To Show Images in Cardview Pre-Requisite: - Android Studio, SDK Code: / Activity - Main - XML
Value Added (Cardview) Aim:-To Show Images in Cardview Pre-Requisite: - Android Studio, SDK Code: / Activity - Main - XML
(Cardview)
Code:-
/*activity_main.xml*\
<FrameLayout
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:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
</FrameLayout>
/*MainActivity.java*\
package com.example.cardview;
import
androidx.appcompat.app.AppCompatActiv
ity;
import androidx.fragment.app.Fragment;
import
androidx.fragment.app.FragmentManager;
import android.os.Bundle;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm =
getSupportFragmentManager();
Fragment fragment =
fm.findFragmentById(R.id.fragmentContain
er);
if (fragment == null) {
fm.beginTransaction()
.add(R.id.fragmentContainer, fragment)
.commit();
/*recycler_items.xml*\
<androidx.cardview.widget.CardView
xmlns:android="http://
schemas.android.com/apk/res/android"
xmlns:card_view="http://
schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="250dip"
android:orientation="vertical"
android:weightSum="4">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="3.2"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
>
<ImageView
android:id="@+id/coverImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:scaleType="centerCrop"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left|bottom"
android:background="@android:drawable/
screen_background_dark_tra
nsparent"
android:orientation="vertical">
<TextView
android:id="@+id/titleTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:textSize="18sp"
android:textColor="#FFFFFF"
android:textStyle="bold" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.8"
android:gravity="center|right"
android:orientation="horizontal">
<ImageView
android:id="@+id/likeImageView"
android:layout_width="60dip"
android:layout_height="60dip"
android:padding="8dp"
android:src="@drawable/download1" />
<ImageView
android:id="@+id/shareImageView"
android:layout_width="60dip"
android:layout_height="60dip"
android:padding="8dp"
android:src="@drawable/download2" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
/*fragment_card.xml*\
<RelativeLayout
xmlns:android="http://
schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerVie
w
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
/*Cardfragment.java*\
package com.example.cardview;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import
androidx.recyclerview.widget.LinearLayout
Manager;
import
androidx.recyclerview.widget.RecyclerView
;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
RecyclerView MyRecyclerView;
Mahal","Colosseum"};
int Images[] =
{R.drawable.download1,R.drawable.downl
oad2,R.drawable.download3,
R.drawable.download4,R.drawable.downlo
ad5,R.drawable.download6,R
.drawable.download7};
ARG_ITEM_NUMBER
public CardFragment() {
@Override
super.onCreate(savedInstanceState);
initializeList();
@Override
container,
Bundle savedInstanceState) {
View view =
inflater.inflate(R.layout.fragment_card,
container, false);
MyRecyclerView = (RecyclerView)
view.findViewById(R.id.cardView);
MyRecyclerView.setHasFixedSize(true);
LinearLayoutManager MyLayoutManager =
new
LinearLayoutManager(getActivity());
MyLayoutManager.setOrientation(LinearLa
youtManager.VERTICAL);
MyRecyclerView.setAdapter(new
MyAdapter(listitems));
MyRecyclerView.setLayoutManager(MyLay
outManager);
return view;
@Override
super.onActivityCreated(savedInstanceStat
e);
RecyclerView.Adapter<MyViewHolder> {
public
MyAdapter(ArrayList<WonderModel> Data)
{
list = Data;
@Override
public MyViewHolder
onCreateViewHolder(ViewGroup
parent,int viewType) {
View view =
LayoutInflater.from(parent.getContext())
.inflate(R.layout.recycle_items, parent,
false);
return holder;
@Override
int position) {
holder.titleTextView.setText(list.get(position)
.getCardName());
holder.coverImageView.setImageResource(
list.get(position).getIma
geResourceId());
holder.coverImageView.setTag(list.get(posit
ion).getImageResource
Id());
holder.likeImageView.setTag(R.drawable.d
ownload1);
@Override
return list.size();
public MyViewHolder(View v) {
super(v);
titleTextView = (TextView)
v.findViewById(R.id.titleTextView);
coverImageView = (ImageView)
v.findViewById(R.id.coverImageView);
likeImageView = (ImageView)
v.findViewById(R.id.likeImageView);
shareImageView = (ImageView)
v.findViewById(R.id.shareImageView);
likeImageView.setOnClickListener(new
View.OnClickListener() {
@Override
int id = (int)likeImageView.getTag();
if( id == R.drawable.download1){
likeImageView.setTag(R.drawable.downloa
d1);
likeImageView.setImageResource(R.drawa
ble.download2);
Toast.makeText(getActivity(),titleTextView.g
etText()+" added to
favourites",Toast.LENGTH_SHORT).show()
;
}else{
likeImageView.setTag(R.drawable.downloa
d1);
likeImageView.setImageResource(R.drawa
ble.download2);
Toast.makeText(getActivity(),titleTextView.g
etText()+" removed
from
favourites",Toast.LENGTH_SHORT).show()
;
});
shareImageView.setOnClickListener(new
View.OnClickListener() {
@Override
Uri imageUri =
Uri.parse(ContentResolver.SCHEME_AND
ROID_RESOURCE +
"://" +
getResources().getResourcePackageName
(coverImageView.getId())
getResources().getResourceEntryName((int
)coverImageView.getTag()
));
shareIntent.setAction(Intent.ACTION_SEN
D);
shareIntent.putExtra(Intent.EXTRA_STREA
M,imageUri);
shareIntent.setType("image/jpeg");
startActivity(Intent.createChooser(shareInt
ent,
getResources().getText(R.string.send_to)));
});
listitems.clear();
for(int i =0;i<7;i++){
item.setCardName(Wonders[i]);
item.setImageResourceId(Images[i]);
item.setIsfav(0);
item.setIsturned(0);
listitems.add(item);
/*WonderModel.java*\
package com.example.cardview;
String cardName;
int imageResourceId;
int isfav;
int isturned;
return isturned;
this.isturned = isturned;
return isfav;
this.isfav = isfav;
return cardName;
this.cardName = cardName;
return imageResourceId;
this.imageResourceId = imageResourceId;
Output :-