11

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 ?

sdabet
  • 18,360
  • 11
  • 89
  • 158

3 Answers3

21

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.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
saschoar
  • 8,150
  • 5
  • 43
  • 46
  • But I don't see how I can tell the editor to use a given `Adapter` for the preview ? – sdabet Mar 15 '13 at 14:53
  • You cannot assign the Adapter to the Spinner in XML, but only the layout that the adapter uses to return each item view. You have to use a custom layout for that if you'd like to customize the preview texts. – saschoar Mar 15 '13 at 15:27
  • This whole `tools:listitem` thing doesn't seem to work in any circumstance in my AS 2.2.3. Can anyone claim it still works? – androidguy Dec 17 '16 at 05:22
  • No, I never got it to work since I first opened AS in september – androidguy Mar 06 '17 at 03:06
11

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"

Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
  • But what if I want to use the `android.R.layout.simple_spinner_item` layout with my spinner (which I cannot modify) – sdabet Mar 15 '13 at 14:50
  • Btw modifying the `android:text` doesn't sound great since it actually modifies the content in my app (and I only want to set a sample preview text) – sdabet Mar 15 '13 at 14:54
  • This doesn't change the content of your application. The `tools:listitem` attribute sets the preview of the widget, not the layout of the actual widget. In general, the `tools` namespace indicates something used by the developer tools to generate your layout preview, and will not be used in the actual application. – Bryan Herbst Mar 15 '13 at 14:59
  • Ok I see. But it means that my `preview.xml` has to be a duplicate of the actual layout that I will use in my adapter, right ? (in my case I should create a copy of `android.R.layout.simple_spinner_item` with a set text ?) – sdabet Mar 15 '13 at 15:20
  • No, you could use an existing layout as the preview layout. – Bryan Herbst Mar 15 '13 at 15:26
  • But I cannot modify `android.R.layout.simple_spinner_item` (which is part of the SDK) to set a sample text inside – sdabet Mar 15 '13 at 15:35
  • Ah, I see. Yes, in this case you will have to "duplicate" the simple_spinner_item layout. – Bryan Herbst Mar 15 '13 at 15:37
  • 1
    Ok. I was hoping there would be a better solution, but it obviously doesn't exist yet in the editor. Thx anyway – sdabet Mar 15 '13 at 15:42
3

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

Community
  • 1
  • 1
frenziedherring
  • 2,225
  • 2
  • 15
  • 23
  • Does it mean I should create my own subclass of `Spinner` ? – sdabet Mar 15 '13 at 15:41
  • That might be the easiest way to achieve this. Have the subclass of spinner implement the isEditMode block of code to set the item text. Personally, I usually just accept that it looks a little different in the preview and I try it out on my device or emulator to verify that the text is correct at run-time. Is there a reason you _need_ to see the text in the preview window at design time? I can see it being desirable for non-text layouts of the spinner, but in this case, only the text content of the selected item will change at run-time. – frenziedherring Mar 15 '13 at 15:46
  • I'm using the editor to create a kinf of app prototype. Basically I need to build empty screens (with no or very little code behind) to demonstrate the final look of the app with some fake data. – sdabet Mar 15 '13 at 15:51
  • But I want to reuse those layouts for actual implementation later – sdabet Mar 15 '13 at 15:56
  • Perhaps then, in the subclass you could create a dummy adapter, a la http://stackoverflow.com/questions/8509488/set-value-of-spinner-from-array-in-strings-xml and then when it comes time to implement the prototype, you can dump the dummy subclass used for editMode and implement the Adapter to actually fetch real data. Then associate the newly filled in adapter to a basic spinner in the xml. – frenziedherring Mar 15 '13 at 16:02
  • What about just loading it to a device or emulator and taking screen shots? You can do this through DDMS in Eclipse rather easily with the screenshot button. – frenziedherring Mar 15 '13 at 16:03
  • Sure, but it would just feel more comfortable to have an instant preview of how your layout looks with real data (that's the whole point of the graphical editor, right ?). Anyway your subclass+dummy adapter idea sounds pretty good :) – sdabet Mar 15 '13 at 16:07
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/26256/discussion-between-frenziedherring-and-fiddler) – frenziedherring Mar 15 '13 at 16:12