Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set multiple text boxes in a dialog in android

I am creating a dialog box which is like a login screen containing two text boxes and two buttons. I am able to create it but my problem is the two edit text boxes are overlapping with each other(Second edit text box is overlapping with the first one). It may be a simple one but since i m new to android i m stuck with it.Pls help me to 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();      
        } 
    }
like image 548
SSG Avatar asked Sep 07 '11 12:09

SSG


2 Answers

What if you try with a 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 perfectly : I invite you to copy paste it :).

like image 176
mthpvg Avatar answered Oct 25 '22 06:10

mthpvg


You can create a layout XML for dialog boxes as well. Before calling your Dialog just do:

myDialog.setContentView(R.layout.my_dialog_layout);
like image 36
Dyonisos Avatar answered Oct 25 '22 05:10

Dyonisos