0% found this document useful (0 votes)
61 views18 pages

Value Added (Cardview) Aim:-To Show Images in Cardview Pre-Requisite: - Android Studio, SDK Code: / Activity - Main - XML

The document provides code for displaying images in card views in an Android application. It includes layout files for the activity, fragment, and individual cards. It also includes Java classes for the activity, fragment, card adapter, card view holder, and card model. The code displays a list of images for the 7 wonders of the modern world in card views within a recycler view fragment.

Uploaded by

K. J kartik Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views18 pages

Value Added (Cardview) Aim:-To Show Images in Cardview Pre-Requisite: - Android Studio, SDK Code: / Activity - Main - XML

The document provides code for displaying images in card views in an Android application. It includes layout files for the activity, fragment, and individual cards. It also includes Java classes for the activity, fragment, card adapter, card view holder, and card model. The code displays a list of images for the 7 wonders of the modern world in card views within a recycler view fragment.

Uploaded by

K. J kartik Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

VALUE ADDED

(Cardview)

Aim:- To show images in cardview

Pre-Requisite:- Android Studio,SDK

Code:-

/*activity_main.xml*\

<?xml version="1.0" encoding="utf-8"?>

<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;

public class MainActivity extends


AppCompatActivity {

@Override

protected void onCreate(Bundle


savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

FragmentManager fm =
getSupportFragmentManager();

Fragment fragment =

fm.findFragmentById(R.id.fragmentContain
er);

if (fragment == null) {

fragment = new CardFragment();

fm.beginTransaction()

.add(R.id.fragmentContainer, fragment)

.commit();

/*recycler_items.xml*\

<?xml version="1.0" encoding="utf-8"?>

<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*\

<?xml version="1.0" encoding="utf-8"?>

<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;

public class CardFragment extends


Fragment {

ArrayList<WonderModel> listitems = new


ArrayList<>();

RecyclerView MyRecyclerView;

String Wonders[] = {"Chichen Itza","Christ


the

Redeemer","Great Wall of China","Machu


Picchu","Petra","Taj

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};

// TODO: Rename parameter arguments,


choose names that match

// the fragment initialization parameters,


e.g.

ARG_ITEM_NUMBER

private static final String ARG_PARAM1 =


"param1";

private static final String ARG_PARAM2 =


"param2";

// TODO: Rename and change types of


parameters

private String mParam1;

private String mParam2;

public CardFragment() {

// Required empty public constructor

@Override

public void onCreate(@Nullable Bundle


savedInstanceState) {

super.onCreate(savedInstanceState);

initializeList();

getActivity().setTitle("7 Wonders of the


Modern World");

@Override

public View onCreateView(LayoutInflater


inflater, ViewGroup

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);

if (listitems.size() > 0 & MyRecyclerView !=


null) {

MyRecyclerView.setAdapter(new
MyAdapter(listitems));

MyRecyclerView.setLayoutManager(MyLay
outManager);

return view;

@Override

public void onActivityCreated(Bundle


savedInstanceState) {

super.onActivityCreated(savedInstanceStat
e);

public class MyAdapter extends

RecyclerView.Adapter<MyViewHolder> {

private ArrayList<WonderModel> list;

public
MyAdapter(ArrayList<WonderModel> Data)
{

list = Data;

@Override

public MyViewHolder
onCreateViewHolder(ViewGroup

parent,int viewType) {

// create a new view

View view =
LayoutInflater.from(parent.getContext())

.inflate(R.layout.recycle_items, parent,

false);

MyViewHolder holder = new


MyViewHolder(view);

return holder;

@Override

public void onBindViewHolder(final


MyViewHolder holder,

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

public int getItemCount() {

return list.size();

public class MyViewHolder extends


RecyclerView.ViewHolder {

public TextView titleTextView;

public ImageView coverImageView;

public ImageView likeImageView;

public ImageView shareImageView;

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

public void onClick(View v) {

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

public void onClick(View v) {

Uri imageUri =

Uri.parse(ContentResolver.SCHEME_AND
ROID_RESOURCE +

"://" +

getResources().getResourcePackageName
(coverImageView.getId())

+ '/' + "drawable" + '/' +

getResources().getResourceEntryName((int
)coverImageView.getTag()

));

Intent shareIntent = new Intent();

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)));

});

public void initializeList() {

listitems.clear();

for(int i =0;i<7;i++){

WonderModel item = new WonderModel();

item.setCardName(Wonders[i]);

item.setImageResourceId(Images[i]);

item.setIsfav(0);

item.setIsturned(0);

listitems.add(item);

/*WonderModel.java*\

package com.example.cardview;

public class WonderModel {

String cardName;

int imageResourceId;

int isfav;

int isturned;

public int getIsturned() {

return isturned;

public void setIsturned(int isturned) {

this.isturned = isturned;

public int getIsfav() {

return isfav;

public void setIsfav(int isfav) {

this.isfav = isfav;

public String getCardName() {

return cardName;

public void setCardName(String


cardName) {

this.cardName = cardName;

public int getImageResourceId() {

return imageResourceId;

public void setImageResourceId(int


imageResourceId) {

this.imageResourceId = imageResourceId;

Output :-

You might also like