3

I am working on an SMS application. I have a "+" button so that when user clicks that button a new ExitText will appear under the existing one for user to enter multiple phone numbers to send the text. Can anyone please help with creating a new EditText when a button is pressed?

Thank You,

aa051
  • 83
  • 4
  • 9
  • show your layout where + button is.also where you want to add EditText.It is all about adding view in the layout – Rasel Jul 22 '11 at 15:29
  • 1
    @rds: I am sorry, but I am new to Android and just started to work with it. I tried to google it but could not find it and thts y i posted a question here. – aa051 Jul 22 '11 at 16:40

3 Answers3

5

I would keep a List of EditText objects and add a new one

EditText toAdd = new EditText(this);
list.add(toAdd);

to the list on button press. Also, read this link for how to add that new EditText to your current layout. How to lay out Views in RelativeLayout programmatically?

When you know the user is done and wants to save the numbers, iterate through your List of EditText objects.

Community
  • 1
  • 1
Kevin King
  • 1,549
  • 11
  • 14
2

I built an app that adds buttons dynamically based on how many rows there on in my database.

basically I found it easier to create an array of buttons with length equal to the number of buttons I need: In your case...

final int PHONE_NUMBERS = 0;

final int OTHER_STUFF = 1;

final int MORE_STUFF = 2;

LinearLayout MyEditTextLayout;

EditText []DynamicFields = new EditText[3];

*note these should be declared outside of onCreate*

then within onCreate {

MyEditTextLayout = (LinearLayout) findViewById (R.id.Whatever_you_named_your_layout_in_xml);

}

then in your onClickListener dialog:

final EditText editText = new EditText();

if(button = myPhoneNumberButton)
{

editText.layout_width = "fill_parent";

editText.hint = "Enter Phone Numbers Here";

DynamicFields[PHONE_NUMBERS] = editText; //that way you can refer to your editTexts later

MyEditTextLayout.addView(editText);

}

please note I typed this out quickly at work so the code might not work exactly as is but this should give you a good start Comment if you have any questions!

VansFannel
  • 45,055
  • 107
  • 359
  • 626
Adam Storm
  • 724
  • 1
  • 6
  • 13
-1

In order to create a dialog with an EditText inside, you can do this in the button's OnClickListener:

final FrameLayout fl = new FrameLayout(ContactView.this);
final EditText txtSms = new EditText(ContactView.this);
txtSms.setRawInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
txtSms.setInputType(InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
txtSms.setHorizontallyScrolling(false);
txtSms.setVerticalScrollBarEnabled(true);
txtSms.setGravity(Gravity.CENTER);
fl.addView(txtSms, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT));

    final AlertDialog.Builder builder = new AlertDialog.Builder(ContactView.this);
        //building the custom AlertDialog
        builder.setView(fl).setTitle("Enviar mensaje").setCancelable(false)
        .setPositiveButton("Send", new DialogInterface.OnClickListener(){

                    //What happens when the positive button is pressed
            public void onClick(DialogInterface d, int which){
                if(txtSms.getText().toString().length() != 0){
                    enviarSms(txtSms.getText().toString());
                    d.dismiss();
                }else{
                    Toast.makeText(((Dialog) d).getContext(), "Can't send an empty message", Toast.LENGTH_SHORT).show();
                }
            }
                    //What happens when the negative button is pressed
        }).setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
                public void onClick(DialogInterface d, int which) {
                d.dismiss();
            }
        }).create().show();
fergerm
  • 241
  • 4
  • 4