Thursday, February 21, 2019

Custom spinner Android

Normal android spinner look like this.

We want to change text color and make text center, let create a xml file name spin.xml in folder layout.

We use a textview to set text color, make it center, expand lines height.
<?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="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:lineSpacingExtra="6dp"
        android:paddingTop="8dp"
        android:singleLine="true"
        android:textColor="#00F"
        android:textSize="17sp" />

</LinearLayout>
Im
In code, set adapter for spinner like this.
adapter = new ArrayAdapter<String>(this, R.layout.spin, R.id.text, data);
Spinner now look like this.

To make a round corner spinner, in drawable, create a file name bu.xml.
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#a1caf1"/>
<corners android:radius="5dp"/>
</shape>

And set background for spinner.

android:background="@drawable/bu"

No comments:

Post a Comment