Android listview divider color

By default, Android provides simple divider between every row of a ListView. However, it is possible to customize the divider. You can apply your own custom shape, height, and color to the divider. In this Android tip, I am going to show how to achieve the goal.
Let’s add a ListView to the activity_main.xml file. There are two attributes of the ListView that make the divider work: android:divider and android: dividerHeight. The value of the divider attribute will be a custom drawable that defines the shape and color of the divider. You specify the height of the divider using android:dividerHeight attribute.





activity_main.xml file

<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="horizontal"
   android:padding="10dp"
   tools:context=".MainActivity"
   android:background="@drawable/shadow"
  >
   <ListView
     android:id="@+id/listv"      android:layout_width="match_parent"      android:layout_height="wrap_content"      android:divider="@drawable/customdivider"      android:dividerHeight="4dp"      /> </LinearLayout>


Now you create a custom drawable xml file. In the file, you specify the shape using <shape> tag and android:shape attribute. You use <solid> tag to define the color and <corners> tag to set color and corners radius of the shape.

customdivider.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android= "http://schemas.android.com/apk/res/android"
   android:shape="rectangle">
   <solid
android:color="#ff6143b5"

      />
   <corners      android:radius="2dp"      /> </shape>


In the res/layout directory, you create a layout file called list_layout that defines row template of the ListView.

List_layout.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:orientation="vertical"
   >

   <TextView
      android:id="@+id/txtitem"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
</LinearLayout>


To display the ListView, in the onCreate() method of the MainActivity class, you write code as shown below.

String[] items={"Item1","Item2","Item3","item4","item5","item6","item7"};      
ListView listv=(ListView)findViewById(R.id.listv);

ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,R.layout.list_layout,R.id.txtitem,items);      listv.setAdapter(adapter);


listview divider color

Posted by: Dara | post date: 05-05-2015 | Subject: Android Apps Development

write programming tip




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.