0% found this document useful (0 votes)
86 views4 pages

Card View

This document contains XML layout code for an Android app that displays a card view with an image and text. It also includes Java code for a Card class to store card data, a CardListAdapter class to populate the card view from a list, and a MainActivity class that initializes the list and adapter.

Uploaded by

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

Card View

This document contains XML layout code for an Android app that displays a card view with an image and text. It also includes Java code for a Card class to store card data, a CardListAdapter class to populate the card view from a list, and a MainActivity class that initializes the list and adapter.

Uploaded by

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

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

>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">

<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="300dp"
app:layout_constraintTop_toTopOf="parent"
android:padding="10dp"
android:layout_margin="20dp"
app:cardCornerRadius="30dp"
app:cardElevation="10dp"

>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="@+id/image"
android:layout_width="150dp"
android:layout_height="match_parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/text"

/>

<TextView
android:id="@+id/text"
android:layout_width="150dp"
android:layout_height="match_parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/image"

/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>

=================================================

<?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">

<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
></ListView>

</androidx.constraintlayout.widget.ConstraintLayout>

=================================================

package com.example.myapplication;

public class Card


{
String Card_name ;
int Card_img;

public Card( String Card_name , int Card_img)

{
this.Card_name = Card_name;
this.Card_img = Card_img;
}

=================================================

package com.example.myapplication;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

public class Card_List_adapter extends BaseAdapter


{

Context context;
ArrayList<Card> Cards_Data;

public Card_List_adapter(Context Returncontext, ArrayList<Card>


ReturnCards_Data)//it will used in the main code latter when create new card
{
context = Returncontext;
Cards_Data = ReturnCards_Data;
}
@Override
public int getCount() {
return Cards_Data.size();// to know how much the Cards_Data arrayLIst size
}

@Override
public Object getItem(int position) {//position will Entered here from the
Cards_Data arrayLIst
return Cards_Data.get(position);// its mean the position entered will be
for the Cards_Data and it will be returned
}

@Override
public long getItemId(int position) {
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent)// witch
view from the Card_List arraylist are selected
{
convertView =
LayoutInflater.from(context).inflate(R.layout.constoum_layout,parent ,
false);//select the view that will be repeat
ImageView Img = convertView.findViewById(R.id.image); //connect this to xml
TextView Text = convertView.findViewById(R.id.text);
Card currentCard = Cards_Data.get(position);
Img.setImageResource(currentCard.Card_img);
Text.setText(currentCard.Card_name);

return convertView;
}
}

=================================================

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {


ListView listView;
ArrayList<Card> Cards_Datas;
Card_List_adapter add;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.list);
populate();
add = new Card_List_adapter(MainActivity.this,Cards_Datas);
listView.setAdapter(add);

public void populate()


{
Cards_Datas = new ArrayList<>();
Cards_Datas.add(new Card("azzz",R.drawable.f));
Cards_Datas.add(new Card("azzz",R.drawable.f));
Cards_Datas.add(new Card("azzz",R.drawable.f));
Cards_Datas.add(new Card("azzz",R.drawable.f));

}
}

You might also like