Header Ads

Header ADS

Android Simple ListView


Android ListView is a view. That is contain called ListItems. The ListView displays the ListItems in vertical. If ListView contains many items it will auto scrollable.  To insert the list items in ListView  have to use ArrayAdapter or ListAdapter . 



ListView Example  - simple_list_item_1



It's very easy to populate ListView in android. Let's see the steps.

Where will I find the ListView in android Studio ?
 In Design mood of android Studio you will see the ListView in the palette also in Containers. To clear See the below image.

ListView Position in Android Studio


Step 1:  
In mine just drag and drop the ListView from containers to your app. Will see a demo list item. It will only shows to developer  not in emulator as a user. Here after drop the ListView in  activity_main.xml  the code is.


 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.hp.symonapps.MainActivity">


    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="77dp" />




</RelativeLayout>


Now have to declare  a String Array in MainActivity.java 
Here area the code of String Array which I declare with the name of my some friends. That's why I named it to friends.

Declare  String Array in MainActivity.java 
    //declare a string array
    String[] friends ={
            "Melisa",
            "Sazzad",
            "Nasir",
            "Morshed",
            "Jewel",
            "Riyad",
            "Mohiuddin",
            "Ellysa",
            "Sharmin",
            "Stiv",
            "Idynha Paulo",
            "Mahmuda"
    };

To see where I declare the String array See the below image.

String Array in Android



Haha! We have reached at the final  task.  Just need to create a ListView object and  Then cast the listView.  After theat use the ArrayAdapter  to include the String in LIstView.  Can you remember what was the id of our ListView in activity_main.xml ?

ListView id

All right the id is  listView.


Create ListView object and cast then ArrayAdapter   in  MainActivity.java
        //Create ListView object and cast the listView
        ListView listView = (ListView) findViewById(R.id.listView);

        //create array adapter then set the adapter with listView
        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,friends);
        listView.setAdapter(adapter);



Here area the full  MainActivity.java


package com.example.hp.symonapps;

        import android.content.Intent;
        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.ImageView;
        import android.widget.ListView;
        import android.widget.Toast;


public class MainActivity extends AppCompatActivity {

    //declare a string array
    String[] friends ={
            "Melisa",
            "Sazzad",
            "Nasir",
            "Morshed",
            "Jewel",
            "Riyad",
            "Mohiuddin",
            "Ellysa",
            "Sharmin",
            "Stiv",
            "Idynha Paulo",
            "Mahmuda"
    };


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

        //Create ListView object and cast the listView
        ListView listView = (ListView) findViewById(R.id.listView);

        //create array adapter then set the adapter with listView
        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,friends);
        listView.setAdapter(adapter);



    } //onCreate end


}



Great!! Okay Now Run the App  The Strings are shown as LIstView will Scrollable.







On next I will write another Article how to take click on Item And show the Toast message. Till then Stay happy!


For that you have to create activities. Here I've created 2 activities names second and nasir.

To take click on item just need the following code in MainActivity.java

        //take click on listview item
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                String friend = (String) adapterView.getItemAtPosition(i);
                Toast.makeText(view.getContext(), "Hello " +friend+ " !", Toast.LENGTH_SHORT).show();

                if(i == 1){
                    Intent s = new Intent(view.getContext(),second.class);
                    startActivity(s);
                }

                     //another way to go to the activity
                if(friend == "Nasir"){
                    Intent n = new Intent(view.getContext(),nasir.class);
                    startActivity(n);
                }


            }
        });

No comments

Powered by Blogger.