0

I have a nice green background that I'm displaying behind the spinner - the spinner sits in front of the background.

You can see that it looks really bad as the spinner can be clearly distinguished from it's background.

I would like to make the spinner background transparent when the dialog for the spinner is CLOSED. When the dialog is OPENED, the spinner can then take on the darker green. Currently, when the dialog is OPENED or CLOSED, the same darker green is used in the layout:

CURRENT LOOK OF SPINNER WHEN CLOSED:

enter image description here

WHAT I WANT THE SPINNER TO LOOK LIKE WHEN CLOSED

enter image description here

WHAT THE SPINNER LOOKS LIKE WHEN OPENED:

enter image description here

I load the spinner using an adapter by doing this:

    ArrayAdapter<String> ageAdapter = new ArrayAdapter<String>(this,
            R.layout.spinner_age, R.id.spinner_item, generateAgeRange());
    ageSpinner.setAdapter(ageAdapter);

I know that the R.layout.spinner_age xml determines how the spinner layout would look:

<?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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="12sp"
        android:background="@color/primary_darker"
        android:textColor="@color/white"
        tools:text="Hello"
        android:gravity="left"
        android:padding="12dp"
        android:id="@+id/spinner_item"/>

</LinearLayout>

Is there a way to load a layout xml for when the dialog is CLOSED and when the dialog is OPENED so that I can achieve the effect I want?

Simon
  • 19,658
  • 27
  • 149
  • 217
  • can you post the layout you want for closed and open. Also it might help if the screenshots weren't clipped. – Helix Feb 26 '16 at 18:41
  • I have posted an updated screenshot for what I want the spinner to look like when it is opened - can you see what I want to do? I just want a transparent background for the spinner when it is closed and a dark green background when it is opened. – Simon Feb 26 '16 at 18:49
  • if that's all you want to change it would be more efficient to call `spinner.setBackgroundResource(R.drawable.your_background);` – Helix Feb 26 '16 at 18:52
  • I can use the setBackgroundResource code provided that there is a listener that is called when the spinner dialog is opened and when it is closed. Then I can set a specific background for when it is opened and closed using the setBackgroundResource code. Is there a listener like that? – Simon Feb 26 '16 at 18:57
  • Requires a subclass of spinner.... http://stackoverflow.com/questions/18447063/spinner-get-state-or-get-notified-when-opens – Helix Feb 26 '16 at 19:09
  • Using a drop down spinner mode might also give you a similar desired effect with less effort. – Helix Feb 26 '16 at 19:13
  • Solved using a custom adapter. See my answer. – Simon Feb 26 '16 at 19:48

1 Answers1

0

SOLVED - you can use the following adapter to achieve the desired effect:

package com.example.simon.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.example.simon.activities.R;

import java.util.List;


public class GenderSpinAdapter extends ArrayAdapter<String> {

    private Context context1;
    private String[] data;
    LayoutInflater inflater;


    public GenderSpinAdapter(Context context, int resource) {
        super(context, resource);
    }

    public GenderSpinAdapter(Context context, int resource, int textViewResourceId) {
        super(context, resource, textViewResourceId);
    }

    public GenderSpinAdapter(Context context, int resource, String[] objects) {
        super(context, resource, objects);
    }

    public GenderSpinAdapter(Context context, int resource, int textViewResourceId, String[] objects) {
        super(context, resource, textViewResourceId, objects);
        context1 = context;
        data = objects;

        inflater = (LayoutInflater) context1
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public GenderSpinAdapter(Context context, int resource, List<String> objects) {
        super(context, resource, objects);
    }

    public GenderSpinAdapter(Context context, int resource, int textViewResourceId, List<String> objects) {
        super(context, resource, textViewResourceId, objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return getNormalView(position, convertView, parent);
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        return getCustomView(position, convertView, parent);
    }

    // This funtion called for each row ( Called data.size() times )
    public View getCustomView(int position, View convertView, ViewGroup parent) {

        View row = inflater.inflate(R.layout.spinner_gender, parent, false);

        TextView tvCategory = (TextView) row.findViewById(R.id.spinner_item);

        tvCategory.setText(data[position].toString());

        return row;
    }

    // This funtion called for each row ( Called data.size() times )
    public View getNormalView(int position, View convertView, ViewGroup parent) {

        View row = inflater.inflate(R.layout.spinner_transparent, parent, false);

        TextView tvCategory = (TextView) row.findViewById(R.id.spinner_item);

        tvCategory.setText(data[position].toString());

        return row;
    }
}
Simon
  • 19,658
  • 27
  • 149
  • 217