Wednesday, August 12, 2015

How to add border between items in a ListView in Android

MainActivity.java

package com.cfsuman.androidtutorials;

import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


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

        // Get the widgets reference from XML layout
        ListView listView = findViewById(R.id.listView);


        // Initializing a new String Array
        String[] fruits = new String[] {
                "Abiu",
                "Batuan",
                "Black Mulberry",
                "Cape Gooseberry",
                "Desert banana",
                "Eastern May Hawthorn",
                "Fibrous Satinash",
                "Gooseberry",
                "Hairless rambutan",
                "Illawarra Plum",
                "Jelly Palm"
        };

        // Create a List from String Array elements
        List<String> fruits_list = new ArrayList<String>
                (Arrays.asList(fruits));


        // Generate a border for listview items
        Drawable borderedDrawable= getBorderedDrawable();


        // Create an ArrayAdapter from List
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>
                (
                        this,
                        android.R.layout.simple_list_item_1,
                        fruits_list
                ){
            @Override
            public View getView(
                    int position, View convertView, ViewGroup parent){
                /// Get the Item from ListView
                View view = super.getView(position, convertView, parent);

                // Set the border of View (ListView Item)
                view.setBackground(borderedDrawable);

                // Return the view
                return view;
            }
        };


        // DataBind ListView with items from ArrayAdapter
        listView.setAdapter(arrayAdapter);
    }


    // Method to get a bordered drawable
    private Drawable getBorderedDrawable(){
        ShapeDrawable drawable = new ShapeDrawable();
        drawable.setShape(new RectShape());
        Paint paint = drawable.getPaint();
        paint.setColor(Color.LTGRAY);
        paint.setStrokeWidth(3);
        paint.setStyle(Paint.Style.STROKE);
        return drawable;
    }
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#DCDCDC"
    android:padding="24dp">

    <ListView
        android:id="@+id/listView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>