Spinner does not appear text inside it

Asked

Viewed 628 times

0

The spinner arrow appears but the text does not and even when I choose one of the spinner options nothing happens empty without the chosen option...the minimum version in my grandle is the maximum 11 version is 24.

My xml:

 <Spinner
                    android:id="@+id/spinnerlinha"
                    android:layout_width="100dp"
                    android:layout_height="30dp"
                    android:layout_below="@+id/tvlinhas"
                    android:background="#000"
                    android:textSize="20sp"
                    android:textColor="#ff0000"
                    android:layout_marginLeft="16dp"
                    android:layout_marginRight="16dp"
                    android:layout_marginTop="5dp"
                    android:layout_marginBottom="5dp"
                    android:spinnerMode="dropdown"
                   />

My code :

      linhaSP=(Spinner)findViewById(R.id.spinnerlinha);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_dropdown_item,
                linhaNome);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
linhaSP.setAdapter(adapter);

        linhaSP.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0,
                                       View arg1, int position, long arg3) {

            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
  • Where is the code to fill the array linhaNome?

  • See by json but you’re giving me the right options at the moment of the click nothing happens

1 answer

1

The problem is in android:background="#000" which gives the color black to the background.

Like android:textColor="#ff0000" has no effect (does not change the color of the text to red).

The result is black text on black background.

Remove these two attributes, or put another color to the background.

If you want to change the color of the text and the background do the following:

In the folder layout create the file spinner_item.xml with this code:

<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:textColor="#ff0000"
    android:background="#00FF00"
    android:ellipsize="marquee"/>

Change the colors to your liking.
Install the Adapter in this way:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            R.layout.spinner_item,
            linhaNome);

If you want the Dropdown view have different colors, create a new file called spinner_dropdown_item.xml with the same code as the previous one but with these other colors.

In the java, then the instantiation Adapter, place:

adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
  • The error continues to exist see the modification of my question , the background does not appear in green as it was supposed and does not appear text

  • I tested and everything works as expected.

  • Could be from my version of the device where I’m using apk ?

  • I don’t think so. You have some custom style? I should not have edited the question, my answer has now become meaningless. I should reverse the edition.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.