Custom Spinner Amongst Text Color

Posted on

Modify from onetime post “Custom ArrayAdapter for Spinner, alongside custom icons“, alter to brand the custom Spinner alongside selected text color.

Add a custom spinner inward layout, layout/activity_main.xml.

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:padding="16dp"     android:orientation="vertical"     tools:context="com.blogspot.android_er.androidcustomspinner.MainActivity">      <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center_horizontal"         android:autoLink="web"         android:text="https://rootandroid.org//"         android:textStyle="bold" />     <Spinner         android:id="@+id/spinner"         android:layout_width="match_parent"         android:layout_height="wrap_content" /> </LinearLayout>  

layout/row.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:orientation="horizontal">      <ImageView         android:id="@+id/icon"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:src="@mipmap/ic_launcher" />      <TextView         android:id="@+id/weekofday"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:textSize="20dp"         android:textStyle="bold"         android:textColor="#0000F0"/> </LinearLayout> 

MainActivity.java

package com.blogspot.android_er.androidcustomspinner;  import android.content.Context; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.TextView;  world shape MainActivity extends AppCompatActivity {      String[] DayOfWeek = {"Sunday", "Monday", "Tuesday",             "Wednesday", "Thursday", "Friday", "Saturday"};      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         Spinner mySpinner = (Spinner)findViewById(R.id.spinner);         mySpinner.setAdapter(new MyCustomAdapter(MainActivity.this, R.layout.row, DayOfWeek));     }      world shape MyCustomAdapter extends ArrayAdapter<String> {          world MyCustomAdapter(Context context, int textViewResourceId,                                String[] objects) {             super(context, textViewResourceId, objects);         }          @Override         world View getDropDownView(int position, View convertView,                                     ViewGroup parent) {             supply getCustomView(position, convertView, parent);         }          @Override         world View getView(int position, View convertView, ViewGroup parent) {             supply getCustomView(position, convertView, parent);         }          world View getCustomView(int position, View convertView, ViewGroup parent) {             LayoutInflater inflater=getLayoutInflater();             View row=inflater.inflate(R.layout.row, parent, false);             TextView label=(TextView)row.findViewById(R.id.weekofday);             label.setText(DayOfWeek[position]);              if(position == 0){                 label.setTextColor(0xFFF00000);             }              supply row;         }     } }