4

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>

2 Answers 2

15

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

1
  • In my case, the list item had a check box. Context menu did not work initially but it worked once I set focusable to false for the check box.
    – mvsagar
    Aug 4, 2014 at 14:23
0

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?

2
  • Hi Swathi - I moved registerForContextMenu right after findViewById() and I still don't see anything happening. What I meant before was by using the scroll wheel I am not able to get the context menu opened. But by using the mouse I can click and hold to open. This is the sample I have used github.com/mikeplate/ListViewDemo Nov 15, 2010 at 16:58
  • 2
    Hi Amir, As i know, context menu will be displayed when we perform long press on a view.This link may help you- developer.android.com/guide/topics/ui/menus.html
    – Swathi EP
    Nov 16, 2010 at 5:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.