1

I know this question has been asked a lot (see: How to customize a Spinner in Android, How to change layout of spinner in Android, How to change spinner text size and text color?, How to change spinner text color)

All the answers suggest to create a custom_spinner.xml file to accomplish this task. This file must be something like this:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="top"
android:singleLine="true"
android:textColor="@color/#FFFFFF" />

However, no one says where to locate this file. In this answer https://stackoverflow.com/a/37859442/5616178 says this file must be in Drawable folder, but when I do that, an "Element TextView must be declared" error is raised by Android Studio. When I locate it in the layout folder, the R.layout won't resolve it either.

Thank you for your answers.

EDIT

I was able to solve the problem. As many of you said, the file must be located inside the res/layout since it's a layout resource. At first, my code looked like this:

citiesSpinner.setAdapter(
                    new ArrayAdapter<String>(SignUp.this,
                    android.R.layout.custom_spinner,
                    cityNames)
);

When I declared the adapter outside the constructor, i.e.:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                    SignUp.this,
                    R.layout.custom_spinner,
                    cityNames);
citiesSpinner.setAdapter(adapter);

The R class was able to resolve the layout. I don't know why that happens, it would be useful if someone can explain it. Thank you again for your answers!

Community
  • 1
  • 1

2 Answers2

2

However, no one says where to locate this file.

That is a layout resource. It would go in res/layout/ by default.

When I locate it in the layout folder, the R.layout won't resolve it either.

Then you have some other problem. For example, perhaps there is a bug in your layout resource that is preventing the R class from being regenerated.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • It'd be nice if you link to this: http://stackoverflow.com/questions/17054000/cannot-resolve-symbol-r-in-android-studio – Ali Bdeir Oct 05 '16 at 17:39
  • As I said, when I locate the file in `res/layout`, an "Element TextView must be declared" error is raised by Android Studio. – Génessis Sánchez Oct 05 '16 at 18:05
  • @GénessisSánchez: That is not what your question has. Your question indicates that you are getting that error when you put that XML in a drawable resource ("this file must be in Drawable folder, but when I do that, an "Element TextView must be declared" error is raised by Android Studio"). This makes sense, as that is not a valid drawable resource. It *is* a valid layout resource, at least insofar as `` is a valid element for a layout resource. – CommonsWare Oct 05 '16 at 18:43
2
  1. Your resource file must be in the layout folder
  2. Your android:id="@android:id/text1" need to be android:id="@+id/text1"
  3. Just declare your array like the links above recommended :

    ArrayAdapter<String> adapter = new ArrayAdapter<String>this,R.layout.custom_spinner,yourArray);