0% found this document useful (0 votes)
6 views6 pages

Android RecyclerView Example Code

The document outlines the structure and code for an Android application featuring a RecyclerView to display a list of items. It includes XML layout files for the main activity and individual item views, as well as Java classes for the main activity, item model, and item adapter. The application initializes a list of items with titles and descriptions, which are displayed in a card format within the RecyclerView.
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)
6 views6 pages

Android RecyclerView Example Code

The document outlines the structure and code for an Android application featuring a RecyclerView to display a list of items. It includes XML layout files for the main activity and individual item views, as well as Java classes for the main activity, item model, and item adapter. The application initializes a list of items with titles and descriptions, which are displayed in a card format within the RecyclerView.
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

Practical-15

 Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<[Link]
android:id="@+id/recyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</[Link]>

 Item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:background="?android:attr/selectableItemBackground"
android:elevation="4dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">

<TextView
android:id="@+id/textViewTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Item Title"
android:textSize="18sp"
android:textColor="@android:color/black" />

<TextView
android:id="@+id/textViewDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Item Description"
android:textSize="14sp"
android:textColor="@android:color/darker_gray" />

</LinearLayout>
</[Link]>

 [Link]
package [Link].pr16;

import [Link];

import [Link];
import [Link];
import [Link];

import [Link].prac15.R;

import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {

private RecyclerView recyclerView;


private ItemAdapter itemAdapter;
private List<Item> itemList;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

// Initialize RecyclerView
recyclerView = findViewById([Link]);
[Link](true);
[Link](new LinearLayoutManager(this));

// Initialize itemList and populate with data


itemList = new ArrayList<>();
[Link](new Item("Title 1", "Description for item 1"));
[Link](new Item("Title 2", "Description for item 2"));
[Link](new Item("Title 3", "Description for item 3"));
[Link](new Item("Title 4", "Description for item 4"));
[Link](new Item("Title 5", "Description for item 5"));

// Set the adapter


itemAdapter = new ItemAdapter(itemList);
[Link](itemAdapter);
}
}

 [Link]
package [Link].pr16;

public class Item {


private String title;
private String description;

public Item(String title, String description) {


[Link] = title;
[Link] = description;
}

public String getTitle() {


return title;
}

public String getDescription() {


return description;
}
}

 [Link]
package [Link].pr16;

import [Link];
import [Link];
import [Link];
import [Link];

import [Link];
import [Link];

import [Link];

public class ItemAdapter extends [Link]<[Link]> {

private List<Item> itemList;

// Constructor
public ItemAdapter(List<Item> itemList) {
[Link] = itemList;
}

@NonNull
@Override
public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
// Inflate the item_layout for each item
View view = [Link]([Link]())
.inflate([Link].item_layout, parent, false);
return new ItemViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
// Bind the data to the views
Item currentItem = [Link](position);
[Link]([Link]());
[Link]([Link]());
}

@Override
public int getItemCount() {
return [Link]();
}

// ViewHolder class to hold the views for each item


public static class ItemViewHolder extends [Link] {
public TextView textViewTitle;
public TextView textViewDescription;

public ItemViewHolder(@NonNull View itemView) {


super(itemView);
textViewTitle = [Link]([Link]);
textViewDescription = [Link]([Link]);
}
}
}

 [Link]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="[Link]
xmlns:tools="[Link]

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Pr16"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="[Link]" />

<category android:name="[Link]" />


</intent-filter>
</activity>
</application>

</manifest>

You might also like