My code is very long so I will describe my problem. It's about the size and position of a Spinner. My ListView has items, each item has a TextView and a Spinner. The data in Spinners is different. I only operate data of the first one when I click the Spinner. It will pop out. The data in the Spinner comes from a list of database. When I edit the data (delete, move, add) in fact, I edit the data from an array. The question is when I pop out the spinner it will fill the whole screen, show at the centre of screen, the area around it will be gray and if I click the gray area the Spinner will hide again but I need it keep out state so I can do something about its data and don't affect the other area (the grey area). Would anyone help me please? Thank you very much.
If you are trying to detect when the Spinner is hidden then you should implement OnItemSelectedListener in your Activity.
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. Handle your data here
}
public void onNothingSelected(AdapterView<?> parent) {
// No item was selected
}
Related
Good day. I have an android application which displays a ListView. Upon clicking the any of the rows, I want all other rows to be grayed out. I am using a custom ListView with my own BaseAdapter and I know I can create an onclick function on the getView function there. However, doing so invalidates the onItemClick function I have in my base activity that's why I want to do this in my base activity as much as possible.
Here is how I made my onItemClick function:
resultListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view,
int position, long id) {
// TODO Auto-generated method stub
resultListView.smoothScrollToPositionFromTop(position, 0);
resultListView.setClickable(false);
resultListView.setScrollContainer(false);
resultListView.setEnabled(false);
};
});
}
Is there a way to gray out all the other items except for the chosen item? Even changing the alpha/lowering it would help. I want to give the user an impression that the other list items are not clickable. I'm currently having a tough time figuring it out. Any help is very much appreciated, thank you.
EDIT
I am able to adjust the alpha by doing:
resultListView.setAlpha((float) 0.5);
However, it affects the whole row and I am unable to adjust the alpha for the selected row.
Create class to hold variables. Pass this class to the adapter instead of string for example. Create a boolean variable isSelected in your class. Set isSelected in onItemClick(), and enable/disable the views from getView() depending on your isSelected variable.
i am trying to change the background color of the selected row in a listview and i am able to do it.
But when i am clicking on another row then the background color of the previously selectedrow remains unchanged. I have the position of the previously selected row, Can anybody help me that how can change the background color of previously selected row as it was before??
If you keep track and update the listItems clicked state in your model you can just put the code for displaying has been clicked-color in the adapter and then call
adapter.notifyDatasetChanged();
I think this might be easier if you look at it another way.
Currently, your logic is "if I click on this row, change its color to a special color and change the old row's color back to the original color". However, this doesn't seem to be the logic you actually want. Rather, you want the last clicked (aka selected) row to be a different color.
You haven't posted any code, so I don't know if you are implementing your own ListAdapter in this project. That is the approach I would take. Create a class which extends ListAdapter, and create an additional private variable that stores the position of the last selected row. Then in the overriden getView() method, do a quick check
if(rowPosition == lastSelectedRowPosition)
viewToReturn.setBackgroundColor();
If you aren't sure how to make your own list adapter, check out the tutorial at http://jnastase.alner.net/archive/2010/12/19/custom-android-listadapter.aspx.
In your activity use setOnItemClickListener of Your_List Object.
See Demo Code:
list1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
v.setBackgroundColor(Color.BLUE); // <--- Use color you like here
// ^ this v gives current row.
}
});
This will make background color changed for that row forever.
Here's what I want to do:
I have this list of tasks. When I click on one of the items I want this to appear:
Assuming that menubar can be found with
findViewById(R.id.menubar);
Can someone please tell what code I have to put onItemClickListener
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
NOTE: The menubar visibility its set as GONE.
Thanks!
Without looking at the code, a precise answer is not possible. However, when I first wanted to implement a "Quick Action Bar" similar to the one you have shown, I had followed this example here:
http://code.google.com/p/simple-quickactions/
And this with some styling put in: https://github.com/lorensiuswlt/NewQuickAction3D
Hope this helps....
It is a bit difficult without seeing any code, but I can give you some logic ideas.
Based on your description, I'm assuming that those quick bar controls are hidden on EACH listview item so, it goes:
Fotos (visible)
Action Controls (hidden)
[End of Item]
PROJECTOS (visible)
Action Controls (hidden)
...
I think the OnItemClick method gives you the view (item: Fotos, Projectos, etc) that was clicked as the 2nd argument. If you cast that back to whatever layout you used to create the listview items, you should be able to use findViewByID to get access to the hidden controls that are on each listview and make them visible.
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
//Assuming each item is a linear layout
LinearLayout itemAsLL = (LinearLayout)view;
//find the action bar controls
LinearLayout actionControlsLayout = itemAsLL.findViewById(R.name.of.actioncontrols.id);
//Make it visible
actionControlsLayout.setVisibility(VIEW.Visible);
}
You would need to find a way to hide those controls when another item is clicked? Maybe save the view (or position in the adapterView ) as a class variable and when another item is clicked, go to that view/find that view and hide the controls.
I finally found out the answer. Apparently, when you fill a view with lots of tasks, the adapter does not fill everything when you load the layout, so everytime you scroll down, hes putting more items on the listview.
What you have to do is make sure you put a field "loaded" and, in case is false, it does not show the menubar below that item, otherwise, when you click in one of the items of that list, when you scroll down, since they will have the same relative position (if you click on the first item, the position = 0, when you scroll down, there will be another position = 0) the menubar will appear on both items.
Hope it helps ;)
I have a list of items, which i'd like to "check" with filled star when user clicks on one item in the list.
I have a ListView with text & image, represented in XML layout, and using simple StringAdapter.
I've implemented the above by doing this:
this.listViewSub.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(final AdapterView parent, final View view, final int position,
final long id) {
ImageView img = (ImageView) view.findViewById(R.id.unchkImg);
img.setImageResource(R.drawable.starchk);
I guess its not the right way to do it, but i don't have any idea how else to code it.
This code for some reason changes few random items in the list, and after one click i have about 4-5 items with icon changed.
Any idea how to solve it correctly?
Thanks
It's hard to tell without code, but my first guess is that as the views get recycled, if you don't reset the ImageView to use the default "unchecked" image in the getView method, each time you star an item the view will remain starred, even it is used for a different item. So your adapter should just reset the ImageView's image resource ; though in that case, you'll have to remember which items you starred in order to reset the ImageView correctly : set it to do the default drawable or to the starred one.
At last, after long search - i've found the answer in an article:
http://www.codegod.biz/WebAppCodeGod/Android-ListView-with-dynamic-Images-AID588.aspx
That was a tough one.
refering to this question: how does scrolling in android listview work?
on change from state 2 to 3 the clicked listitem is increased in its size to show more information. if the clicked item was the last one on screen, the extended listitem is not fully visible because the new content flows out at the bottom of the screen.
my current solution is to call "setSelection(index)", if the last visible listitem was clicked. this results in a state were the selection is brought to the top of the screen. this is annoying because the listitem moves from bottom to top.
is there a way to avoid this? this means, is there something to let the listview know, that is should scroll only the minimum amount of pixels, so that the clickeditem is fully visible?
or have i to programm this functionality on my own?
Add a custom OnItemClickListener to the ListView. Implement OnItemClickListener.onItemClick like this:
public void onItemClick(final AdapterView<?> parent, final View view,
int position, long id) {
View hiddenContent = view.findViewById(R.id.hiddenContent);
hiddenContent.setVisibility(View.VISIBLE);
// At this point the layout hasn't be redone and you don't have reliable
// measurements on the view. It would be nice to do something after the view
// has gone through another layout
view.post(new Runnable() {
public void run() {
Rect r = new Rect();
view.getDrawingRect(r);
parent.requestChildRectangleOnScreen(view, r, false);
}
});
}
Whenever you click on a list item to show the hidden content the ListView control will go ahead and do whatever scrolling is needed (or no scrolling if none is needed) to completely show the row.