Amir Raminfar Amir Raminfar - 2 months ago 10
Android Question

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>

Answer

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

Comments