Add/Set border around whole listview in android

How to create/show list view with border using XML file to separate from below layout.

In this tutorial we are setting up border on complete listview to make list view separate from below layout surface. This can be possible via external custom XML border layout file. So here is the complete step by step tutorial for Add/Set border around whole listview in android.

android-project-download-code-button

How to Add/Set border around whole listview in android.

Code for MainActivity.java file.

 package com.setborderaroundwholelistview_android_examples.com;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

 ListView listView;
 String[] listValue = new String[] {"Android Examples","Android Studio","SDK Manager","Eclipse"
 ,"Java","JDK"};
 
 @Override
 protected void onCreate(Bundle savedInstanceState) 
 {
 super.onCreate(savedInstanceState);
 
 setContentView(R.layout.activity_main);
 
 listView = (ListView)findViewById(R.id.listView1);
 
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_2, android.R.id.text1, listValue);
 
 listView.setAdapter(adapter); 
 
 
 }
}

Code for activity_main.xml layout file.

<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"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.setborderaroundwholelistview_android_examples.com.MainActivity" >

 <ListView
 android:id="@+id/listView1"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:background="@layout/listview_border" >
 </ListView>

</RelativeLayout>

Code for listview_border.xml file.

 <shape xmlns:android="http://schemas.android.com/apk/res/android">
 <stroke
 android:width="2dp"
 android:color="#fe0315" />
</shape>

Screenshot:

Add/Set border around whole listview in android

Click here to download Add/Set border around whole listview in android project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *