Download Code

Android Studio Listview Example Android

In Android Studio Listview example android tutorial you will learn how to use ListView in android application.Listview is mostly used to display list of avialable items within any ecommerce app.

ListView

List View is a view group that displays a list of scrollable items. The list items are automatically inserted to the list using an Adapter.

Array Adapter

A concrete BaseAdapter that is backed by an array of arbitrary objects. By default this class expects that the provided resource id references a single TextView. If you want to use a more complex layout, use the constructors that also takes a field id. That field id should reference a TextView in the larger layout resource.

Create new android studio listview example android application project.

Drag and Drop List View in activity_main.xml file.

activity_main.xml

Android Studio Listview,android studio listview example,android studio listview example android,listview example android,

Go to your MainActivity.java class.

  • Create variable of ListView.
  • Create String array.
  • Refer id of List View.
  • Store string array in ArrayAdapter.
  • Set ArrayAdapter to List View.
  • Apply click event on list view items.
  • Display Toast to show which item is clicked.

MainActivity.java

package com.example.anonymous.studiolistview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    ListView codecList;   // list view variable
    // string array for list menu
    String androidCodec[] = {"Java","C#","C++","Php","Javascript","Android","Codec"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        codecList = (ListView) findViewById(R.id.list);   // refering list view of xml
        //setting array adapter with item layout
        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, androidCodec);
        codecList.setAdapter(adapter);   // setting adapter to list view
        // click event on list view items
        codecList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getApplicationContext(),androidCodec[position].toString(),Toast.LENGTH_SHORT).show();
            }
        });

    }
}

Deploy your android application project.It will display a listview containing string array which you initialized at the start.When you click on any list item.It will display that item text in Toast.

Android Studio Listview,android studio listview example,android studio listview example android,listview example android,

That’s it for this tutorial.

Thanks.