ListView in Android

In this tutorial we will learn about ListView in Android. ListView in one of the most commonly used widget. We normally see ListView in different Android Apps for example contact list. In this tutorial we will learn how we can create simple list in Android and how we can create a custom ListView in Android.

ListView in Android, Custom ListView, Custom Adapter


First of all we will add ListView Widget in XML layout file. Check this code.

XML Example: -
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" tools:context=".MainActivity"> <ListView android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/listView" /> </LinearLayout>


To add listView in Activity we need an adapter which will help us to merge data and view together in ListView. We can provide data to list in format of array or list. Here we will add simple ArrayAdapter. Check this Java code.

Example

public class MainActivity extends AppCompatActivity {

    String colors[]={"Red","Green","Blue"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ListView listView=findViewById(R.id.listView);

        ArrayAdapter<String> arrayAdapter=new ArrayAdapter<>(getApplicationContext(),android.R.layout.simple_list_item_1,colors);

        listView.setAdapter(arrayAdapter);
    }
}



Here is output after writing all this code.

ListView in Android


Custom ListView in Android:

To make a custom ListView in android first of all we have to create a layout for single item of list and then we have to create a custom adapter to create listview. Here you have to crate new layout file. Here I just create new layout file and named it as list_item_layout Check this code to create a custom list item.

XML Example: -
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18dp" android:textColor="@color/colorPrimaryDark" android:id="@+id/text1"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="14dp" android:id="@+id/text2"/> </LinearLayout>


After creating this layout file we will create custom adpater to add data in this custom list view. To create a custom adapter just add a new java class in your android project and inherit BaseAdapter class in this class. BaseAdapter is an abstract class it mean you have to override all the abstract methods of this class into user class. Here i will create new class called MyListAdapter to create a custom adapter check this code.

Example

public class MyListAdpater extends BaseAdapter {

    String array1[]={"Red","Blue","Green"};
    String array2[]={"Rose is Red","Sky is Blue","Grass is Green"};
    LayoutInflater layoutInflater;
    public MyListAdpater(Context context){
        layoutInflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

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

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View v=layoutInflater.inflate(R.layout.list_item_layout,null,false);
        TextView text1=v.findViewById(R.id.text1);
        TextView text2=v.findViewById(R.id.text2);
        text1.setText(array1[i]);
        text2.setText(array2[i]);
        return v;
    }
}



Here our custom list item design is ready and our adapter is also ready. Now we will change the adapter of our listview from ArrayAdapter to MyListAdapter in MainActivity.

Example

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ListView listView=findViewById(R.id.listView);

        MyListAdpater myListAdpater=new MyListAdpater(getApplicationContext());

        listView.setAdapter(myListAdpater);
    }




Check this Output after adding CustomAdapter to our ListView.

ListView in Android


Share this tutorial with your friends on your social Media..

ListView in Android ListView in Android ListView in Android ListView in Android



Now You Can Follow All These Tutorials on Your Android Mobile Just Download This App.


Owlbuddy.com