Reputation: 324
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();
As per image when scroll needed it hides the buttons.
Upvotes: 6
Views: 2588
Reputation: 3879
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..
Upvotes: 3
Reputation: 64
use Dialog instead of Alert Dialog
Dialog dialog = new Dialog(mContext);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.setContentView(R.layout.activity_pop_up_payemnt);
DisplayMetrics dm=new DisplayMetrics();
mContext.getResources().getDisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
int width=dm.widthPixels;
int height=dm.heightPixels;
dialog.getWindow().setLayout((int)(width*.8),(int)(height*.7));
dialog.setCanceledOnTouchOutside(true);
WindowManager.LayoutParams wmlp = dialog.getWindow().getAttributes();
wmlp.gravity = Gravity.CENTER;
dialog.show();
Upvotes: 0
Reputation: 917
Try to give some padding for your inner linear layout from bottom.
layout.setPadding(50, 0, 50, 20);
Or Try this
scrollView.setFillViewport(true);
Hope this will help you.
Upvotes: 0