How to show a Progress Bar inside an AlertDialog B

2019-07-04 02:21发布

问题:

I am trying to create an AlertDialog for a Bluetooth transfer after the transfer notification is touched in an android phone.

I am trying something like this: Out of the below, I am getting everything - icon, title and two buttons. I am sure I can add other info like From, FileName and others using cursors with the help AlertDialog.Builder properties. I just do not know to get a progress-bar in it. I do not want to use an XML.

protected Dialog onCreateDialog(int id) {
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setIcon(R.id.imageFile);
    alertDialogBuilder.setTitle("File Transfer");

    ProgressDialog progressDialog;
    Context mContext = null;
    progressDialog = new ProgressDialog(mContext);
    progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

    //alertDialogBuilder.setView(progressDialog); //STUCK HERE
    //"!" icon and "File Transfer"
    //from : device name
    //File: <name>
    //Type: <type> (<size>)
    //Receiving/Sending File
    //<%> | Green progress bar
    //Hide and Stop buttons

    alertDialogBuilder.setPositiveButton("STOP", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
        }
    });
    alertDialogBuilder.setNegativeButton("HIDE", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
        }
    });
    AlertDialog alertDialog = alertDialogBuilder.create();
    return alertDialog;

}

I have everything in place except I am not able to figure out how to bring a progress-bar here.

Can I getjust a progress-bar using XML layout and use as

alertDialogBuilder.setView(R.id.what-ever-xml-file) Or how to create a view for that progress-bar in he Java file itself and put a progressbar in that view and then put that view inside the dialog.

I want sth like this:

回答1:

You can use Dialog and add ProgressBar on this by using

_dialog.addContentView(view, params) method.