Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom text for spinner preview in Android graphical layout editor

By default if I create a Spinner in the graphical layout editor (using the Spinner Item preview layout, i.e android.R.layout.simple_spinner_item) the displayed text is

Item 1

Is there any way to change this preview text ?

like image 830
sdabet Avatar asked Mar 15 '13 13:03

sdabet


3 Answers

For a spinner preview text in particular, use the tools:listitem attribute together with a layout:

<Spinner
 android:id="@+id/spinner1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 tools:listitem="@android:layout/simple_list_item_1" />

You can also set that preview in the visual editor by right-clicking on the Spinner and then selecting "Preview spinner layout". Anyway, it has to be a concrete layout, no simple text string.

So the best practice would be to set your dummy texts in the particular list item layout that you are going to use anyway (e.g. in your Adapter in Java code), and then directly preview that layout as described above.

like image 165
saschoar Avatar answered Nov 08 '22 06:11

saschoar


Views have a function called "isEditMode()" that can be used to change the way items look in the Graphical Editor. This SO might help you out:

Custom Android Views in Eclipse Visual Editor

like image 33
frenziedherring Avatar answered Nov 08 '22 07:11

frenziedherring


First you need to create an appropriate preview layout. For example, you could put this in layout/preview.xml:

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1"
    style="?android:attr/spinnerDropDownItemStyle"
    android:singleLine="true"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:text="NEW PREVIEW TEXT"
    android:ellipsize="marquee" />

Then you can right-click the Spinner in your actual layout and select Preview Spinner Layout > Choose Layout... Choose your layout from your project resources, and you should see your new preview.

You can also set the preview layout in XML with tools:listitem="@layout/preview"

like image 11
Bryan Herbst Avatar answered Nov 08 '22 08:11

Bryan Herbst