0% found this document useful (0 votes)
38 views7 pages

Mad Pract6

The document describes creating a recycler view Android application to display a list of student details including name, address, photo. It includes code for the main activity class, example item class to hold student data, adapter class to populate recycler view from the example item list, and layout files for the main activity and example item. The main activity code handles adding/removing items from the example list and recycler view on button clicks.

Uploaded by

Darshan
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)
38 views7 pages

Mad Pract6

The document describes creating a recycler view Android application to display a list of student details including name, address, photo. It includes code for the main activity class, example item class to hold student data, adapter class to populate recycler view from the example item list, and layout files for the main activity and example item. The main activity code handles adding/removing items from the example list and recycler view on button clicks.

Uploaded by

Darshan
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/ 7

180220131032 | 2021

PRACTICAL - 6
Aim - Create a Recycle View and list the details of student using.
1. Name
2. Address
3. Photo(Image)
4. Delete (Button Operation)

Main_activity.java-------------
package com.example.pr6;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {


private ArrayList<ExampleItem> mExampleList;
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private Button buttonInsert;
private Button buttonRemove;
private EditText editTextName;
private EditText editTextAddress;
private EditText editTextRemove;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createExampleList();
buildRecyclerView();
buttonInsert = findViewById(R.id.button_insert);
buttonRemove = findViewById(R.id.button_remove);
editTextName = findViewById(R.id.edittext_name);
editTextAddress = findViewById(R.id.edittext_address);
editTextRemove = findViewById(R.id.edittext_remove);
buttonInsert.setOnClickListener(v -> {
String name = editTextName.getText().toString();
String address = editTextAddress.getText().toString();
insertItem(name, address);
});
buttonRemove.setOnClickListener(v -> {
int position =
Integer.parseInt(editTextRemove.getText().toString());
removeItem(position);
});
}
public void insertItem(String name, String address) {
180220131032 | 2021

mExampleList.add(0, new ExampleItem(R.drawable.ic_android, name,


address));
mAdapter.notifyItemInserted(0);
}
public void removeItem(int position) {
mExampleList.remove(position);
mAdapter.notifyItemRemoved(position);
}
public void createExampleList() {
mExampleList = new ArrayList<>();
mExampleList.add(new ExampleItem(R.drawable.ic_android, "Darshan",
"Sidhpur"));
}
public void buildRecyclerView() {
mRecyclerView = findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mAdapter = new ExampleAdapter(mExampleList);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
}
}

ExampleItem.java----------
package com.example.pr6;

public class ExampleItem {


private int mImageResource;
private String mText1;
private String mText2;
public ExampleItem(int imageResource, String text1, String text2) {
mImageResource = imageResource;
mText1 = text1;
mText2 = text2;
}
public int getImageResource() {
return mImageResource;
}
public String getText1() {
return mText1;
}
public String getText2() {
return mText2;
}
}

ExampleAdapter.java---------------------
package com.example.pr6;

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 java.util.ArrayList;
public class ExampleAdapter extends
RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder> {
180220131032 | 2021

private ArrayList<ExampleItem> mExampleList;


public static class ExampleViewHolder extends RecyclerView.ViewHolder {
public ImageView mImageView;
public TextView mTextView1;
public TextView mTextView2;
public ExampleViewHolder(View itemView) {
super(itemView);
mImageView = itemView.findViewById(R.id.imageView);
mTextView1 = itemView.findViewById(R.id.textView);
mTextView2 = itemView.findViewById(R.id.textView2);
}
}
public ExampleAdapter(ArrayList<ExampleItem> exampleList) {
mExampleList = exampleList;
}
@Override
public ExampleViewHolder onCreateViewHolder(ViewGroup parent, int
viewType) {
View v =
LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item,
parent, false);
ExampleViewHolder evh = new ExampleViewHolder(v);
return evh;
}
@Override
public void onBindViewHolder(ExampleViewHolder holder, int position) {
ExampleItem currentItem = mExampleList.get(position);
holder.mImageView.setImageResource(currentItem.getImageResource());
holder.mTextView1.setText(currentItem.getText1());
holder.mTextView2.setText(currentItem.getText2());
}
@Override
public int getItemCount() {
return mExampleList.size();
}
}

Activity_main.xml-----------------------
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:fitsSystemWindows="true"
tools:context=".MainActivity">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/darker_gray"
android:padding="4dp"
android:scrollbars="vertical" />

<EditText
android:id="@+id/edittext_name"
android:layout_width="177dp"
180220131032 | 2021

android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="15dp"
android:layout_marginBottom="100dp"
android:hint="@string/name"
android:layout_alignParentLeft="true"
android:layout_marginLeft="15dp" />

<EditText
android:id="@+id/edittext_address"
android:layout_width="177dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="15dp"
android:layout_marginBottom="60dp"
android:hint="@string/address"
android:layout_alignParentLeft="true"
android:layout_marginLeft="15dp" />

<Button
android:id="@+id/button_insert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginTop="670dp"
android:text="@string/insert"
android:layout_marginLeft="15dp" />

<EditText
android:id="@+id/edittext_remove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="300dp"
android:layout_marginBottom="60dp"
android:inputType="number"
android:hint="@string/index"
android:layout_alignParentLeft="true"
android:layout_marginLeft="300dp" />

<Button
android:id="@+id/button_remove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="300dp"
android:layout_marginTop="670dp"
android:text="@string/remove"
android:layout_marginLeft="300dp" />

</RelativeLayout>

Example_item.xml---------------
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
180220131032 | 2021

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
app:cardCornerRadius="4dp" >

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="4dp">

<ImageView
android:id="@+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="2dp"
tools:srcCompat="@tools:sample/avatars" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/imageView"
android:text="@string/line_1"
android:textColor="@android:color/black"
android:textSize="20sp"
android:textStyle="bold"
android:layout_toRightOf="@+id/imageView" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_marginStart="8dp"
android:layout_toEndOf="@+id/imageView"
android:text="@string/line_2"
android:textSize="15sp"
android:layout_marginLeft="8dp"
android:layout_toRightOf="@+id/imageView" />

</RelativeLayout>

</androidx.cardview.widget.CardView>

Output:
180220131032 | 2021
180220131032 | 2021

You might also like