When I click on any list item nothing happens, can someone please help or tell me where the problem is...?
Here is my Listview:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".defineBybeldbAlles">
<ListView
android:id="#+id/BybelHoofstukListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#ff303030"
android:dividerHeight="1dp"
android:layout_marginTop="5dp"
android:choiceMode="singleChoice" />
</RelativeLayout>
Here is my Listview Item:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:focusable="false"
android:focusableInTouchMode="false"
android:descendantFocusability="blocksDescendants"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/customButtonLayout"
android:layout_height="50dp"
style="#android:style/Widget.Button"
android:focusable="false"
android:layout_width="200dp">
<! hoofstuk_id is hidden and is only for reference >
<TextView
android:text="id"
android:id="#+id/hoofstuk_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
>
</TextView>
<TextView
android:textColor="#000"
android:text="nommer"
android:layout_height="wrap_content"
android:id="#+id/custom_row_hoofstuktext"
android:layout_width="wrap_content"
android:layout_marginLeft="10dp">
</TextView>
</LinearLayout>
</LinearLayout>
Here is my OnItemclicklistener in the activity:
listviewHoofstuk.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick (AdapterView<?> arg0, View view, int arg2, long arg3){
//on selecting a hoofstk
//BybelActivityVers will be launched to show verse inside
Intent hoofstukIntent = new Intent(BybelActivityHoofstuk.this,BybelActivityVers.class);
//send hoofstuk_id to VersActivity to get verse under that book
String hoofstuk_id_na_vers =((TextView)view.findViewById(R.id.hoofstuk_id)).getText().toString();
hoofstukIntent.putExtra("hoofstuk_id", hoofstuk_id_na_vers);
startActivity(hoofstukIntent);
}
});
Ive looked at a few other solutions but nothing works in my case, I added this code but it makes no difference when I use it or not:
android:focusable="false"
android:focusableInTouchMode="false"
android:descendantFocusability="blocksDescendants"
Yout list item is taking focus. Set below line in LinearLayout Tag in my Listview Item xml file
android:clickable="false"
Your intent will fire without taking focus and work properly I ahve checked it... Let me know once its works for you...
Use AdapterView to get adpater item views here you should use parent to find TextView
listviewHoofstuk.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//on selecting a hoofstk
//BybelActivityVers will be launched to show verse inside
Intent hoofstukIntent = new Intent(BybelActivityHoofstuk.this,BybelActivityVers.class);
//send hoofstuk_id to VersActivity to get verse under that book
String hoofstuk_id_na_vers =((TextView)parent.findViewById(R.id.hoofstuk_id)).getText().toString();
hoofstukIntent.putExtra("hoofstuk_id", hoofstuk_id_na_vers);
startActivity(hoofstukIntent);
}
});
Here is your solution:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".defineBybeldbAlles">
<ListView
android:id="#+id/BybelHoofstukListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#ff303030"
android:dividerHeight="1dp"
android:layout_marginTop="5dp"/>
</RelativeLayout>
In your listview item you have to make this changes:
<LinearLayout
android:id="#+id/customButtonLayout"
android:layout_height="50dp"
style="#android:style/Widget.Button"
android:layout_width="200dp">
<! hoofstuk_id is hidden and is only for reference >
<TextView
android:text="id"
android:id="#+id/hoofstuk_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
>
</TextView>
<TextView
android:textColor="#000"
android:text="nommer"
android:layout_height="wrap_content"
android:id="#+id/custom_row_hoofstuktext"
android:layout_width="wrap_content"
android:layout_marginLeft="10dp">
</TextView>
</LinearLayout>
</LinearLayout>
This should solve your question if not just hit comment on this answer. After that you will start getting OnItemClick() event of listview.
I removed
android:focusable="false"
android:focusableInTouchMode="false"
android:descendantFocusability="blocksDescendants"
and then applied
android:clickable="false"
it did not work but after fiddling around I found that
android:clickable="false"
android:focusable="false"
in combination did the trick!
This was applied in the listview item just after LinearLayout.
I tested and can confirm it works with Gridview as well.
Related
I'm trying to add a ListView that has an ImageButton in it. Everything works except my onItemClick() isn't called. I've googled around and searched on StackOverflow to find suggestions of
android:descendantFocusability="blocksDescendants"
and
android:focusable="false"
I've tried both of these but neither work. This my layout file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="#dimen/listview_item_height"
android:descendantFocusability="blocksDescendants" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingStart="#dimen/activity_horizontal_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingEnd="#dimen/activity_horizontal_margin"
android:layout_gravity="center_vertical"
android:orientation="vertical" >
<TextView
android:id="#+id/song_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/listview_item_title_text_size"
android:textColor="#color/text_primary"
android:ellipsize="end"
android:singleLine="true"
android:lines="1" />
<TextView
android:id="#+id/song_artist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/listview_item_subtitle_text_size"
android:textColor="#color/text_secondary"
android:ellipsize="end"
android:singleLine="true"
android:lines="1" />
</LinearLayout>
<TextView
android:id="#+id/song_duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" />
<ImageButton
android:id="#+id/overflow_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="8dp"
android:background="?android:selectableItemBackground"
android:src="#drawable/ic_action_overflow"
android:scaleX="0.75"
android:scaleY="0.75" />
</LinearLayout>
Then my onItemClick() simply logs the id of the element clicked.
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
long id = adapter.getItemId(position);
Log.d(TAG, id + "");
}
I never get the output in the console though. I do get the selectable background when I click on the item though. I did not get that until adding "blocksDescendants" to the code. Any help is appreciated as I've spent a bit of time on this already.
You can use imagsView instead of imageButton and do onClick actions when list item is clicked
Or
You set onClickListener after layout is inflated in view adapter.
Read this: http://androidforbeginners.blogspot.it/2010/03/clicking-buttons-in-listview-row.html?m=1
Also this should be set like this to let system listen for clicks:
ListView focusable
android:focusable="true"
Button not focusable
android:focusable="false"
Or
You can use this in xml. On click, it will look for your method in same activity. Doesn't work for fragments.
android:onClick="myMethod"
Have you tried using an ImageView instead? That way by default it won't intercept your clicks.
Or you could try playing with onInterceptTouchEvent.
http://developer.android.com/training/gestures/viewgroup.html
this app is a simple phonebook ,this is the xml file for contact
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imgAvatar"
android:layout_width="70dip"
android:layout_height="70dip"
android:scaleType="fitCenter"
android:layout_toLeftOf="#+id/tvName"
android:layout_toStartOf="#+id/tvName" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/linearLayout">
</LinearLayout>
<TextView
android:id="#+id/tvName"
android:layout_width="276dp"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_gravity="right"
android:layout_below="#+id/linearLayout"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textAlignment="center" />
<TextView
android:id="#+id/tvPhone"
android:layout_width="276dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_below="#+id/tvName"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textAlignment="center" />
</RelativeLayout>
and contacts are contained in this listView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MyActivity">
<ListView
android:id="#+id/listPhone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
how can i pick up the value of the textfield called tvPhone when i click on his contact on the ListView ?
If you just want to get the information for the TextView, you do as this :
String tvValue = tv.getText().toString();
If you loading the values from an adapter, and if you are storing the values in an array list, you will have something like this in the adapter side in the getView() method:
yourTextView.setText(mDetailsArrayList.get(position));
And to get the value of that, you will again get the value using:
getText().toString()
Depending on what's your values looks like, you may have values for int, float etc.
In this example, its a String value you are getting back.
Now, if you have a ListView and a textView on the same layout, you will simple get the value from the textView using the above method and add it to you arrayList for example and to Update the listView you will just notify the changes by using notifyDataSetChanged() method.
EDIT:
For an onClick event on a ListView, just use onItemClickListener as this :
yourListView.setOnItemClickListener(listItemClickListener);
public OnItemClickListener listItemClickListener = new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String getText = yourArrayList.get(position).getTextFromTextViewMethod();
}
};
Good luck.. :)
I have an activity that extends ListActivity with an ArrayAdapter:
ArrayAdapter<String> fileList = new ArrayAdapter<String>(this, R.layout.row, R.id.text, items);
setListAdapter(fileList);
It uses a custom layout for a row:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:id="#+id/checkbox"
style="#style/CheckBox" />
<TextView
android:id="#+id/text"
style="#style/Label.Plain"
android:layout_width="fill_parent"
android:layout_height="50sp"
android:gravity="center_vertical" />
</LinearLayout>
And there is onListItemClick is the activity:
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// ...
}
It worked fine when my row consisted of a single TextView. But when I changed it to a layout, onListItemClick ceased being called. Why?
The focus goes to the CheckBox and that way your item do not get the click event. Try modifying your XML like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:id="#+id/checkbox"
style="#style/CheckBox"
android:focusable="false"
android:focusableInTouchMode="false" />
<TextView
android:id="#+id/text"
style="#style/Label.Plain"
android:layout_width="fill_parent"
android:layout_height="50sp"
android:gravity="center_vertical" />
</LinearLayout>
Set the bellow properties to the checkbox and textview
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
When you have a widget like checkbox button radiobutton, then you have to add this propery in your listview element in the xml :-
android:focusableInTouchMode="true"
Now your click listener will work and also your checkbox listener if you implement it.
yes it is common question always asked,it happens because child controls of custom layout gets the click before list,so list doesn't responds on click. add android:focusable=false; this will allow list to listen click.
<CheckBox
android:id="#+id/checkbox"
android:focusable="false"
style="#style/CheckBox" />
<TextView
android:id="#+id/text"
style="#style/Label.Plain"
android:focusable="false"
android:layout_width="fill_parent"
android:layout_height="50sp"
android:gravity="center_vertical" />
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:orientation="horizontal"
android:layout_width="fill_parent"
android:clickable="false"
android:focusable="false">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:clickable="false"
android:focusable="false"
android:orientation="vertical" >
<TextView
android:id="#+id/MXname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:clickable="false"
android:focusable="false"
android:gravity="left"
android:text="Slashdot"
android:textStyle="bold" />
<TextView
android:id="#+id/MXtnumber"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:clickable="false"
android:focusable="false"
android:gravity="left"
android:text="10 minutes ago" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="false"
android:focusable="false"
android:orientation="vertical"
android:gravity="right"
android:descendantFocusability="blocksDescendants">
<ImageView
android:id="#+id/MXdialer"
android:layout_width="35dp"
android:layout_height="fill_parent"
android:layout_weight="2"
android:clickable="true"
android:focusable="true"
android:gravity="right"
android:src="#drawable/lcall_button" />
</LinearLayout>
</LinearLayout>
I have a listview with the ROW xml listed above and below is the code from activity:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position , long id)
{
Log.d("ItemClick","in the list"+ String.valueOf(view.getId()));
if (view==findViewById(R.id.MXdialer))
{
Toast.makeText(this, "Name and/or Phone number empty"+String.valueOf(id), Toast.LENGTH_LONG).show();
}
}
the Logcat does indicate the onitemClick being execuated but toast never works and view.getid() always returns -1. I have read the articles on onitemclicklisterner and have already made all the views as not focusable and not clickable but the still no go. Please suggest
view is the root layout of the clicked list item not the any child in the item layout. So the Toast will never show.
If you want click on any specific child view, set the onClickListener to that child, but then the OnItemClickListener will not be called. Either of the two will get called. The child views clikclistener takes priority.
for your if statement try this as not sure you can compare objects like the way your doing it.
int viewID=view.getId();
if(viewID== R.id.MXdialer)
Toast.makeText(this, "Name and/or Phone number empty"+String.valueOf(id), Toast.LENGTH_LONG).show();
I try to set the focus on ListView but onItemSelected - not work, here is my source
uAdapter = new UrlArrayAdapter(DatabaseTable.this, urlListView,
urlLists);
urlListView.setAdapter(uAdapter);
urlListView.setItemsCanFocus(true);
urlListView.setClickable(false);
urlListView.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1, int pos,
long id) { });
I use the following XML. My LisWiev xml
<ListView
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:descendantFocusability="beforeDescendants" >
</ListView>
and an item xml which add to the ListView
<LinearLayout
android:id="#+id/editLinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minHeight="50dp" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.66"
android:ems="10" >
<requestFocus />
</EditText>
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false" />
</LinearLayout>
I solved the problem, helped me this article, it is shown how to use the ListView
[Linc] (http://www.lalit3686.blogspot.in/2012/06/today-i-am-going-to-show-how-to-deal.html)
and Helped me to this link
http://vikaskanani.wordpress.com/2011/07/27/android-focusable-edittext-inside-listview/
Your onItemSelected is not being called, since you have set the setClickable attibute of your list to false. Try changing it to true and then check out. I hope that will work.