onItemClickListener and view not clickable - android

<?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();

Related

Click item in listview does not work

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.

Android ImageButton in ListView blocking onItemClick()

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

How should inflating go right way?

I need to have like several listviews on a scrollview which Android doesn't allow me to do so I decided to make it manually to simply inflate some listrows on a vertical LinearLayout, but everything goes not the right way
private void fillData(final ViewGroup parent, final ArrayList<Person> array ){
for (int i=0;i<array.size();i++){
final View view = lf.inflate(R.layout.personal_listrow, parent,true);
parent.setBackgroundColor(getActivity().getResources().getColor(R.color.background_light_darker));
view.setBackgroundColor(getActivity().getResources().getColor(R.color.background_night_lighter));
TextView tvName=(TextView) view.findViewById(R.id.tvName);
TextView tvStatus=(TextView) view.findViewById(R.id.tvStatus);
TextView tvGender=(TextView) view.findViewById(R.id.tvGender);
//fill textviews with info, set the onClickListeners and so on
}
So what I get - the view.setBackgroundColor paints parent as well .
parent is a vertical LinearLayout, view is horizontal LinearLayout - it's strange that I don't paint inflated element only so please explain me why
The next thing - after I add something to array - 1st element changes and all the rest is with unchanged text. However, I walked through the code with debugger - it passes every element and sets text.
The clicklisteners works on the 1st element only..
Would you plz explain
here's vertical , parent layout - nothing special:
<LinearLayout
android:id="#+id/rodll"
android:layout_marginTop="16dp"
android:layout_marginBottom="8dp"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
the inflateable listrow is nothing special also :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/tvStatus"
android:layout_width="0dp"
android:layout_weight=".3"
android:maxLines="1"
android:layout_height="wrap_content"
android:text="tvSt" />
<TextView
android:id="#+id/tvName"
android:layout_weight="1"
android:layout_width="0dp"
android:maxLines="1"
android:layout_height="wrap_content"
android:text="tvName" />
<TextView
android:id="#+id/tvGender"
android:layout_weight=".1"
android:layout_width="0dp"
android:maxLines="1"
android:layout_height="wrap_content"
android:text="tvGender" />
<com.andexert.library.RippleView
android:id="#+id/removeRipple"
xmlns:ripple="http://schemas.android.com/apk/res-auto"
android:layout_marginLeft="30dp"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginRight="7.5dip"
android:padding="3dp"
ripple:rv_alpha="255"
ripple:rv_zoom="true"
ripple:rv_zoomScale="0.8"
ripple:rv_type="rectangle">
<ImageView
android:id="#+id/remove_icon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/remove" />
</com.andexert.library.RippleView>
<com.andexert.library.RippleView
android:id="#+id/addRipple"
xmlns:ripple="http://schemas.android.com/apk/res-auto"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginRight="7.5dip"
android:padding="3dp"
ripple:rv_alpha="255"
ripple:rv_zoom="true"
ripple:rv_zoomScale="0.8"
ripple:rv_type="rectangle">
<ImageView
android:id="#+id/add_icon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/add" />
</com.andexert.library.RippleView>
</LinearLayout>
So. what's the problem and how to solve it?

How to pick a value from a TextView into a ListView

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.. :)

registerForContextMenu not get calling properly - what am I doing wrong?

The answer to this question might be really obvious but its giving me a headache. I have a simple LinearLayout with one single ListView in it. I do this: onCreate
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.friends);
ListView listView = (ListView) findViewById(R.id.friend_list);
listAdapter = new CheckinListAdapter(checkins, listView, R.layout.checkin_list_item);
listView.setAdapter(listAdapter);
if (getLastNonConfigurationInstance() != null) {
FriendsActivity last = (FriendsActivity) getLastNonConfigurationInstance();
this.checkins.addAll(last.checkins);
this.sort = last.sort;
} else {
refresh();
}
registerForContextMenu(listView);
}
But for some reason onCreateContextMenu never gets called! So I did some research and since I am loading the list after the register, perhaps it doesn't register it correctly. If I go in my ListAdapter and do registerForContextMenu it does show up. But it doesn't behave correctly with the keyboard. So I am now confused on what can be the error because it seems a little non intuitive for me to have to register each child item. All the examples I find online are using ArrayAdapter. :(
Any suggestions?
Edit
Here is more detail, in case it something I don't see:
My XML file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:text="#string/check_in"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onCheckInClicked"/>
<ListView android:id="#+id/friend_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
List item xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="5dip"
android:paddingBottom="5dip">
<ImageView android:id="#+id/user_photo"
android:layout_width="40dip"
android:layout_height="40dip"
android:scaleType="centerCrop"/>
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="8dip">
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
<Button android:id="#+id/user" style="#style/TextButton"/>
<TextView android:text="#string/at"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button android:id="#+id/venue"
android:singleLine="true"
android:ellipsize="end"
style="#style/TextButton"/>
</LinearLayout>
<TextView android:id="#+id/venue_address" style="#style/GreyLarge"/>
<LinearLayout android:id="#+id/checkin_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dip"
android:layout_marginBottom="6dip">
<ImageView android:id="#+id/checkin_image"
android:layout_width="70dip"
android:layout_height="60dip"
android:layout_marginRight="8dip"
android:scaleType="centerCrop"/>
<TextView android:id="#+id/checkin_shout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<TextView android:id="#+id/elapsedTime" style="#style/GreySmall"/>
</LinearLayout>
</LinearLayout>
This took me 6 hours to figure out but it turns out I had to add:
android:focusable="false"
to all my <Button/> tags.
Related post: TextView and Button in each row and onListItemClick()
Do, registerForContextMenu(listView);
before setting the adapter, that is before:
listView.setAdapter(listAdapter);
You have mentioned that the menu is not responding correctly with the keyboard actions. Can you tell me what exactly the problem is?

Categories

Resources