android how to give click event on gallery item - android

http://www.inter-fuser.com/2010/02/android-coverflow-widget-v2.html
Just go through the above link. there is an example sample code for Android Coverflow widget.
I want to give click event on the images. How can I do it.
Plz suggest me any way.
Thank you

Follow below code
mCoverFlow.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long arg3)
{
// TODO Auto-generated method stub
}
});

Hmmm, since the widget extends from Gallery, presumably you can just add an onItemClickListener:
coverFlowView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> gallery, View view, int position, long id) {
//do stuff!
}
});
in the same way, you can set an onItemSelectedListener, which fires whenever an item is "selected" (comes in the center of the gallery:
coverFlowView.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> list, View view, int position, long id) {
//do stuff!
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});

Related

How do I enable click on listview?

I'm currently working on a code which is written by someone else in my office. I need to enable selection of items in the listview. I know I have to use setOnClickListener. Can someone guide me through this? I've written:
Edit:
listview.setOnClickListener( (View.OnClickListener) this );
Now what do I do?
I need to select an int value and pass it on to another function, which is used to retrieve a certain set of values from a db.
listviewName.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//variable position will give you the required element/object from the array list.
}
});
YourListView.setOnItemClickListener(new ListView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int pos, long l) {
try {
Toast.makeText(this,"Position is===>>"+pos , Toast.LENGTH_LONG).show();
}
catch(Exception e) {
System.out.println("Nay, cannot get the selected index");
}
}
});
Hope it helps.
ListView list = (ListView)findViewById(R.id.quoteX2);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
// - -- - -Your Code here
}
});
Hope it helps!

LongItemClickListener on Listview android

I have a question, Can we apply OnItemClickLisyener and OnLongItemClickListener on the same ListView?
I want to delete items of listview on long item click, listview is already handling ItemClickListener to view files.
What should I do now?
Can someOne explain how to do this?
Here is example
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//do your stuff
});
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent,
View view, int position, long id) {
//Do your stuff
}});

ListView ContextMenu in Fragment

In Fragment onCreateView i have the following code
consumerlist=(ListView)view.findViewById(R.id.consumerlist);
consumerlist.setAdapter(new consumerListAdapter(getActivity(), dataList));
consumerlist.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> listview, View view, int position,
long id) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), "ID"+id, 1);
}
});
Button btn=(Button)view.findViewById(R.id.button1);
registerForContextMenu(consumerlist);
return view;
if i write registerForContextMenu(btn) then it works.but list view row long click does nothing.Even my listView onItemClick listener does not work.how to fix it?Anybody helps me greatly appreciated.Thanks in advance.
try this:
consumerlist.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
registerForContextMenu( view );
openContextMenu( view );
return false;
}
});
also this tutorial should help you a lot ...
//EDIT
Move
registerForContextMenu(consumerlist);
to
public void onActivityCreated(Bundle savedState) {
Remove scollview in layout or define it as not focusable make the code working.

Adding an event listener to listView

getSherlockActivity().findViewById(android.R.id.list);
new OnItemClickListener() {
#Override
public void onItemClick(AdapterView <? > arg0, View arg1, int position,
long id) {
// TODO Auto-generated method stub
Toast.makeText(getSherlockActivity(),
"You selected :", Toast.LENGTH_SHORT).show();
}
};
I am trying to add an event listener to my activity, but the toast message is not shown at all. I am calling this piece of code from onCreateView method
It doesn't seem like you are actually setting the click listener on your list, you are just creating it, and not even keeping a reference to it.
try like this:
ListView lv = (ListView)(getSherlockActivity().findViewById(android.R.id.list));
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView <? > arg0, View arg1, int position,
long id) {
// TODO Auto-generated method stub
Toast.makeText(getSherlockActivity(),
"You selected :", Toast.LENGTH_SHORT).show();
}
});
getSherlockActivity().findViewById(android.R.id.list).setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView <? > arg0, View arg1, int position,
long id) {
// TODO Auto-generated method stub
Toast.makeText(getSherlockActivity(),
"You selected :", Toast.LENGTH_SHORT).show();
}});
EDITED
((ListView)getSherlockActivity()).findViewById(android.R.id.list).setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView <? > arg0, View arg1, int position,
long id) {
// TODO Auto-generated method stub
Toast.makeText(getSherlockActivity(),
"You selected :", Toast.LENGTH_SHORT).show();
}});
Add this line inside onCreate().
I do not see you adding anything. All you did was findViewById() for no purpose (as you did not used return value. Also you just created new OnItemClickListener for no reason as you do not use it either. And no, putting two lines of code next to each other won't make then automagically interact in any way...
Right approach is:
ListView v = (ListView)getSherlockActivity().findViewById(android.R.id.list);
v.setOnItemClickListener( new OnItemClickListener() {
...
...
Couple of weeks ago, I found same situation. I didn't use Sherlock but, it may help to set following line.
listView.setItemsCanFocus(false);
Also make sure to set clickable false to any Button in your row item.
Only add
ListView lv = (ListView)getSherlockActivity().findViewById(android.R.id.list);
lv.setOnItemClickListener(this);
#Override
public void onItemClick(AdapterView <? > arg0, View arg1, int position,
long id) {
}
and Activity should implement OnItemClickListener
or
ListView lv = (ListView)getSherlockActivity().findViewById(android.R.id.list);
lv.setOnItemClickListener( new OnItemClickListener() {
#Override
public void onItemClick(AdapterView <? > arg0, View arg1, int position,
long id) {
}

Check selected item of ListView

I have a ListView and need to check, which item has been clicked.
The way I have it, it only shows my the selected item when I use the scrollwheel of my mouse (when testing it in the Eclipse emulator). When I test it on a real mobile device, it doesn't react at all.
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems);
listView.setAdapter(adapter);
listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
myFunction(listView.getSelectedItem());
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Use setOnItemClickListener instead of setOnItemSelectedListener
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
//get value for listItems using position
String str =listItems[position];
myFunction(str);
}
});
I think better you need to use on onListItemClick listener
public void onListItemClick(ListView parent, View v, int position, long id)
{
parent.setItemChecked(position, parent.isItemChecked(position));
Toast.makeText(this, "You have selected" + items[position],30).show();
}

Categories

Resources