Wednesday, March 16, 2016

android - Manage space between ListView items

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:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity"
    android:background="#cbd1d1"
    >
    <ListView
        android:id="@+id/lv"
        android:layout_width="175dp"
        android:layout_height="wrap_content"
        android:background="#ff0d0d"
        />
    <ListView
        android:id="@+id/lv_second"
        android:layout_width="175dp"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/lv"
        android:background="#08be63"
        />
    <ListView
        android:id="@+id/lv_third"
        android:layout_width="175dp"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/lv_second"
        android:background="#0a71d7"
        />
</RelativeLayout>
res/layout/listview_custom_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="20dp"
    android:paddingStart="20dp"
    android:paddingBottom="2dp"
    android:paddingTop="2dp"
    android:paddingRight="20dp"
    android:paddingEnd="20dp"
    />
res/layout/listview_custom_layout2.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="20dp"
    android:paddingStart="20dp"
    android:paddingBottom="25dp"
    android:paddingTop="25dp"
    android:paddingRight="20dp"
    android:paddingEnd="20dp"
    />
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class MainActivity extends AppCompatActivity {

    private Context mContext;
    private Activity mActivity;

    private RelativeLayout mRelativeLayout;
    private ListView mListView;
    private ListView mListViewSecond;
    private ListView mListViewThird;


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

        // Get the application context
        mContext = getApplicationContext();

        // Get the activity
        mActivity = MainActivity.this;

        // Get the widgets reference from XML layout
        mRelativeLayout = (RelativeLayout) findViewById(R.id.rl);
        mListView = (ListView) findViewById(R.id.lv);
        mListViewSecond = (ListView) findViewById(R.id.lv_second);
        mListViewThird = (ListView) findViewById(R.id.lv_third);

        // Initialize an array of animals
        String[] animals = new String[]{
                "Quelea",
                "Red deer",
                "Salamander",
                "Sea urchin",
                "Sea lion",
                "Squirrel",
                "Turkey"
        };

        // Create a list from string array elements
        List<String> animals_list = new ArrayList<String>(Arrays.asList(animals));

        // Initialize an ArrayAdapter object from the list
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                mContext,android.R.layout.simple_list_item_1,
                animals_list
        );

        // Populate the ListView widget with ArrayAdapter
        mListView.setAdapter(adapter);

        // Initialize a new ArrayAdapter object from list
        ArrayAdapter<String> adapterSecond = new ArrayAdapter<String>(
                mContext,
                R.layout.listview_custom_layout,
                animals_list
        );

        // Populate the second ListView with second ArrayAdapter
        mListViewSecond.setAdapter(adapterSecond);

        // Initialize a new ArrayAdapter object from list
        ArrayAdapter<String> adapterThird= new ArrayAdapter<String>(
                mContext,
                R.layout.listview_custom_layout2,
                animals_list
        );

        // Populate the third ListView with third ArrayAdapter
        mListViewThird.setAdapter(adapterThird);
    }
}