Thursday, September 22, 2016

Android: Preview Simple Data of Spinner (DropDown List) in Android Studio

Let's make spinner preview to make your design more productive!
By default, Android Studio will preview the Spinner with their sample title and label data. To make designing more productive, it is better if I can change Spinner to preview with my own data.

Luckily, It is possible to do this in Android Studio. Here is how

1 Create layout xml file contain preview data.
<?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: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.example.piyapan039285.helloworld.AboutActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="8:00"
        android:id="@+id/tvAbout"/>
</RelativeLayout>

2 In Spinner, add attribute tools:listItem like this
<?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: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.example.piyapan039285.helloworld.TableLayoutMainActivity">

    <Spinner
        android:id="@+id/spTimes"
        android:layout_width="96dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        tools:listitem="@layout/your_preview_xml_file_name"/>
        
</RelativeLayout>

3 If everything is OK, Android Studio will preview spinner to you like this


Happy Coding!

No comments:

Post a Comment