Spinner does not show text inside it

0

The spinner arrow appears but the text is not even when I choose one of the spinner options nothing happens appears empty without the option chosen ... 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) {

            }
        });
    
asked by anonymous 05.07.2016 / 22:22

1 answer

1

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

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

The result is black text on a black background.

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

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

In the layout folder, create the spinner_item.xml file 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. Instantiate the adapter like this:

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

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

In Java, after instantiating the adapter , place:

adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
    
05.07.2016 / 23:48