Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Dynamically or Programmatically add two EditText in one line and make them relate

I have a button that says "Add contact", which, when pressed, adds an EditText (name) right below the button. When the button is pressed again, it adds one more EditText right below it, and so on.

How can I add two EditText in one horizontal line programmatically? Currently I have an array of EditText of size 5 so no more than 5 EditText can be added. I need one more EditText right next to each of them in which they enter another value: phone number.

I want it to look like
[ Name ] [Phone number]
[ Name ] [Phone number]

I can make them print it like
[ Name ] [Phone number]
[ Name ] [Phone number]

but I want them in one line.

Also is it possible to relate two EditText? So for example, name and phone number need to match so that when name is searched, the corresponding phone number will show up.

private EditText et[] = new EditText[5];
private int index = 0;
protected void onCreate(Bundle savedInstanceState){
  super.onCreate(savedInstanceState);

  //apply layout dynamically
  final LinearLayout layout = new LinearLayout(this);
  /* layout style: omit */
  layout.addView( /*omit*/);

  //apply button dynamically
  Button button = new Button(this);
  button.setText("Add Contact");
  /* more button styles: Omit */
  layout.addView(button);

  button.setOnClickListener(new View.onClickListener(){
    @Override
    public void onClick(View v){
      if(index < 5){
        et[index] = new EditText(getApplicationContext());
        et[index].setHint(" NAME ");
        /* more et style: omit*/
        layout.addView(et[index]);
        index++;
      }
    }
  }
}

My current code works fine for adding one EditText each time. But I am not sure how to add two EditText at a time in one line each time.

like image 796
Wang Dang Avatar asked Nov 19 '25 03:11

Wang Dang


1 Answers

Use a horizontal linear layout and add two edit texts inside.
Try this :

LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,       ViewGroup.LayoutParams.WRAP_CONTENT));
linearLayout.setOrientation(LinearLayout.HORIZONTAL);  

Using layout weight as 1 for both edit texts(1.0f is the weight). You can tweak the width and height of the edit texts.

EditText et1 = new EditText(this);
et1.setLayoutParams(new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT,1.0f));  

EditText et2 = new EditText(this);
et2.setLayoutParams(new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT,1.0f));  

Now add edit texts to the horizontal linearLayout and then add it to your layout.

linearLayout.addView(et1);
linearLayout.addView(et2);
layout.addView(linearLayout);
like image 103
Pavan Avatar answered Nov 20 '25 18:11

Pavan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!