ANDROID EXAMPLE
Android Example: Building a Simple Android Application
In this tutorial, we'll go through the steps of creating a simple Android application that displays a list of items in a RecyclerView and allows the user to interact with it. This will demonstrate some of the fundamental concepts of Android development, including creating activities, using UI elements, and handling basic events.
Prerequisites:
- Android Studio installed on your machine.
- Basic knowledge of Java or Kotlin (we'll be using Java in this example).
- Familiarity with Android's UI components (such as
RecyclerViewandButton).
Step 1: Create a New Android Project
- Open Android Studio.
- Select "Start a new Android Studio project".
- Choose a Basic Activity template and click Next.
- Name your project (e.g.,
RecyclerViewExample), choose a save location, and select Java as the language (you can also use Kotlin if preferred). - Make sure that the Minimum API level is set to at least API 21 (Lollipop).
- Click Finish to create the project.
Step 2: Add Dependencies to build.gradle
To use RecyclerView, you need to add the required dependency to your build.gradle file. If it’s not already added by default, follow these steps:
- Open the
build.gradle(Module: app) file in Android Studio. - Inside the
dependenciessection, add:
implementation 'androidx.recyclerview:recyclerview:1.2.1'
- Sync your project by clicking on Sync Now in the yellow bar at the top.
Step 3: Design the Layout
Next, you’ll create a simple layout that includes a RecyclerView to display a list of items.
- Open the
res/layout/activity_main.xmlfile. - Replace the existing content with the following XML code to define a
RecyclerViewinside aLinearLayout:
<?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">
<!-- RecyclerView to display list of items -->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="16dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
In this layout:
- We are using a
RecyclerViewthat will hold the items we want to display. - The
RecyclerViewwill be centered and have margins from the edges of the screen.
Step 4: Create a Data Model
Before we can populate the RecyclerView, we need to create a simple data model that will represent each item in the list.
- Create a new Java class called
Item.javain thejava/com/example/recyclerviewexample/directory.
public class Item {
private String name;
public Item(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
This class will represent the individual items in the list, containing just a String for the name.
Step 5: Create the Adapter
The RecyclerView requires an adapter to bind the data to the views. We will create a custom adapter class.
- Create a new Java class called
ItemAdapter.java.
package com.example.recyclerviewexample;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ItemViewHolder> {
private List<Item> itemList;
public ItemAdapter(List<Item> itemList) {
this.itemList = itemList;
}
@Override
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(android.R.layout.simple_list_item_1, parent, false);
return new ItemViewHolder(view);
}
@Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
Item currentItem = itemList.get(position);
holder.textView.setText(currentItem.getName());
}
@Override
public int getItemCount() {
return itemList.size();
}
public static class ItemViewHolder extends RecyclerView.ViewHolder {
public TextView textView;
public ItemViewHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(android.R.id.text1);
}
}
}
In this adapter:
- The
ItemAdapterclass extendsRecyclerView.Adapter. - The
onCreateViewHoldermethod inflates the list item layout (we are using a simple built-in layout:android.R.layout.simple_list_item_1). - The
onBindViewHoldermethod binds the data from theItemobject to theTextView. - The
getItemCountmethod returns the size of the data list.
Step 6: Set Up the RecyclerView in MainActivity.java
Now that we have the data model and adapter, let’s configure the RecyclerView in MainActivity.java to display the items.
- Open
MainActivity.javaand update the code as follows:
package com.example.recyclerviewexample;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private ItemAdapter adapter;
private List<Item> itemList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize RecyclerView
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
// Create sample data
itemList = new ArrayList<>();
itemList.add(new Item("Apple"));
itemList.add(new Item("Banana"));
itemList.add(new Item("Cherry"));
itemList.add(new Item("Date"));
itemList.add(new Item("Grapes"));
// Initialize the adapter and set it to the RecyclerView
adapter = new ItemAdapter(itemList);
recyclerView.setAdapter(adapter);
}
}
In this code:
- We initialize the
RecyclerViewand set aLinearLayoutManagerto display the list vertically. - We create a sample list of
Itemobjects (Apple,Banana, etc.) and add them to theitemList. - The
ItemAdapteris then created with theitemList, and we set it to theRecyclerViewusingsetAdapter.
Step 7: Run the Application
Now, you can run the application on an emulator or a physical device. When you launch the app, you should see a list of fruit names displayed in the RecyclerView. The RecyclerView should allow for smooth scrolling through the list.
Conclusion
In this example, we created a simple Android app with the following components:
- A RecyclerView for displaying a list of items.
- A data model (
Itemclass) to represent each item. - A custom adapter (
ItemAdapter) to bind the data to theRecyclerView. - A MainActivity to set up and manage the UI.
This example shows how to use basic Android components to build a simple and interactive list display. You can extend this example by adding more complex features such as item click handling, loading data from a network, or adding animations.

0 Comments