Set multiple text fields in dialog box in android

I am creating a dialog box that looks like a login screen containing two text fields and two buttons. I can create it, but my problem is that two editing text fields overlap with each other (the second editing text field overlaps with the first). It might be simple, but since I'm new to android, I'm stuck with it. Pls help me solve it. Here is the source code

public class LoginActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btn= (Button) findViewById(R.id.btn_Login); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showDialog(0); } }); } protected Dialog onCreateDialog(int id) { final AlertDialog.Builder alert = new AlertDialog.Builder(this); final EditText input = new EditText(this); final EditText input1 = new EditText(this); alert.setIcon(R.drawable.icon); alert.setTitle("Login"); alert.setView(input); alert.setView(input1); alert.setView(input1); 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(); } }); return alert.create(); } } 
+7
source share
3 answers

You can also create an XML layout for dialog boxes. Before calling your dialogue, do the following:

 myDialog.setContentView(R.layout.my_dialog_layout); 
+4
source

What if you try with LinerLayout :

 LinearLayout lila1= new LinearLayout(this); lila1.setOrientation(LinearLayout.VERTICAL); final EditText input = new EditText(this); final EditText input1 = new EditText(this); lila1.addView(input); lila1.addView(input1); alert.setView(lila1); 

Like this:

 public class LoginActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btn= (Button) findViewById(R.id.btn_Login); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showDialog(0); } }); } protected Dialog onCreateDialog(int id) { final AlertDialog.Builder alert = new AlertDialog.Builder(this); LinearLayout lila1= new LinearLayout(this); lila1.setOrientation(1); //1 is for vertical orientation final EditText input = new EditText(this); final EditText input1 = new EditText(this); lila1.addView(input); lila1.addView(input1); alert.setView(lila1); alert.setIcon(R.drawable.icon); alert.setTitle("Login"); 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(); } }); return alert.create(); } } 

And it works great: I invite you to copy it :).

+17
source

thnx men, this is work !!! but in this case I give me an error, so I change this:

 lila1.setOrientation(1); //1 is for vertical orientation 

:

 lila1.setOrientation(LinearLayout.VERTICAL); 
0
source

All Articles