简体   繁体   中英

Custom alert dialog in android overwrites other object

I am trying to create custom alert dialog with two field one is edit-text and other is spinner. Problem is i can only see spinner, i am not able to see edit-text. Edit-text is available but spinner hides the content of edit-text. So my question is how can i set the position of edit-text and Spinner.

Below is my code.

AlertDialog.Builder alertDialog = new AlertDialog.Builder(
            RecordAudio.this);
    alertDialog.setTitle("Would you Like to save your Recording");
    alertDialog.setMessage("Enter Audio Name");
    alertDialog.setIcon(R.drawable.save_icon);

    final EditText editTextAudioName = new EditText(RecordAudio.this);
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    editTextAudioName.setLayoutParams(lp);
    alertDialog.setView(editTextAudioName);

    final Spinner spinnerListData = new Spinner(RecordAudio.this);
    RelativeLayout.LayoutParams lpSpinner = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    spinnerListData.setLayoutParams(lpSpinner);
    alertDialog.setView(spinnerListData);

    dataManipulatorClass = new DataManipulatorClass(this);
    names2 = dataManipulatorClass.selectAll();

    stg1 = new String[names2.size()];
    int x = 0;
    String stg;

    for (String[] name : names2) {
        stg = "Class Name : " + name[1];
        stg1[x] = stg;
        x++;
    }

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(
            RecordAudio.this, android.R.layout.simple_spinner_item, stg1);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerListData.setAdapter(adapter);
    spinnerListData.setWillNotDraw(false);

    spinnerListData.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View v,
                int position, long id) {
            Toast.makeText(getApplicationContext(),
                    "Spinner Item selected", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
        }
    });

    alertDialog.setPositiveButton("Save",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                    Code.audioName = editTextAudioName.getText().toString()
                            .trim();

                    recorder = new MediaRecorder();
                    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                    recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
                    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                    recorder.setOutputFile(getFilename());
                    recorder.setOnErrorListener(errorListener);
                    recorder.setOnInfoListener(infoListener);

                    try {
                        recorder.prepare();
                        recorder.start();

                        if (Code.i == true) {
                            new Timer().schedule(new TimerTask() {
                                @Override
                                public void run() {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            stopRecording();
                                            enableButtons(false);
                                        }
                                    });
                                }
                            }, 41000);
                        } else {
                            new Timer().schedule(new TimerTask() {
                                @Override
                                public void run() {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            stopRecording();
                                            enableButtons(false);
                                        }
                                    });
                                }
                            }, 21000);
                        }
                    } catch (IllegalStateException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    myChronometer.setBase(SystemClock.elapsedRealtime());
                    myChronometer.start();
                }
            });

    alertDialog.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                    enableButtons(false);
                }
            });
    alertDialog.show();

setView Method:-

Set a custom view to be the contents of the Dialog. If the supplied view is an instance of a ListView the light background will be used.

so you need to add both view in some layout for example LinearLayout and then call setView method for

It is because you are setting views to alert dialog two times. First define one Linearlayout and then add your views (Edit text and Spinner) into it and then set that Linearlayout to Alert Dialog.

LinearLayout parentLayout = new LinearLayout( RecordAudio.this);

LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

final EditText editTextAudioName = new EditText(RecordAudio.this);

 RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); editTextAudioName.setLayoutParams(lp); parentLayout.addView(editTextAudioName); final Spinner spinnerListData = new Spinner(RecordAudio.this); RelativeLayout.LayoutParams lpSpinner = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); spinnerListData.setLayoutParams(lpSpinner); parentLayout.addView(spinnerListData); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM