The first image shows the spinner when it isn't selected.
When the spinner is selected I want to change it's style so it looks like shown in the second image below.
The first image shows the spinner when it isn't selected.
When the spinner is selected I want to change it's style so it looks like shown in the second image below.
I guess there is no such direct way available to do above one. But you can take layout, set image to its background. Take imageview on that layout & on click listener of that layout change arrow image & same time show listview on click of that layout & on double click make listview invisible.
See below :
Layout(having background image) -> Imageview(arrow/s) -> Pop Up ListView -> Make it visible/invisible
A long time ago, you used to be able to specify the android:spinnerSelector
property for Spinners, but unfortunately that property has been removed and won't work any more.
Unfortunately the only other way to do this (AFAIK) is to extend the Spinner
class. This is because in order to change the background the way you want, you need to determine if the Spinner is opened or closed. There is no built in way to detect this, so you need to build it into a custom Spinner class.
In the custom spinner class, you will detect when the Spinner has been pressed and opened, and when the dialog has been closed either by cancelling the dialog, or by selecting an item.
I have referred to the following answer heavily in writing this answer (copy pasted a lot of code then modified it for your use). Please reference it as well. I changed that answer by eliminating the use of the listener interface since you don't care about knowing when the the click happens, you just want the background to change. Add the listener back in if you want that information in your activity.
First, create a custom Spinner in a new File CustomSpinner.java
public class CustomSpinner extends Spinner {
private boolean mOpenInitiated = false;
// the Spinner constructors
// add all the others too, I'm just putting one here for simplicity sake
public CustomSpinner(Context context){
super(context);
setBackgroundResource(R.drawable.CLOSE_SPINNER_DRAWABLE);
}
@Override
public boolean performClick() {
// register that the Spinner was opened so we have a status
// indicator for the activity(which may lose focus for some other
// reasons)
mOpenInitiated = true;
setBackgroundResource(R.drawable.OPENED_SPINNER_DRAWABLE);
return super.performClick();
}
/**
* Propagate the closed Spinner event to the listener from outside.
*/
public void performClosedEvent() {
mOpenInitiated = false;
setBackgroundResource(R.drawable.CLOSED_SPINNER_DRAWABLE);
}
/**
* A boolean flag indicating that the Spinner triggered an open event.
*
* @return true for opened Spinner
*/
public boolean hasBeenOpened() {
return mOpenInitiated;
}
}
In your Activity create the custom spinner (probably in onCreate), and override onWindowFocusChanged to tell the spinner to cancel notify you of a close event if it is opened.
CustomSpinner mSpin;
@Override
public void onCreate(Bundle savedInstanceState){
// Do all your initialization stuff
mSpin = (CustomSpinner) findViewById(R.id.myCustomSpinner);
// shouldn't need this if you are setting initial background in the constructors
mSpin.setBackgroundResource(R.drawable.CLOSED_SPINNER_DRAWABLE);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
// mSpin is our custom Spinner
if (mSpin.hasBeenOpened() && hasFocus) {
mSpin.performClosedEvent();
}
}
Your xml resource for this spinner would then look like the following (NOTE: I have only added a couple parameters to this XML file, it is definitely not complete as there are no width, height or alignment parameters).
<com.packagename.CustomSpinner
android:id="@+id/myCustomSpinner"
/>
Hope this helped and sorry it's so complicated. I may have looked over an easier solution, but I searched extensively and couldn't find anything that worked correctly.