You are on page 1of 12

Lecture 5

Interacting with UI
components
programmatically
Interacting with UI Components Programmatically
Context in Android
RecyclerView
Outline
Interacting with UI Components Programmatically

1. Identify the UI element.


2. Retrieve a specific view element from your application's layout by
( findViewById(R.id)) method.
3. Implement an event handling method.
Handling UI Events in Android Apps
In Android apps, a listener is an interface or callback mechanism that allows
one component to receive notifications or events from another component.

OnClickListeners: To handle button clicks.


OnTouchListener: To handle touch events like taps, swipes, or drags on
various UI elements.
OnFocusChangeListener: For text fields.
OnTextChangedListener: For text input changes.
Context
Context: refers to an object that provides access to various resources and services within an
application environment.
It represents the current state of an application and allows you to obtain information about its
environment.
It allows us to interact with other Android components by sending messages.

Types of Context in Android


1. Application Context and
2.Activity Context
Application Context
getApplicationContext():
It is used to return the Context which is linked to the Application which holds all activities
running inside it.
List of functionalities of Application Context:
•Load Resource Values
•Start a Service
•Bind to a Service
•Send a Broadcast
•Register BroadcastReceiver
Activity Context
getContext(), getBaseContext() or this:
It returns the Context which is linked to the Activity from which it is called. This is useful when
we want to call the Context from only the current running activity.

List of Functionalities of Activity Context:


•Load Resource Values
•Layout Inflation
•Start an Activity
•Show a Dialog
•Start a Service
•Bind to a Service
•Send a Broadcast
•Register BroadcastReceiver
RecyclerView
RecyclerView is used for providing a limited window
to a large data set, which means it is used to display a
large amount of data that can be scrolled very
efficiently by maintaining a limited number of Views.
Step-by-step using RecyclerView in Android
1. Add the RecyclerView dependency to your project. build.gradle file.
dependencies {
implementation “androidx.recyclerview:recyclerview:1.1.0”
}

2. Create a RecyclerView layout in your activity's XML layout file


<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
3. Create a new layout file list_item.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<!-- Add your UI components here -->


</LinearLayout>

4. Create a custom ViewHolder class that extends RecyclerView.ViewHolder.


public class MyViewHolder extends RecyclerView.ViewHolder {
// Define your views here

public MyViewHolder(View itemView) {


super(itemView);
// Initialize your views here
}
}
5. Create a custom RecyclerView.Adapter class that extends RecyclerView.Adapter.

public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {


private List<MyData> dataList;

public MyAdapter(List<MyData> dataList) {


this.dataList = dataList;
}

6. In your activity or fragment, initialize the RecyclerView and set the adapter:
RecyclerView recyclerView = findViewById(R.id.recyclerView);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);

MyAdapter adapter = new MyAdapter(dataList);


recyclerView.setAdapter(adapter);
Thank
you

You might also like