Android Spinner: Customize DropDown color

1

I need to customize the background color of a DropDown, until I get to do this:

if possible try to give rounded edges and have the same color as the background TextViews (As you can see the background is white), to get to do that, this is my code:

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#bdfbea"
android:fontFamily="@font/comfortaa"
android:padding="20dp"
android:textColor="#00887b"
android:textSize="24sp"
android:textStyle="bold"
android:maxLines="1"/>

//

    String[] idiomas = {"ESPAÑOL", "ENGLISH", "PORTUGUES"};
    Spinner spinner = (Spinner) v.findViewById(R.id.sp_idioma);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), R.layout.spinner_textview, idiomas);
    adapter.setDropDownViewResource(R.layout.spinner_dropview);
    spinner.setAdapter(adapter);

PS: already probe with android:popupBackground="COLOR" and not to function, also try several solutions on the web, and none with the answer

// --------------------------------------------- ---- AGGREGATED

I use a Contraint Layout to orient the components, my component spinner is configured as follows

        <Spinner
        android:id="@+id/sp_idioma"
        android:layout_width="344dp"
        android:layout_height="50dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/spinner_personalizado"
        android:spinnerMode="dialog"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_idioma" />

The Background is the following:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <layer-list>
        <item>
            <shape>
                <solid android:color="#bdfbea" />

                <stroke android:width="3dp" android:color="#00887b" />

                <padding android:bottom="1dp" android:left="1dp" android:right="15dp" android:top="1dp" />

                <corners android:radius="50dp" />
            </shape>
        </item>
        <item>
            <bitmap android:gravity="center|right"
                android:src="@drawable/img_flecha_spinner"
                />
        </item>
    </layer-list>
</item>

    
asked by LcsGrz 19.07.2018 в 02:53
source

1 answer

1

For this you must use your custom Adapter and within the getView( method) you can write about the colors of the text and the background:

  @Override
    public View getView(int pos, View view, ViewGroup parent) {


        LayoutInflater inflater= LayoutInflater.from(context);
        view = inflater.inflate(android.R.layout.simple_spinner_dropdown_item, null);
        TextView txv = (TextView)view.findViewById(android.R.id.text1);
        txv.setPadding(20,20,20,20);
        //Color Fondo.
        txv.setBackgroundColor(Color.parseColor("#BDFCEB"));
        //Color Texto.
        txv.setTextColor(Color.parseColor("#2D9989")); 
        txv.setText(values.get(pos));

        return view;
    }

This would be the code of the Adapter so that you have it as a reference:

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

/**
 * Created by jorgesys
 */
class SpinnerAdapter extends BaseAdapter
{
    List<String> values;
    Context context;

    public SpinnerAdapter(Context context, List<String> values )
    {
        this.context = context;
        this.values = values;
    }
    @Override
    public int getCount()
    {
        return values.size();
    }
    @Override
    public Object getItem(int arg0)
    {
        return values.get(arg0);
    }
    @Override
    public long getItemId(int arg0)
    {
        return arg0;
    }
    @Override
    public View getView(int pos, View view, ViewGroup parent) {


        LayoutInflater inflater= LayoutInflater.from(context);
        view = inflater.inflate(android.R.layout.simple_spinner_dropdown_item, null);
        TextView txv = (TextView)view.findViewById(android.R.id.text1);
        txv.setPadding(20,20,20,20);
        //Color Fondo.
        txv.setBackgroundColor(Color.parseColor("#BDFCEB"));
        //Color Texto.
        txv.setTextColor(Color.parseColor("#2D9989")); 
        txv.setText(values.get(pos));

        return view;
    }

}

To use this Adapter, simply configure it to your Spinner:

   spinner.setAdapter(new SpinnerAdapter(getApplicationContext(), listadeDatos));
    
answered by 19.07.2018 в 18:09