display a dialog after long click on list view android

11,575

Use the setOnItemLongClickListener as following..

 selectRoom.setOnItemLongClickListener(new OnItemLongClickListener(){
        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                int position, long arg3) {
            // TODO Auto-generated method stub
            Dialog();
  or 
final MyDialog rDialog=new MyDialog(this,this);
            return false;
        }

    } );

public void Dialog()    {

        AlertDialog.Builder dialogAlert = new AlertDialog.Builder(getApplicationContext());
        dialogAlert.setTitle("Demo ?");

        dialogAlert.show();
}

 //Another way

 public class MyDialog extends Dialog {

Activity act;
public ReminderSettingDialog(Context context,Activity act) {
    super(context);
    this.act=act;
    // TODO Auto-generated constructor stub
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.settings);

}

}

Share:
11,575
yama
Author by

yama

Updated on June 04, 2022

Comments

  • yama
    yama almost 2 years

    in my application I will fetch data from a cursor and put it in a ListActivity.

    If a long click on a list item happens it should display a dialog.

    Now I'll show you some code:

    final ListView selectRoom = (ListView) findViewById(android.R.id.list);
            selectRoom.setOnItemLongClickListener(new OnItemLongClickListener() {
    
                public boolean onItemLongClick(AdapterView<?> adapter, View view,
                        int position, long id) {
    
                    Cursor unitCursor = (Cursor) getListView().getItemAtPosition(
                            position);
                    int rid = unitCursor.getInt(0);
                    DBAccessor dba = new DBAccessor(getApplicationContext());
                    dba.alterUnitName(rid);
                    callAdapter();
                    final Dialog d = new Dialog(getApplicationContext());
                    d.setContentView(R.layout.settings);
                    d.setTitle("My Dialog");
                    d.show();
    
                    return true;
    

    Here is the XML file with the List...

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <Spinner
            android:id="@+id/spinnerRooms"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </Spinner>
    
        <ListView
            android:id="@android:id/android:list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:layout_below="@id/spinnerRooms">
        </ListView>
    
    </RelativeLayout>
    

    the XML file with the columns of cursor is...

    <?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="match_parent"
        android:orientation="horizontal" >
    
        <TextView
            android:id="@+id/unitId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:visibility="invisible"/>
    
        <TextView
            android:id="@+id/unitName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
        <TextView
            android:id="@+id/unitRoomId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:visibility="invisible"/>
    
    </LinearLayout>
    

    Thank you for some answers and have a nice day.

  • Paresh Mayani
    Paresh Mayani almost 12 years
    He has already assigned setOnItemLongClickListener. Anything new in this answer?
  • yama
    yama almost 12 years
    the method onLongListItemClick() is not available
  • Munish Kapoor
    Munish Kapoor almost 12 years
    you can create MyDialog Class separately and use the MyDialog object in ListActivity