i have just started android programming, wrote a quick code, and havn’t managed to get it to do what i want. Basically, i want a dialog box to appear, with 2 text boxes and an image shown in a particular layout. I have the following code:
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
RelativeLayout dialogItems = new RelativeLayout(this);
EditText itemTitle = new EditText(this);
EditText itemBody = new EditText(this);
ImageView dIcon = new ImageView(this);
itemTitle.setText("Note Title");
itemBody.setText("Note Details");
dIcon.setImageResource(R.drawable.create);
final RelativeLayout.LayoutParams imageParam = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
imageParam.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
imageParam.addRule(RelativeLayout.ALIGN_PARENT_TOP);
dIcon.setLayoutParams(imageParam);
dialogItems.addView(dIcon, imageParam);
final RelativeLayout.LayoutParams titleParam = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
titleParam.addRule(RelativeLayout.RIGHT_OF, dIcon.getId());
titleParam.addRule(RelativeLayout.ALIGN_PARENT_TOP);
itemTitle.setLayoutParams(titleParam);
dialogItems.addView(itemTitle, titleParam);
final RelativeLayout.LayoutParams bodyParam = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
bodyParam.addRule(RelativeLayout.ALIGN_LEFT, itemTitle.getId());
bodyParam.addRule(RelativeLayout.BELOW, itemTitle.getId());
itemBody.setLayoutParams(bodyParam);
dialogItems.addView(itemBody, bodyParam);
dialog.setView(dialogItems);
dialog.show();
Does anyone know why this wouldn’t be working? The problem is, the popup appears, but all the items are just overlapping in the top left. Thanks
P.S. i have checked other posts and questions, and even the answers don’t work! So please just fix my code rather than linking me to another question.
You didn’t set the IDs so every view has the same ID (-1). This should work: