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>
<?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>
<?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>
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);
|
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.