A Simple Android ListView Example

Posted by

Hello everyone and welcome to easytechsolutions A place for all your queries , Today in our post we are going to implement A Simple ListView in Android !

So lets get started to implement this clean way of arranging things inside our Android App!

Below are the steps to do:-

Step 1:- Open up your Android Studio and make a new project and select Blank Activity and you are good to go

Step 2:- Open up activity_main.xml and copy and paste the below code in it:-

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="com.listview.MainActivity">

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ListView>
</LinearLayout>

Step 3:- Now Open up your MainActivity.java file and paste below code in it:-

MainActivity.java

package com.listview;

import android.app.ListActivity;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {


    static final String[] NAME = new String[]{"Bill Gates","Mark Zuckerberg","Larry Page","Elon Musk","Steve Jobs","Andy Rubin","Sundar Pichai","Satya Nadella","Sachin Bansal","Shaun Parker"
    ,"Jeff Bezos","Kunal Deshmukh","Jack Maa","Brian Acton","Steve Ballmer","Sachin Tendulkar","AB Devilliers","Sharukh Khan"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListAdapter adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,NAME);

        ListView listView = (ListView) findViewById(R.id.listview);
        listView.setAdapter(adapter);

    }

}

That’s it now just save the above code and run it and you will get to see a nice arrangement of all the text in ListView.

Hope this short post gives you a head start in understanding the ListView concept in Android

Keep Support | Share | Thank You!

Leave a Reply

Your email address will not be published. Required fields are marked *