I have a Spinner in my action bar which gets initialized only once in onCreateActionMode();
Issue: When the spinner drop down is visible and I try to rotate the screen, the drop down state is not maintained after orientation change.
How could I maintain the state so that if in Portrait mode the Spinner dropdown is visible then after orientation change in Landscape mode too the dropdown should be visible.
Also let me know the API's by which we check whether the drop down is visible or not at a given point. Also an API to show the drop down forcefully.
I have also specified configChanges="orientation" in my Manifest file.
I found a very ugly solution to this (the 'action' takes place in the onConfigurationChanged method):
Before calling setContentView, check if the dropdown view is shown (*) and if so, save the position that is currently selected in the spinner (int pos = spinner.getSelectedItemPosition()).
After calling setContentView and setting the spinner adapter, if the dropdown view was shown in step 1, force the dropdown view to show by calling performClick on the spinner:
spinner.setSelection(pos);// this way we make sure that the same item
// remains selected after rotating the device
spinner.performClick(); //show the dropdown view
(*) Checking if the dropdown view is shown is the trickier part. I haven't found (yet) a method that lets me know whether the dropdown view is shown, so I had to do the following:
Hold the spinner's pressed state in a boolean variable (named, for example, isClicked).
Set an onTouchListener for the spinner and in the onTouch method set isClicked to true (when tapping the spinner, the dropdwon view opens, so isClicked == true means that the dropdown view is shown).
Override onKeyDown or onKeyUp and when back button is pressed, if isClicked is true, set it to false (I assumed that pressing back with isClicked==true means closing the dropdown view).
Use the value of isClicked in onConfigurationChanged method to check if the dropdown view is shown.
Like I said, it's an ugly fix, but it's the only one I could come up with until now. If anybody has other ideas, please post them.
Humbly taken from here
There are other solutions too which are as follows
SO answer 1
SO answer 2
Bottom Line is you need to capture the status(some boolean variable) when the spinner is opened and when on activity configChange need to intialize the spinner state from the status
Hope it helps!!
Related
I try to make a spinner which on its first use shows a text like "please select..." in its collapsed state and when it shows the dropdown state, it shows all the items, but nothing is selected.
I tried different approaches:
use a list with an extra "please select..." item and try to remove it from the list on first dropdown (the problem is there is no notification when the spinner drops down for the first time)
try to hide the first item in the dropdown view by overriding getDropDownView. But when I set the height of the view at position 0 to 0 pixel, the spinner leaves some extra space at the end of its view, it looks like the size of the view is calculated before getDropDownView is called.
Any other ideas? Thank You!
Here's my problem:
In my android app I use AutoCompleteTextView and refresh items list for each 5 seconds. Refreshing is working, when I call notifyDataSetChanged(). But when I type here something and it'll give me the list of matching items and then I close it manually, when another thread is refreshing the list and calling notifyDataSetChanged() dropdown list is showing automatically (even if I closed it before). And it's really annoying, because if list is long it takes whole screen and after closing it'll appear in another 5 secounds.
I tried dismissDropDown() just after calling notifyDataSetChanged(), but it doesn't have any effect. It looks like dropdown is showing with a little delay. It's also not good way to fix it, because if user hasn't closed the list it'll close after a refresh. Also tried to take focus away - still no effect.
So I need a method to:
update my items list (via notifyDataSetChanged()) without automatically showing dropdown list, but if dropdown was already showed don't close it.
Hope you can help,
cheers
I did it, finally. I post here a solution, maybe someone will use this.
To check if dropdown is visible:
final boolean isVisible = autocomplete.isPopupShowing();
Next, call notifyDataSetChanged(),
Finally:
new Handler().post(new Runnable(){
#Override
public void run() {
if(!isVisible){
autocomplete.dismissDropDown();
}
}
});
This works perfectly.
I have also this problem, I think it seems to be a bug, i solved this by the following way
First of all set your AutoCompleteTextView focusable property to false.
Second change your AutoCompleteTextView focusable property to true in onTouch event of that view.For that you have to implement onTouchListener to the AutoCompleteTextview.
Whenever you touching on that AutoCompleteTextView, you should change focusable property to true otherwise keep it focusable to false. So that the dropdown list is never appear when the view is in unfocusable state.
It shows dropdown list automatically, only when the view is in focusable state which is annoying to user, otherwise it works good.
I don't know if it's clear from the title what I asked, so here are some steps to reproduce (assuming you have a layout with a spinner):
Tap on the spinner > dropdown list opens.
Rotate the device from portrait to landscape (or vice-versa) > the list is closing.
My problem is that I would like to keep the list opened after rotating the device. I know that this is usually possible if overriding onConfigurationChanged, but I have defined a different layout for landscape mode, so in my onConfigurationChanged method I have to call setContentView and set the adapter for the landscape spinner, which closes the dropdown view that was opened in portrait mode.
Is there a way I could prevent the dropdown list from closing or perhaps forcing it to show after rotating the device?
I found a very ugly solution to this (the 'action' takes place in the onConfigurationChanged method):
Before calling setContentView, check if the dropdown view is shown (*) and if so, save the position that is currently selected in the spinner (int pos = spinner.getSelectedItemPosition()).
After calling setContentView and setting the spinner adapter, if the dropdown view was shown in step 1, force the dropdown view to show by calling performClick on the spinner:
spinner.setSelection(pos);// this way we make sure that the same item
// remains selected after rotating the device
spinner.performClick(); //show the dropdown view
(*) Checking if the dropdown view is shown is the trickier part. I haven't found (yet) a method that lets me know whether the dropdown view is shown, so I had to do the following:
Hold the spinner's pressed state in a boolean variable (named, for example, isClicked).
Set an onTouchListener for the spinner and in the onTouch method set isClicked to true (when tapping the spinner, the dropdwon view opens, so isClicked == true means that the dropdown view is shown).
Override onKeyDown or onKeyUp and when back button is pressed, if isClicked is true, set it to false (I assumed that pressing back with isClicked==true means closing the dropdown view).
Use the value of isClicked in onConfigurationChanged method to check if the dropdown view is shown.
Like I said, it's an ugly fix, but it's the only one I could come up with until now. If anybody has other ideas, please post them.
By default the behavior on switching from portrait to landscape is restart your Activity. So, you can save the spinner state to somewhere, for example to SharedPreferences and read it when restarting the activity(in onCreate() or onResume()) methods.
Is there a way to programmatically deselect/wipe whatever the user has selected with the trackball/trackpad?
When I hit the back button on an Activity, the Activity it falls back to has a button that is selected as if the user had used the trackball/pad. I'm not sure what is selected on the previous Activity, but obviously something is. I'd like to programmatically wipe any selection just before the Activity finishes.
Looking through the JavaDoc for View I see a number of focus-related functions.
void clearFocus(); // drop focus from this view.
View findFocus(); // finds a view that is a child of this view that has focus, if any
View focusSearch(int dir); // finds the next view that can take focus in the given direction
void requestFocus
Sounds like findFocus().clearFocus() should do the trick (unless findFocus happens to return null)... you just need a handle to the other activity's View... which shouldn't be too hard if it's your code, or Non Trivial if it isn't.
If it IS your code, it seems like you could just add a clearFocus() to the button's onClickHandler.
In Android, I want to present the user with a list. When an item on the list is selected some action is performed, and this list item is no longer selectable. It is also 'grayed out' or the like to indicate that it cannot be selected the next time the list is displayed. I have seen the isSelectable() override in Adapter, but I believe this causes the item to be treated as a separator, which causes visual problems. And I have not found a way to 'gray out' an item. Any ideas? Thanks...
As far as graying out an item. I'm not sure if this is the best way, but it's what I do:
view.setAlpha(75);
view.setBackgroundColor(Color.GRAY);
I'm basically making the item transparent and then setting the background color to gray. If you're reusing your list items you should also change them back to their original state if the condition is not met, i.e.:
view.setAlpha(255);
view.setBackgroundColor(Color.WHITE);
that is, if your original state was no transparency and the background color was white.
You need the view to be disabled. If you are creating the views just call .setDisabled(boolean) on the top view. Setting the list item to be disabled doesn't work very well in my experience.
Here is the solution I am using. I set up an OnItemClickListener for my ListView. When an item in the list is clicked, I take the passed in View and call setEnabled(false) on it. This will gray out the item. However, subsequent clicks on this item will still call the onItemClick method. So, you will need to check on each click if the item is enabled/disabled and act accordingly.