How To Add Textview And Edittext Using Default Alertdialog Programmatically
I've been trying to add two elements in a default AlertDialog but I can't seem to make it work. Here's my code: // START Dialog AlertDialog.Builder alertDialogBuilder = new Ale
Solution 1:
AlertDialog.BuilderalertDialogBuilder=newAlertDialog.Builder(this);
LinearLayoutlayout=newLinearLayout(this);
LinearLayout.LayoutParamsparms=newLinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(parms);
layout.setGravity(Gravity.CLIP_VERTICAL);
layout.setPadding(2, 2, 2, 2);
TextViewtv=newTextView(this);
tv.setText("Text View title");
tv.setPadding(40, 40, 40, 40);
tv.setGravity(Gravity.CENTER);
tv.setTextSize(20);
EditTextet=newEditText(this);
etStr = et.getText().toString();
TextViewtv1=newTextView(this);
tv1.setText("Input Student ID");
LinearLayout.LayoutParamstv1Params=newLinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
tv1Params.bottomMargin = 5;
layout.addView(tv1,tv1Params);
layout.addView(et, newLinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
alertDialogBuilder.setView(layout);
alertDialogBuilder.setTitle(title);
// alertDialogBuilder.setMessage("Input Student ID");
alertDialogBuilder.setCustomTitle(tv);
if (isError)
alertDialogBuilder.setIcon(R.drawable.icon_warning);
// alertDialogBuilder.setMessage(message);
alertDialogBuilder.setCancelable(false);
// Setting Negative "Cancel" Button
alertDialogBuilder.setNegativeButton("Cancel", newDialogInterface.OnClickListener() {
publicvoidonClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
});
// Setting Positive "OK" Button
alertDialogBuilder.setPositiveButton("OK", newDialogInterface.OnClickListener() {
publicvoidonClick(DialogInterface dialog, int which) {
if (isError)
finish();
else {
Intentintent=newIntent(ChangeDeviceActivity.this,
MyPageActivity.class); startActivity(intent);
}
}
});
AlertDialogalertDialog= alertDialogBuilder.create();
try {
alertDialog.show();
} catch (Exception e) {
// WindowManager$BadTokenException will be caught and the app would// not display the 'Force Close' message
e.printStackTrace();
}
Solution 2:
Make one .xml file which include whatever view you want like below.
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:id="@+id/textView1"android:layout_width="312dp"android:layout_height="wrap_content"android:text="Enter your email address :"android:layout_marginLeft="5dp"android:textAppearance="?android:attr/textAppearanceSmall"/><EditTextandroid:id="@+id/dialog1Edittext"android:layout_width="fill_parent"android:layout_height="wrap_content"android:ems="10"android:layout_marginTop="10dp"android:inputType="textEmailAddress"><requestFocus/></EditText>
After that in your .java file implements following code.
Viewview= View.inflate(this, R.layout.yourxmlname, null);
AlertDialog.Builderalert=newAlertDialog.Builder(this);
// Now set the dialog's content
alert.setContentView(view);
Hope it helps you.
Solution 3:
When calling setView()
the original TextView
holding the message gets hidden.
You need to
- create a small layout xml and put the EditText and a TextView to it
- inflate the layout
- get references of the views with
findViewById()
- do something with the views
Post a Comment for "How To Add Textview And Edittext Using Default Alertdialog Programmatically"