6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Android]Spinnerの文字色をJavaで指定する

Last updated at Posted at 2015-11-02

なにこれ

「Spinner」に表示される文字の文字色をJava側で変更できるようにする。
同じSpinnerに渡すObjectsが変化しても、内容に応じて文字色を変えることができるようになる。

環境

Android Studio 1.4.1
Min SDK Version : API 16 (Android 4.1 (Jelly Bean))
Compile/Target SDK Version : API 23 : (Android 6.0 (Marshmallow))
テストした実機 : Xperia Z3 Compact (Android 5.1.1 (Lollipop))

ソース

Spinnerに自作Adapterを渡す

Java
//** Spinnerで表示するデータ例 **//
String[] strings = { "aaa", "bbb", "ccc" };
int[] colors = { Color.RED, Color.BLUE, Color.YELLOW };

CustomAdapter customAdapter = new CustomAdapter( getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, strings, colors );

Spinner spinner = (Spinner)findViewById(R.id.spinner);
spinner.setAdapter( customAdapter );

CustomAdapterを作る

Java
public class CustomAdapter extends ArrayAdapter<String> {

    int[] colors;

    public CustomAdapter( Context context, int resource, String[] strings, int[] colors ) {

        super( context, resource, strings );
        this.colors = colors;
    }


    @Override
    public View getDropDownView( int position, View convertView, ViewGroup parent ) {

        if( convertView == null ) {
            LayoutInflater inflater = LayoutInflater.from( getContext() );
            convertView = inflater.inflate( android.R.layout.simple_spinner_dropdown_item, parent, false );
            //** android.R.layout.simple_spinner_dropdown_item で inflate **//
        }

        this.setCustomTextView( (TextView) convertView, position );

        return convertView;
    }


    @Override
    public View getView( int position, View convertView, ViewGroup parent ) {

        if( convertView == null ) {
            LayoutInflater inflater = LayoutInflater.from( getContext() );
            convertView = inflater.inflate( android.R.layout.simple_spinner_item, parent, false );
            //** android.R.layout.sinple_spinner_item で inflate **//
        }

        this.setCustomTextView( (TextView) convertView, position );

        return convertView;
    }


    //** Spinnerの中身のTextViewを作る **//
    private void setCustomTextView( TextView textView, int position ) {

        textView.setText( super.getItem( position ) );
        textView.setTextColor( colors[position] );
    }
}

あとがき

参考にさせて頂きました : androidのSpinnerの文字色を変えるのが難しい件

このサイト様のままだとドロップダウンのレイアウトが崩れてしまったので、ArrayAdapterのソースを読んで改造しました。
LayoutInflater.from()とかinflater.inflate()parentとか良く分かってないけど上手くいったからいいよね。

これをさらに改造していろいろなことができそう。

6
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?