user1302569
user1302569

Reputation: 7191

Dismiss dialog in android

I have a imageView in onCreate method where I am using method showList. This is code:

action_bar_log_in_icon.setOnClickListener(new OnClickListener() {

                            @Override
                            public void onClick(View v) {

                                allResults.clear();
                                allResults.add(action_bar_log_in_icon);
                                allResults.add(action_bar_data_privacy_icon);
                                allResults.add(action_bar_vCard_icon);
                                allResults.add(action_bar_email_icon);
                                showList();
                            }
                        });

and code method showList:

private void showList() {
            final ListDialog listDialog = new ListDialog(this);
            listDialog.setCanceledOnTouchOutside(true);

            final ListView lv = (ListView) listDialog
                    .findViewById(R.id.spinnerlist);

            if(lAdapter == null){
                lAdapter = new ListAdapter(getBaseContext(),
                    R.layout.spinner_item,
                    (ArrayList<ImageView>) allResults);
            }

            lv.setAdapter(lAdapter);
            lv.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    switch(position){
                    case 0:
                        intent = new Intent(ActionBarMain2.this, LoginActivity.class);
                        startActivity(intent);
                        break;
                    case 1:
                        intent = new Intent(ActionBarMain2.this, LoginActivity.class);
                        startActivity(intent);
                        break;
                    case 2:
                        intent = new Intent(ActionBarMain2.this, vCard_view.class);
                        startActivity(intent);
                        break;
                    case 3:
                        intent = new Intent(ActionBarMain2.this, EmailView.class);
                        startActivity(intent);
                        break;
                    }
                    listDialog.dismiss();
                }

            });
            WindowManager.LayoutParams WMLP = listDialog.getWindow()
                    .getAttributes();
            WMLP.gravity = Gravity.TOP | Gravity.RIGHT;
            WMLP.y = 50;
            WMLP.width = 100;
            listDialog.getWindow().setAttributes(WMLP);

                listDialog.show();



        }

This solution work and if I click on imageview my list is showing, but problem is when I click again on the same imageview. List is still visible. How I can do when I click first time my list is showing and when I click again my list dismiss etc?

Upvotes: 1

Views: 243

Answers (3)

user1302569
user1302569

Reputation: 7191

I find solution my problem. This dialog cover my imageView, so I placed dialog a little lower. Thank you for advices.

Upvotes: 0

mainu
mainu

Reputation: 448

set some flag for capturing the state of the dialog. Then set condition on that flag.. If its visible then set dismissDialog(dialogName);

Upvotes: 1

Alexander
Alexander

Reputation: 48232

What if you create your ListDialog just once in your Activity's onCreate not showing it yet.Then upon clicking your ImageView you can do if(dialog.isVisible()) { dialog.show(); } else { dialog.dismiss(); }

Upvotes: 1

Related Questions