2

Is it possible to set variable number of textedits in alertdialog? I've tried to fill some container views such as StackView or LinearLayout dynamically but method addView is said to be not supported in AdapterView(exception). What's the solution?
Added:
I want to build alertdialog from the dynamic information.

AlertDialog.Builder alert = new AlertDialog.Builder(context);

now I can set its view like this:

alert.setView(v);

but v element can only be something simple as TextView or EditText. What if I want to create some container view which may contain variable number of elements, for example 2 textviews and 3 edittexts? How can I do this? Now I just create separate layout file and inflate view with it bit it's not a solution. What can I do?

Sergey
  • 11,548
  • 24
  • 76
  • 113
  • Hi.. i dont quite understand your question could u elaborate more? wad exactly are u trying to do? this question looks familiar though.. http://stackoverflow.com/questions/4576219/logcat-error-addviewview-layoutparams-is-not-supported-in-adapterview-in-a – Rejinderi Oct 09 '11 at 14:29
  • Hm, adding a LinearLayout to AlertDialog.Builder#setView(View) works just fine - and adding/subtracting child Views from that layout programatically will work just fine. – Jens Oct 12 '11 at 08:37

5 Answers5

0

Why would you need a variable number of TextViews? You could use a single one to display multiple lines. If you need something more complicated, you could create your own dialog-like activity with theme @android:style/Theme.Dialog, constraining its dimensions so that it does not cover the entire display area.

Update:

Here is an example of how to do a dialog-like subactivity:

:: ComplexDialog.java

public class ComplexDialog extends Activity {
    ...regular Activity stuff...

    protected void onCreate(Bundle savedInstanceState) {
        ...get extras from the intent, set up the layout, handle input, etc...

        LinearLayout dialogLayout = (LinearLayout) findViewById(R.id.dialogLayout);
        Display display = getWindowManager().getDefaultDisplay();
        int width = display.getWidth() > 640 ? 640 : (int) (display.getWidth() * 0.80);
        dialogLayout.setMinimumWidth(width);
        dialogLayout.invalidate();

        ...more regular stuff...
};

:: AndroidManifest.xml

<activity android:name=".ComplexDialog" android:theme="@android:style/Theme.Dialog"></activity>
K-ballo
  • 80,396
  • 20
  • 159
  • 169
  • No, I don't just need variable number of textviews, I need multiple number of pairs - (TextView + TextEdit) where text in textview gives an overview of what user should type. The problem is that every time I need to build different number of textedits. – Sergey Oct 12 '11 at 07:00
  • @Sergey: I would recommend using the `EditText` hint property for the overview of what the user should type. Make yourself a dialog-like activity that takes a list of strings with the edittexts and hints to show, and have them be returned by the activity `setResult`. – K-ballo Oct 12 '11 at 07:04
  • anyway, even if I would use only EditText elements without textviews, I also face the problem of their variable number – Sergey Oct 12 '11 at 07:06
  • @Sergey: Indeed, that's why I recomend a dialog-like subactivity. `AlertDialog` is just for showing alerts, and simple stuff. If you want to do complicate stuff in the form of a dialog, just use an activity and make it look like a dialog. – K-ballo Oct 12 '11 at 07:10
0

Is the number of EditTexts changing while the user is viewing the dialog or is the number fix by then? If it is fixed and only varies from time to time, you could build an own layout xml for each of them, and then decide with a switch statement which layout xml you want to display in the dialog.

A custom alert dialog can be displayed with this code:

// This example shows how to add a custom layout to an AlertDialog
            LayoutInflater factory = LayoutInflater.from(this);
            final View textEntryView = factory.inflate(
                    R.layout.helpdialog_main, null);
            return new AlertDialog.Builder(Main.this)
                    .setView(textEntryView)
                    .setPositiveButton(R.string.stringHelptextButtonOK,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int whichButton) {

                                    // Popup is closed automatically, nothing
                                    // needs to be done here
                                }
                            }).create();
banzai86
  • 727
  • 1
  • 5
  • 13
  • number of edittexts is fix by the moment when user is viewing it. What you suggest is how I've already done it but the question is about other ways of doing it because this way is not optimal - creating manu layouts. – Sergey Oct 13 '11 at 06:27
  • that's true, but I think it's better than adding views programmatically to a basic-version of an alert dialog – banzai86 Oct 13 '11 at 07:14
0

Adding a LinearLayout should work just fine:

In #onCreate(Bundle):

...
...
/*LinearLayout*/ mDlgLayout = new LinearLayout(this);
mDlgLayout.setOrientation(LinearLayout.VERTICAL);

AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Some title");
alert.setView(mDlgLayout);
alert.setNeutralButton("Regenerate", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dlg, int which) {
            // onClick will dismiss the dialog, just posting delayed
            // to pop up the dialog again & change the layout.
            mDlgLayout.postDelayed(new Runnable() {
                    public void run() {
                        alterDlgLayout();
                    }
                }, 200L);
        }
    });
/*AlertDialog*/ mDlg = alert.create();
...
...

In #alterDlgLayout()

void alterDlgLayout() {
    mDlgLayout.removeAllViews();
    Random rnd = new Random(System.currentTimeMillis());
    int n = rnd.nextInt(3) + 1;
    for (int i = 0; i < n; i++) {
        EditText txt = new EditText(this);
        txt.setHint("Some hint" + rnd.nextInt(100));
        mDlgLayout.addView(txt);
    }
    mDlgLayout.invalidate();
    mDlg.show();
}

In #onResume()

alterDlgLayout();

In #onPause()

mDlg.dismiss();
Jens
  • 16,853
  • 4
  • 55
  • 52
0

here is the link below is for static layout of dialogbox

http://knol.google.com/k/thiyagaraaj-m-p/custom-dialog-box-popup-using-layout-in/1lfp8o9xxpx13/171#

if you want to add dynamic layout or edit the existing layout then you can get that by method

RelativeLayout myMainLayout= (RelativeLayout)myDialog.findViewById(R.id.myMainLayout);

and add view of your choice to your main layout by creating them in java and using addView() method..

myMainLayout.addView(yourChildView);
Umar Qureshi
  • 5,985
  • 2
  • 30
  • 40
0

The easiest way is to inflate views dinamically, In your dialog creation method put this code to build Dialog:

AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Some title");
ViewGroup final mainLayout = getLayoutInflater().inflate(R.layout.the_custom_holder);
final EditText[] editors = new EditText[requiredItemCount];
for (int i = 0; i < requiredItemCount; i++) {
   View inputter = getLayoutInflater().inflate(R.layout.the_custom_line);
   editors[i] = inputter.findViewById(R.id.editorId);
}

alert.setPositiveButton(R.string.stringHelptextButtonOK,
   new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int whichButton) {
      // Accessing one of the edittexts
      String requiredText = editors[3].getText().toString();
      // TODO Do stuff with result
   }
alert.setView(mDlgLayout);
alert.create().show();
Andras Balázs Lajtha
  • 2,576
  • 25
  • 32