I have a GridView whose items are ToggleButtons, and I defined an event listener for clicks on items, but nothing happens when I click on them. Here's the listener:
paintActionsGrid.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent,View view,int position,
long id)
{
((ToggleButton)view).setChecked(true);
paintBoardView.setPaintAction(paintActions[position]);
}
});
I put a break point in the listener, but it's not reached, which means the listener is not called at all!
Can somebody please tell me what's wrong? Is it related to the fact that the items are ToggleButtons? I can attach the adapter code if needed.
Thanks.
The click event is consumed by ToggleButton hence not passed to the GridView. Similar to this post.
Yeah the problem is ToggleButton by default has its clickable enabled and hence your ItemClickListener won't work. The idea is to set the ToggleButton's focus to false by default.
Assuming that you have a custom xml for your ToggleButton, add this line to it,
android:focusable="false"
Since you have a focused element, this problem occurs. Try this simple idea.
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.
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 ListView and on clicking a list item, it starts another activity.
So my problem is that, when I click on list item, it should change its background and move to another activity and on pressing back on this activity the list item should retain it changed background color. And again clicking on another list, it should remove the earlier background color and again do the same.
Thanks in advance.
You'll want the setItemChecked() method in the ListView. This should make the item you click "permanently on", if you place it in the onItemClick() method in a listener. Note that when you navigate to the next activity, you will have to use onSaveInstanceState() and onRestoreInstanceState() to store which item in the ListView has been clicked; I don't believe Android will retain which one has been clicked when you navigate away from the Activity with the ListView.
There is quite a good tutorial of this in the Fragments section of the Developers website, here.
Hope this helps.
Keep track of the selected item. Use a custom adapter and override the getView method. In this method check if the position of the view is the selected item and change the background of the view accordingly.
Override the item click listener and do the following to change the color of the clicked view:
listView1.setOnItemClickListener(new ListView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View v, int arg2, long arg3)
{
v.setBackgroundColor(Color.BLACK);
}
});
I solve it by setItemChecked() methood and use android:choiceMode="singleChoice" inside ListView in my layout xml file. I used MyClass.this.getListView().setItemChecked(position,true); on clicking the Item in the list and the on getView() i am doing this...
if(position == MyClass.this.getListView().getCheckedItemPosition()){
holder._newsLinear.setBackgroundColor(Color.CYAN);
}
else{
holder._newsLinear.setBackgroundColor(Color.BLACK);
}
I've searched around and have not come out with a solution (maybe not using the correct keywords).
So, I've a custom ListView which its item can be dragged around when the item is long clicked. Within its item, there's an ImageView and LinearLayout containing two TextViews. Actions are done when the LinearLayout or ImageView is clicked.
To do this, I've use setOnItemLongClickListener on my DragListView which extends ListView, to initiate drag action, and onInterceptTouchEvent to manage the drag action.
Then, I have built a custom adapter extending BaseAdapter and overrided its getView() to implement the child items in the row. The LinearLayout and ImageView have been setOnClickListener.
The problem is, the LinearLayout and ImageView are able to do their stuff, but the onItemLongClick isn't called.
The listener inside getView();
holder.delete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Do something
}
For item long click (drag initiator)
setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
//Do something
}
Thank you very much!
I think that a gesture detector is one of ways to handle events.
Usually, however, a gesture detector is used when we want to detect a gesture not a long-press.
The reason why onItemLongClick isn't called is that onClickListener might consume a touch event.
In that reason, if you want to handle onItemLongClick, intercept touch event and dispatch it to views you want to handle.
You can find more details following link.
http://developer.android.com/guide/topics/ui/ui-events.html
Ok, just found out the solution myself.
Instead of using onItemLongClickListener, I create a gesture detector to detect for long press. Then I override dispatchTouchEvent and force to scan for long press first, then return super.dispatchTouchEvent and the other following touch events.
Suggestions are still welcomed!
I am trying to set item selected in OnItemClick event in ListView and it just wouldn't leave item selected. What am I doing wrong?
lView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(#SuppressWarnings("rawtypes") AdapterView parent, View clickedview, int position, long id)
{
clickedview.setSelected(true);
mItemsAdapter.select(position);
}
});
few things:
1. I am trying to implement Multiple Select on the list View.
2. I cannot extend from ListActivity because Activity extends from BaseActivity custom class already.
3. mItemsAdapter is a custom ItemsAdapter adapter that extends BaseAdapter.
4. I don't need a checkbox in there, just to be able to see the row selected is fine.
5. ItemsAdapter overrides getView() and sets the layout of the row by inflating xml
I could manage to get an item of a ListView to be set as selected when long-clicked:
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
parent.requestFocusFromTouch(); // IMPORTANT!
parent.setSelection(position);
return true;
}
It only worked after I called requestFocusFromTouch().
I currently don't have much time. So I'll take a look again later this day.
Anyway take a look at my previous questions, I was struggling with the same:
Change ListView background - strange behaviour
Clickable ListView
In case the solution for you is not in there (I think it's in the first one) we will need more code, in order to help you.
Hope this helps a bit.
I have not had much luck using the built in functionality for selection.
I see you have your own custom adapter, which I am guessing means your inflating custom views as rows. If your row has anything more then a checkedtextview I don't think you will be able to correctly use setSelections.
I solved this problem by using my own models and functions. Each item in the list had data with it to determine it if was selected or not. I could then iterate though that array, toggle selections with and and even update the UI by changing values and calling notifydatasetchanged on the adapter (which used getView and checked against my selection model to draw checks).