I had a scenario where I am using spinner in my app. A made spinner drop down list to open up directly using "performclick()" method . The drop down list is opened. Is there a way in which I can close it automatically or click an item from drop down list automatically so that the drop down list is dismissed.
Borg8's answer is the only solution that works! Here is my version to add the spinner back.
if (spinner.getParent() == null){
parent.addView(spinner);
spinner.setLayoutParams(params);
spinner.forceLayout();
}
Spinner does not expose any means to close it explicitly. If setSelection() does not have that effect, then you may need to write your own widget that gives you more control, or avoid trying to do this.
This solution works for me and, for now, I don't see any reason why it will stop work.
If spinner is your spinner, then:
// get spinner layout and its parameters
ViewGroup parent = (ViewGroup)spinner.getParent();
ViewGroup.LayoutParams params = spinner.getLayoutParams();
// remove spinner from the layout - will dismiss its dropdown menu
parent.removeView(spinner);
// add spinner back and set same parameters
parent.addView(spinner);
spinner.setLayoutParams(params);
That's it
Related
I want to make custom spinner in which user can select one of the items on dropdown list (like in normal spinner) but I want to prevent from closing (collapsing/dismiss) dropdown list after item selection. Or closing dropdown when user tap two times on item instead of single tap. Is anyone here knows how to achive such behavior using spinner?
Set itemSelectedListener on your spinner and onItemSelected() run this code
spinner.setClickable(false);
I got a fragment with a ListView. The purpose of the list is simply checking items in that list, so I want to add something to the onCreateView method which will make the list enter its multiple selection mode automatically as the fragment shows, without the need for the user to long press an item. How can I do that?
to use longpress , you can do this way,
android:choiceMode="multipleChoiceModal"
or
setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL)
go through this for more information
I want to have a dropdownlist like this:
But i want to pop it up when you click a menu item. So spinner is not a necessary. Is there a standalone dropdownlist, or is it possible i trigger the spinner automatically?
You can do this a few ways:
Give your item a submenu by putting another <menu> element inside the <item> tag.
Handle the item click yourself and show a ListPopupWindow
Create a DialogFragment. Override onCreateDialog() and use an AlertDialog.Builder to create the dialog. Call one of the variations of setSingleChoiceItems() on the builder to set a list of single choice items to show in the content.
The first method is maybe the easiest since the selection will call back to your onOptionsItemSelected() method and you can easily handle it there, but the other options may be of interest to you as well.
If you want to show a dropdown list on an menu item click, you need to use the Spinner object.
Alternatively you can also use the Action bar with drop-down navigation : http://developer.android.com/guide/topics/ui/actionbar.html#Dropdown
I am working on a simple to-do list app, and on the main view I have a list of tasks with a checkmark aside to each, as the image below:
Upon clicking on the checkmark I want the respective task to be removed from the list. I tried to implement this as TextViews (for the tasks) and Buttons (for the checkmark), but I would need to know the number/position of the clicked checkmark (0,1,2 or 3) to remove the correct task from my array. Can I get this some how?
I also thought about implementing the tasks/checkmarks as a ListView, but then I would need to set a onItemClickListener only on the checkmark, and not on the task text. Is this possible?
Any other ideas? Thanks.
Checkbox has a tag property which you can use to set custom data like row number. Something like this should work,
checkbox.setTag(row_number);
So when you click on it, do something like
int rowNum = Integer.parseInt(checkbox.getTag());
removeTaskAt(rowNum);
If you're using a Custom Adapter, you can add a click listener on the check on getView, so the click is only on the checkbox and not on the full item.
The only problem with this is that the scroll may not work if you try to scroll by clicking on the checkbox.
When you touch some grid or listview items in an android app, he change the background color telling you that you have pressed it.
I'm having a problem in my app. When I create that a listview for instance, and I press it in my phone, he change color, but if I set the OnClick property, the item dont change it color. Whats wrong? The OnClick works as its suppose.
Thanks
Generally with ListView you want to use an OnItemClickListener.
This gives you a chance to perform an action based on the position in the list that was clicked.
If you need access to the view adapter from within onItemClick then you can use AdapterView#getAdapter() on the AdapterView that is supplied to you.