Search code examples
androidlayout-inflatercustomdialog

inflating layout item


i'm sorry if this question is a repeat. I have a problem:

I have one main_activity with main_layout.xml. I have a TextView and SeekBar1 in it. I added a menu with a custom_dialog_layout which has a seekbar2. Dialog shows that custom_dialog with seek bar.

when doing this:

inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.resize_dialog,null);
seekbar = (SeekBar)findViewById(R.id.seekBar2);

application force closes.


Solution

  • Try this.

     inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     view = inflater.inflate(R.layout.resize_dialog,null); 
     seekbar = (SeekBar)view. findViewById(R.id.seekBar2); 
    

    /** Add that inflated view to AlertDialog */

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.app_name);
        builder.setView(view);
    

    use this for cancelling Dialog. i.e dialog.dismiss();

    .setPositiveButton(
                    getResources().getString(R.string.Cancel),
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int whichButton) {
                            dialog.dismiss();
                        }
                    });
            alert = builder.create();
            alert.show();