0

I need an alertdialog with a custom view in title. Since I was not able to inflate a view as title I used this link. I was able to make custom title and was able to inflate list items. But on list item click the alertdialog is not closing. How to implement this?

    package com.qustom.dialog;

        import android.app.AlertDialog;
        import android.content.Context;
        import android.content.DialogInterface;
        import android.content.DialogInterface.OnClickListener;
        import android.graphics.Color;
        import android.graphics.drawable.Drawable;
        import android.view.View;
        import android.widget.AdapterView;
        import android.widget.FrameLayout;
        import android.widget.ImageView;
        import android.widget.ListAdapter;
        import android.widget.ListView;
        import android.widget.TextView;

        public class QustomDialogBuilder extends AlertDialog.Builder {

            private View mDialogView;

            private TextView mTitle;

            private ImageView mIcon;

            private TextView mMessage;

            boolean firstTime = true;

            private View mDivider;

            private Context mContext;

            public QustomDialogBuilder(Context context) {
                super(context);

                mDialogView = View
                        .inflate(context, R.layout.qustom_dialog_layout, null);
                setView(mDialogView);

                mTitle = (TextView) mDialogView.findViewById(R.id.alertTitle);
                mMessage = (TextView) mDialogView.findViewById(R.id.message);
                mIcon = (ImageView) mDialogView.findViewById(R.id.icon);
                mDivider = mDialogView.findViewById(R.id.titleDivider);
                mContext = context;
            }

//This is where I add listview
            public QustomDialogBuilder setCustomAdapter(ListAdapter adapter, final OnClickListener listener) {
                ListView listView = new ListView(mContext);
                listView.setAdapter(adapter);
                ((FrameLayout) mDialogView.findViewById(R.id.customPanel))
                        .addView(listView);

                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                            long arg3) {
                        QustomDialogBuilder dialog = QustomDialogBuilder.this;
                        if (dialog != null) {
                            Log.d("skt", "dialog in custom not null");//This log is working
                        }
                        listener.onClick( null, arg2);//Here I want the DialogInterface Object. this is where I am stuck

                    }
                });
                return this;
            }

            public QustomDialogBuilder setDividerColor(String colorString) {
                mDivider.setBackgroundColor(Color.parseColor(colorString));
                return this;
            }

            @Override
            public QustomDialogBuilder setTitle(CharSequence text) {
                mTitle.setText(text);
                return this;
            }

            public QustomDialogBuilder setTitleColor(String colorString) {
                mTitle.setTextColor(Color.parseColor(colorString));
                return this;
            }

            @Override
            public QustomDialogBuilder setMessage(int textResId) {
                mMessage.setText(textResId);
                return this;
            }

            @Override
            public QustomDialogBuilder setMessage(CharSequence text) {
                mMessage.setText(text);
                return this;
            }

            @Override
            public QustomDialogBuilder setIcon(int drawableResId) {
                mIcon.setImageResource(drawableResId);
                return this;
            }

            @Override
            public QustomDialogBuilder setIcon(Drawable icon) {
                mIcon.setImageDrawable(icon);
                return this;
            }


            public QustomDialogBuilder setCustomView(int resId, Context context) {
                View customView = View.inflate(context, resId, null);
                ((FrameLayout) mDialogView.findViewById(R.id.customPanel))
                        .addView(customView);
                return this;
            }


            @Override
            public AlertDialog show() {
                if (mTitle.getText().equals(""))
                    mDialogView.findViewById(R.id.topPanel).setVisibility(View.GONE);
                return super.show();
            }
        }

Can anyone help me?

I know I can set a customview as title by setCustomTitle(view) method. But my problem is this

Community
  • 1
  • 1
SKT
  • 1,821
  • 1
  • 20
  • 32

1 Answers1

2

If you check android document there is no dismiss or cancel method in alertdialog.builder class so, if you want to dismiss you need to get object of alertdialog when you override show method of alertdialog.builder. After this you have to call alertdilogObject.dismiss() method in your list item click. I hope this answer will helpful you.

Meenal
  • 2,879
  • 5
  • 19
  • 43
Narayan Soni
  • 114
  • 8
  • I have used that and it always is null. In my code I am passing null in the listener. I know that is the reason of it. But still I am not finding any values to pass other than null. – SKT Aug 26 '13 at 12:33