I have 20 buttons in vertical layout in a scrollable window. While click on a button I want to know the position of that button on screen.Some times I click the button after a scrolling.I want the exact position at that time too.What is the code for that.please help me.
Never use 20 buttons inside layout. Use listview with button and use onitem click of listview and get button position:
list view onitem click:
listId.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> list, View v, int pos, long id) {
-------Here position displays **pos**----
}
});
Refer this example for a nice listview sample
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
System.out.println("this is position"+lv.getItemAtPosition(arg2)
}
});
Related
Could anyone possibly give me an entire example for demonstrating a ListView.
Each item in listview needs to fire a new activity.
For ex, if I use 'Subjects' as content of ListView then after pressing particular a subject from listview , a new screen with its title and some content of that subject gets displayed.
I know this is too much to ask, but i have been looking for ListView Ex. since quite long and nothing has helped me so far. PLease Help
Thank you
yourlistview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
then use bundle to send some content to your next activity.
then getActionBar.setTitle(sometext); this text is come from your bundle.
This contains:
http://www.androidbegin.com/tutorial/android-search-listview-using-filter/
you can refer following link for listview demo code.
http://www.androidhive.info/2011/10/android-listview-tutorial/
and for opening new activity on selecting any item, you can use,
listCoupon.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
// here you can compare selected item by its position and then start a new activity
// arg2 is selected item position in your arraylist
}
});
Can anyone knows how can i make something like this in ListView ?
When a item in ListView is Clicked the imageButton get Visible.
http://img850.imageshack.us/img850/2377/is4t.png
Then you click another item and the previus item that you clicked get invisible , and the actual item get the image button visible.
http://img14.imageshack.us/img14/1920/lnpy.png
I have searched about this , but i haven't found something to work exactly i want. And i have tried to make the button visible onItemClick for that view , but , when i click another button , the previus item still visible. I have tried to do notifyDataBaseChanged but still there. Thanks for help , and sorry for my bad english.
¿How can i get access on the previous Item View to set button to View.GONE ?
Code :
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position,
long arg3) {
// TODO Auto-generated method stub
MPlayer.playSong(position);
Button bPlaying = (Buttton)view.findViewById(R.id.button1);
bPlaying.setVisibility(View.VISIBLE);
ca.notifyDataSetChanged();
}
});
Not the best solution but hope this might help you solving you the problem
private Button previousButton = null;
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
MPlayer.playSong(position);
if(previousButton != null){
previousButton.setVisibility(View.GONE);
}
Button bPlaying = (Buttton)view.findViewById(R.id.button1);
bPlaying.setVisibility(View.VISIBLE);
previousButton = bPlaying;
ca.notifyDataSetChanged();
}
});
I searched a lot but I couldn't find a straight way for that. All I need is when I clicked on a menu-item I can realize that which on of items in the list is focused?
For example in the blow picture how can I know which item is highlighted(when i clicked on a button for example on the Back Button)?
You must implement an OnItemClickListener
myList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
YourClass yourObject = (YourClass) arg0.getItemAtPosition(arg2);
}
}
This should do it:
http://developer.android.com/reference/android/widget/AdapterView.html#getSelectedItemPosition()
or keep a reference of the current selection.
as we know using android grid view, we can do the following and get notified when item is clicked:
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(PopularCapitActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
we also know that, if the cell in the grid contains a clickable item, say a button, the above won't get fired.
so currently i have a grid view, each cell has its own button, so now when user clicks on the button, it will have its own action based on the cell the button resides in, my question is, how can i access the cell position in the button handler?
thanks
Assuming you are using a custom adapter for the GridVIew, in the getView method you can simply add a tag to the Button object that contains the position passed into getView:
button.setTag(new Integer(position));
Then, in the onClickListener method, with the view that is passed in (the button) you can do:
Integer position = (Integer)view.getTag();
And then handle the position value from there.
EDIT:
It appears the best practice would be to do:
button.setTag(Integer.valueOf(position));
rather than using the Integer constructor.
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
String a = String.valueOf(position);
Toast.makeText(getApplicationContext(), a, Toast.LENGTH_SHORT).show();
}
});
I created a custom ListView with ImageView and TextViews and every thing worked fine until i tried to implement onItemClick, which for the time being only shows a Toast.
The problem occurs when i scroll down my ListView: it won't receive any clicks.
Funny thing is that when i use the keyboard to move from item to item it works and when i hit enter the Toast is shown
This is the code i used for onItemClick listener.
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
RestaurantReservationBean clickedItem = resultArray.get(position);
Toast.makeText(this, clickedItem.getName()+", "+clickedItem.getCost(), 1000).show();
}
i think i solved this problem: after going through some documentation i figured out that this problem comes from the textviews and imagesview on top of each row which block the onitemselected listener. so i tryed to refresh the list view after scroll and it worked just fine. here's what i did hoping it 'll help those who may come accross this problem
listView.setOnScrollListener(new OnScrollListener() {
public void onScrollStateChanged(AbsListView view, int scrollState) {
if ( scrollState == OnScrollListener.SCROLL_STATE_IDLE )
{
listView.invalidateViews();
}
}
#Override
public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) {}
});