Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AlertDialog Buttons not visible when using Custom layout

Tags:

android

I wanted a custom layout inside the pop up, so I used Alert Dialog. As the number of subviews in the popup is dynamically decided so I used ScrollView. Now the twist comes here, when there is no space left and scroll gets in action, the positive and negative buttons gets invisible. Below is the code:

AlertDialog.Builder dialog;
dialog = new AlertDialog.Builder(this);

dialog.setMessage("Please enter below parameters");

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.WRAP_CONTENT
);

LinearLayout rootLayout = new LinearLayout(this);
rootLayout.setOrientation(LinearLayout.VERTICAL);

ScrollView scrollView = new ScrollView(this);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setPadding(50, 0, 50, 0);


try {
                JSONArray data = new JSONArray(parameters);
                String toastDaata = "";
                for (int i = 0; i < data.length(); i++) {
                    JSONObject parameter = data.getJSONObject(i);
                    String sources = parameter.getString("sources");
                        String name = parameter.getString("name");
                        String mandatory = parameter.getString("mandatory");
                        toastDaata = toastDaata + "\n" + name + ":" + mandatory;

                        EditText editText$name = new EditText(this);
                        editText$name.setHint(name);
                        editText$name.setLayoutParams(params);
                        TextInputLayout textInputLayout = new TextInputLayout(this);
                        textInputLayout.setLayoutParams(params);
                        textInputLayout.addView(editText$name, params);
                        layout.addView(textInputLayout);

                                    }
                flowDescription.setText(toastDaata);
                Toast.makeText(this, toastDaata, Toast.LENGTH_SHORT).show();
            } catch (JSONException e) {
                e.printStackTrace();
            }

scrollView.addView(layout);
            rootLayout.addView(scrollView);


            dialog.setView(rootLayout);
            dialog.setNegativeButton("Cancel", null);
            dialog.setPositiveButton("Go", null);

            dialog.show();

enter image description here enter image description here

As per image when scroll needed it hides the buttons.

like image 210
Pawan Dubey Avatar asked Oct 28 '22 20:10

Pawan Dubey


1 Answers

Try this .. It's working for me...

AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this); 
        LinearLayout rootLayout = new LinearLayout(this);
        rootLayout.setOrientation(LinearLayout.VERTICAL);
        ScrollView scrollView = new ScrollView(MainActivity.this);
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        for (int i = 0; i < 20; i++) {
            EditText editText$name = new EditText(this);
            editText$name.setHint("Test");
            TextInputLayout textInputLayout = new TextInputLayout(this);
            textInputLayout.addView(editText$name);
            layout.addView(textInputLayout);
        }
        scrollView.addView(layout);
        rootLayout.addView(scrollView);
        alertDialog.setNegativeButton("Cancel", null);
        alertDialog.setPositiveButton("Go", null);
        alertDialog.setView(rootLayout);
        alertDialog.show();

Same Output As you excepted..

enter image description here

like image 161
Vikas singh Avatar answered Nov 15 '22 05:11

Vikas singh