Listing Activity using Listview in Android Studio – Java

In this article, we will learn how to develop a Listing Activity using ListView in Android Studio in Java. Below we are going to create a simple Activity for Products Listing. It contains the ImageView, TextView, and ListView. At the end of this tutorial, the following screen will be designed.

Products Listing Activity - Handy Opinion

BaseAdapter

BaseAdapter is a base class of implementation that can be used in ListView. If you need a custom frame, make your own adapter and extend the base adapter on it. The base adapter can be expanded to create a custom adapter to display a custom list item. ArrayAdapter is also an implementation of BaseAdapter.

Activity Main XML File

Copy the following code in your activity_main.xml file. It only contains a ListView.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

<ListView
    android:id="@+id/simpleListView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:divider="@color/material_blue_grey_800"
    android:dividerHeight="1dp"
    android:footerDividersEnabled="false" />
</LinearLayout>

List View XML File

Create a new empty Activity with the name of ListView and copy the below code in activity_listview.xml. It contains only one ImageView and one TextView.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/ic_launcher_foreground" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:padding="18dp"
        android:textSize="18dp"
        android:textColor="@color/black" />
</LinearLayout>

MainActivity Code

Now we will use Custom Adapter to display the country names in User Interface.

Note: Flag images are store in a drawable folder inside the res folder.

package com.example.productlisting;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

public class MainActivity extends Activity {

    ListView simpleList;
    String countryList[] = {"Pakistan", "Turket","Saudi Arabia","Iraq","Qatar" };
    int flags[] = {R.drawable.pakistan, R.drawable.turkey,R.drawable.saudiarabia,R.drawable.iraq,R.drawable.qatar};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        simpleList = (ListView) findViewById(R.id.simpleListView);
        CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(), countryList, flags);
        simpleList.setAdapter(customAdapter);
    }
}

CustomAdapter Code

Now create a Custom Adapter java class that will extend BaseAdapter and paste the following code in your CustomAdapter.java file.

package com.example.productlisting;

import android.content.Context;
import android.media.Image;
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.zip.Inflater;

public class CustomAdapter extends BaseAdapter {
    Context context;
    String countryList[];
    int flags[];
    LayoutInflater inflter;

    public CustomAdapter(Context applicationContext, String[] countryList, int[] flags) {
        this.context = context;
        this.countryList = countryList;
        this.flags = flags;
        inflter = (LayoutInflater.from(applicationContext));
    }

    @Override
    public int getCount() {
        return countryList.length;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        view = inflter.inflate(R.layout.activity_listview, null);
        TextView country = (TextView) view.findViewById(R.id.textView);
        ImageView icon = (ImageView) view.findViewById(R.id.icon);
        country.setText(countryList[i]);
        icon.setImageResource(flags[i]);
        return view;
    }
}

That’s it. This is how to develop a Listing Activity using ListView in the Android Studio.

If you have any questions or suggestions, feel free to ask in the comments section below. Thanks

Next Articles

1. Products Listing Activity in Android Studio in Kotlin
2. How to Validate an Email in Android Using Kotlin
3. Reset Password Activity in Android Studio Kotlin and Java

Please share this post:
Posts created 41

Ask a Question

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

%d bloggers like this: