1

I'm trying to get my button to create a text field where the user can input information. This way they can only create as many lines as they would like. Also, is there a way to create multiple fields at once? So, it'll end up being something like this:

"Add Event" (rest of the screen is blank until they click on that button)

Text field 1/ Text field 2/ Text field 3

(once they press that button and of course without the underlines, just an example)

So they can put in information that they want there. If they want another row, they click on the add button again.

Am I supposed to be using an onClickListener? I'm confused as to how I would go about making the button create that field for the user.

public class BudgetScreen extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_budget_screen);

    Button addBillExpense = (Button) findViewById(R.id.addBillExpense);

    addBillExpense.setOnClickListener(new Button.OnClickListener() {
        public void onClick (View v) {

            TextView inputField = new TextView(BudgetScreen.this);

        }
    });
}

}

That is what I have so far. I've been stuck on this for a hot minute. I am aware that I haven't used "inputField" yet.

2
  • Try using a onClickListener; if it doesn't do what you expect, post your code, explain what it's doing and what you're expecting it to do. Stack Overflow works best with specific questions like that. Commented May 9, 2016 at 0:08
  • Look into RecyclerViews Commented May 9, 2016 at 0:09

1 Answer 1

1

Suppose you have the following layout xml:

<LinearLayout ...>
   <Button .../>
   <LinearLayout ...
    android:id="@+id/holder"
    android:orientation="vertical">

   </LinearLayout>
</LinearLayout>

in button onClickListener you can have something like:

LinearLayout layout = (LinearLayout) findViewById(R.id.holder);
EditText et = new EditText(this);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
layout.addView(et,lp);

You can change the LayoutParams to get the layout you like.

If you want multiple EditText in a single row, you can do the following:

    final int NUM_EDITTEXT_PER_ROW = 3;
    LinearLayout layout = (LinearLayout) findViewById(R.id.holder);
    Display display = ((WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    int width = display.getWidth()/NUM_EDITTEXT_PER_ROW;
    LinearLayout tempLayout = new LinearLayout(this);
    tempLayout.setOrientation(LinearLayout.HORIZONTAL);
    for(int i=0;i<NUM_EDITTEXT_PER_ROW;i++){
        EditText et = new EditText(this);
        LayoutParams lp = new LayoutParams(width,LayoutParams.WRAP_CONTENT);
        tempLayout.addView(et,lp);
    }
    layout.addView(tempLayout);
Sign up to request clarification or add additional context in comments.

2 Comments

I know this is a very late response, but does this not work for a relative layout? 'cause I've tried that and I just get a bunch of errors in the IDE. All I did was replace all of the "LinearLayout" with "RelativeLayout".
Be sure to avoid layout attributes like "orientation" that are specific to LinearLayout and not to be used in RelativeLayout. Whenever you change from LinearLayout to RelativeLayout, you need to set its child's position relative to each other as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.