I am using a custom ListView with RatingBar and ImageButton. Here is my problem: When I click on my ListView, my OnItemClickListener is not working. Please can any one help me.
Code:
ListView lv = getListView();
setContentView(lv);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Toast.makeText(SuggestionActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
Though a very old question, but I am still posting an answer to it so that it may help some one.
If you are using any layout inside the list view then use ...
android:descendantFocusability="blocksDescendants"
... on the first parent layout inside the list. This works as magic the click will not be consumed by any element inside the list but will directly go to the list item.
I have an Activity that extends ListActivity.
I tried doing something like this in onCreate:
ListView listView = getListView();
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.i("Hello!", "Y u no see me?");
}
});
But that didn't work.
Instead I simply needed to override onListItemClick:
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Log.i("Hello!", "Clicked! YAY!");
}
Hey check this, works for me... hope it work for u too
If list item contains ImageButton
Problem: OnItemClickListener just doesn’t repond any at all!
Reason: No idea
Solution: in code, set ImageButton's focus to “false”
1: ImageButton button = (ImageButton) convertView.findViewById(R.id.imageButton);
2: button.setFocusable(false);
If you want to enable item click in list view use
listitem.setClickable(false);
this may seem wrong at first glance but it works!
if list item view contains button or checkbox or imagebutton,
the onitemclicklistener and onitemlongclicklistener not working due to it has own onclick listener.
set focusable as false
holder.button.setFocusable(false);
1) Check if you are using OnItemClickListener or OnClickListener (which is not supported for ListView)
Documentation Android Developers ListView
2) Check if you added Listener to your ListView properly. It's hooked on ListView not on ListAdapter!
ListView.setOnItemClickListener(listener);
3) If you need to use OnClickListener, check if you do use DialogInterface.OnClickListener or View.OnClickListener (they can be easily exchanged if not validated or if using both of them)
You can also use lambda. Lambda syntax is not supported under Java 1.7 or earlier JVMs.
listView.setOnItemClickListener((parent, view, position, id) -> {
...
});
listPaired = (ListView) findViewById( R.id.listView1 );
listPairedData = new ArrayList < String >();
araPaired = new ArrayAdapter( this, android.R.layout.simple_list_item_1, listPairedData );
listPaired.setAdapter( araPaired );
listPaired.setOnItemClickListener( listPairedClickItem );
private OnItemClickListener listPairedClickItem = new OnItemClickListener() {
#Override
public void onItemClick(AdapterView < ? > arg0, View arg1, int arg2, long arg3) {
String info = ( (TextView) arg1 ).getText().toString();
Toast.makeText( getBaseContext(), "Item " + info, Toast.LENGTH_LONG ).show();
}
};
setClickable as false to ImageButton like this
imagebutton.setClickable(false);
and then perform OnItemClickListener to listview.
If you define your ListView programatically:
mListView.setDescendantFocusability(ListView.FOCUS_BLOCK_DESCENDANTS);
In Java as other suggest
listitem.setClickable(false);
Or in xml:
android:clickable="false"
It works very fine
Is there and image in the list view that you are using>
then follow the link:
http://vikaskanani.wordpress.com/2011/07/20/android-custom-image-gallery-with-checkbox-in-grid-to-select-multiple/
I think when you work out on the link that I have provided first every thing will work fine, I have tried that. If you want a refined answer please elaborate the question with code and description.
Just insert the line into RatingBar:
android:isIndicator="true"
In XML the ratingbar look like this.
<RatingBar
android:id="#+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:isIndicator="true"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false" />
Android OnItemClickLIstener conflicts with the OnClickListener of items of row of listview in Adapter. You just have to make sure your code is well managed and properly written with standards.
Check the answer in the link given below:
Make list clickable
lv.setOnItemClickListener( new OnItemClickListener() {
#Override
public void onItemClick(AdapterView < ? > parent, View view,
int position, long id) {
// TODO Auto-generated method stub
}
} );
Asked by many, The childs in list must not have width "match_parent" if you are looking for listview click only.
Even if you set the "Focusable" to false it wont work.
Set the child's Width to wrap_content
<TextView
android:id="#+id/itemchild"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
...
The most simple solution and the easiest way I can provide is this. You just need to do these two steps.
1.add this in your XML file.
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:id="#+id/list_view"
android:layout_below="#+id/info_text"
/>
Add this in your MainActivity file.
ListView listView;
listView = findViewById(R.id.list_view);
String[] fruits = new String[5];
fruits[0]="hello";
fruits[1]="hello";
fruits[2]="hello";
fruits[3]="hello";
fruits[4]="hello";
List newList = new ArrayList(Arrays.asList(fruits));
ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, newList);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
String selectedItem = (String) parent.getItemAtPosition(position);
Toast.makeText(DisplayPannel.this, selectedItem, Toast.LENGTH_SHORT).show(); }
});
Related
I'm developing an application for Android TV. I made a custom view where I represent list items (in RecyclerView).
How can I get a focus on my items and highlight them (a simple example would be wonderful)?
I did try to add
android:focusableInTouchMode="true"
android:focusable="true"
to .xml file, but I've not seen any changes.
EDIT: I want to get focus when navigating through items by dpad
in youf adapter do something like this
#Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
viewHolder.YOUR_LAYOUT.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// do whatever you want
viewHolder.YOUR_LAYOUT.setBackgroundResource(R.drawable.ripple_effect);
}
});
}
Why don't you use setOnitemClicklistener on your listview or recycleview . This is how you can do this:
ListView lv = getListView();
setContentView(lv);
lv.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View view,int position, long arg3)
{
Toast.makeText(Activity.this, "" + position, Toast.LENGTH_SHORT).show();
//========== Here you can change the color of selected item ========
TextView Tv_title = (TextView)view.findViewById(R.id.title);
Tv_title.setTextColor(Color.RED);
//====== Your can make any view color change here just like i did for textview ===
}
});
If still you are not getting focusable try using this solution :
Use android:descendantFocusability="blocksDescendants" in your XML file of the first parent layout inside the list.
I am using android built in layout android.R.layout.simple_list_item 2. I wanted to perform onItemclicklistner() for the items in this layout. I couldn't find the resource id for this layout without which I couldn't find a way to perform the listner function. The examples I have seen so far is
ListView list = (ListView) findViewById(R.id.mylist); // Since I am using built in layout I couldn't figure out the resourse id as in this case.
list.setOnItemClicklistner();
So my problem is without knowing the resourse id of the built in layout "simple_list_item 2" how can I create a Listview object. and without having a ListView Object I am unable to access setOnItemClickListner(). Hope I delivered the question in a meaningful way. Thanks
Use setOnItemClickListener() instead of setOnClickListener()
If you want to perform clicks on items in the ListView, try using OnItemClickListener in place of OnClickListener. The OnItemClickListener can be set on the ListView as shown below.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
// Perform the required tasks here
}
});
for list item click u should set onItemClickListener like below
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
}
});
also you can set onItemClickListener Interface to your Activity and override method in activity
use setonItemClickListner();
listview.setOnItemClickListner(new OnItemClick(){});
this is the activity
public void onListItemClick(ListView parent, View v,int position, long id) {
String str;
if (nRowSelected>=0) {
View row=parent.getChildAt(nRowSelected);
if (row!=null) {
row.setBackgroundColor(0xFFFFFFFF);
}
}
nRowSelected=position;
v.setBackgroundColor(Color.GRAY);
}//onListItemClick
this is my listview
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="425dp"
>
</ListView>
i need highlight single choice. i choose/focus row number 1. but when i scroll, the focus is more than one. the row focus in row 8 too
this is the capture
and
how to fix that?
You are fighting the way Adapter's recycle the row layouts... You need to extend your current Adapter and override getView() to highlight the correct row (and only the correct row).
At the most basic level it would look like:
public View getView(...) {
View view = super.getView(...);
if(position == mRowSelected) {
view.setBackgroundColor(Color.GRAY);
}
else { // You must use a default case to "un-highlight" the reused layout
view.setBackgroundColor(0xFFFFFFFF);
}
return view;
}
Add this to the xml:
android:cacheColorHint="#00000000"
Replace your xml code with following::
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="425dp"
android:cacheColorHint="#00000000">
</ListView>
Hi Please see the below code might be help it is single choice selection example follow this it work good .
public class List17 extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Use the built-in layout for showing a list item with a single
// line of text whose background is changes when activated.
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_activated_1, mStrings));
getListView().setTextFilterEnabled(true);
// Tell the list view to show one checked/activated item at a time.
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
// Start with first item activated.
// Make the newly clicked item the currently selected one.
getListView().setItemChecked(0, true);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// Make the newly clicked item the currently selected one.
getListView().setItemChecked(position, true);
}
private String[] mStrings = Cheeses.sCheeseStrings;
}
This is list activity it does't matter that you have list view or list activity.
I will sound stupid to most of you by asking this, but I haven't been able to figure it out on my own and can't pass to my next task until I found an answer.
So far I have downloaded the LazyList project from https://github.com/thest1/LazyList made by Fedor, I'm trying to understand how it works so I can implement it on my own project. My problem is that I don't know where to implement the onitemclicklistener part:
AdapterView.OnItemClickListener onitemClick = new AdapterView.OnItemClickListener()public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
noteId= //the item id from the list
Toast.makeText(getApplicationContext(), noteId, 2000).show();
}
I have try to add it on my mainActivity but then I can't find how to connect to my LazyAdapter to find the item id to be displayed in a toast. My project will contain some other information in the list (as a table several columns) so I want to be able to access an specific columns from that rows using the item id. and testing with a simple toast will help.
Thanks, I hope you don't laugh to much and help me a little.
I think you are going about it in the wrong manner: AdapterView.OnItemClickListener is an interface and the onItemClick() method takes four parameters... I assume this is what you are trying to do:
ListView listView = (ListView) findViewById(R.id.list);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), id + "", 2000).show();
}
});
I am using listview in which each item (of list view) has a checkbox and a textview.
when i am clivking on listview thw listener doesn't executes.
here is the code.
ListView lv = (ListView) findViewById(R.id.list);
final CustomListArrayAdaptor aa = new CustomListArrayAdaptor(this,data1);
lv.setAdapter(aa);
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View v,int position, long arg3)
{
TextView tv=(TextView)v.findViewById(R.id.text);
String s=tv.getText().toString();
Toast.makeText(getApplicationContext(), "Item Selected :"+s,Toast.LENGTH_LONG).show();
}
});
It doesnt show the toast "Item Selected" when clicked on any item.
This part of your code is correct. Upload the other files code also.
As much I know this could be the problem of focus.Add (android:focusable="false")
if you are defining the check box in xml file or for the java code use the method myCheckBox.setFocusable(false).
As explained here
Android custom ListView unable to click on items,
the click listener only works if no other view is focusable. Setting your CheckBox to focusable="false" should do the trick for you