Reputation: 597
I am trying to show an alert with two edittext fields in Android. The source is as follows:
public void UserPass(){
final SharedPreferences prefs=getSharedPreferences("PrefsPreferences",MODE_PRIVATE);
String user=prefs.getString("user", "");
String pass=prefs.getString("password", "");
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("U/P");
builder.setCancelable(false);
final EditText input1 = new EditText(this);
final EditText input2 = new EditText(this);
builder.setView(input1);
builder.setView(input2);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//My code
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
Only one EditText is showing up. What am I doing wrong?
Thanks!
Upvotes: 1
Views: 5083
Reputation: 51
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Toast;
public class DialogWithInputBox extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString().trim();
Toast.makeText(getApplicationContext(), value,
Toast.LENGTH_SHORT).show();
}
});
alert.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
});
alert.show();
}
}
Upvotes: 5
Reputation: 1328
The second setView
call replaces the EditText
you set in the first. You can't add two View
s this way. Instead, create a LinearLayout
, add both input1
and input2
to that, and then add that to the builder
.
Upvotes: 1