Adding EditText dynamically by pressing a Button Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Trigger a button click with JavaScript on the Enter key in a text box<button> vs. <input type=“button” />. Which to use?Android - Handle “Enter” in an EditTextStop EditText from gaining focus at Activity startupHow to create an HTML button that acts like a link?Limit text length of EditText in AndroidFirst letter capitalization for EditTextPlace cursor at the end of text in EditTextSet EditText cursor colorHow do I play an audio file in Android?

Does the Black Tentacles spell do damage twice at the start of turn to an already restrained creature?

How to change the tick of the color bar legend to black

Special flights

Monty Hall Problem-Probability Paradox

Why does electrolysis of aqueous concentrated sodium bromide produce bromine at the anode?

Tips to organize LaTeX presentations for a semester

Random body shuffle every night—can we still function?

Rationale for describing kurtosis as "peakedness"?

A term for a woman complaining about things/begging in a cute/childish way

Trying to understand entropy as a novice in thermodynamics

What does it mean that physics no longer uses mechanical models to describe phenomena?

Should a wizard buy fine inks every time he want to copy spells into his spellbook?

How were pictures turned from film to a big picture in a picture frame before digital scanning?

Why are vacuum tubes still used in amateur radios?

A proverb that is used to imply that you have unexpectedly faced a big problem

Nose gear failure in single prop aircraft: belly landing or nose-gear up landing?

Simple Http Server

Where is the Next Backup Size entry on iOS 12?

Most effective melee weapons for arboreal combat? (pre-gunpowder technology)

The Nth Gryphon Number

Delete free apps from library

How much damage would a cupful of neutron star matter do to the Earth?

Tannaka duality for semisimple groups

How can I prevent/balance waiting and turtling as a response to cooldown mechanics



Adding EditText dynamically by pressing a Button



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Trigger a button click with JavaScript on the Enter key in a text box<button> vs. <input type=“button” />. Which to use?Android - Handle “Enter” in an EditTextStop EditText from gaining focus at Activity startupHow to create an HTML button that acts like a link?Limit text length of EditText in AndroidFirst letter capitalization for EditTextPlace cursor at the end of text in EditTextSet EditText cursor colorHow do I play an audio file in Android?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I want to add an "ingredient" EditText view next to a "quantity" EditText view, pressing an "add" Button. Here's the starting layout code:



<LinearLayout android:id="@+id/ingredients_line_layout"
android:layout_below="@+id/title_line_layout"
android:layout_height="wrap_content"
android:layout_margin="@dimen/layout_margin"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<EditText
android:id="@+id/ingredientsField"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="@dimen/boxes_margin"
android:hint="@string/ingredients"
android:inputType="text" />

<EditText
android:hint="@string/quantity"
android:id="@+id/quantityField"
android:inputType="number"
android:layout_height="match_parent"
android:layout_margin="@dimen/boxes_margin"
android:layout_width="wrap_content" />

<com.google.android.material.button.MaterialButton
android:id="@+id/add_ingredient_btn"
android:layout_height="wrap_content"
android:layout_margin="@dimen/boxes_margin"
android:layout_width="wrap_content"
android:text="@string/add"
app:icon="@drawable/ic_add_ingr_btn" />

</LinearLayout>


How can I implement the onClick method for the button, in the corresponding activity?










share|improve this question
























  • Do you want to just show a single EditText on the button click or want to add a new one every time the button is pressed?

    – Ajay Sivan
    Mar 22 at 12:03












  • I want to add a new one every time the button is pressed

    – Edoardo Tavilla
    Mar 25 at 10:11

















1















I want to add an "ingredient" EditText view next to a "quantity" EditText view, pressing an "add" Button. Here's the starting layout code:



<LinearLayout android:id="@+id/ingredients_line_layout"
android:layout_below="@+id/title_line_layout"
android:layout_height="wrap_content"
android:layout_margin="@dimen/layout_margin"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<EditText
android:id="@+id/ingredientsField"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="@dimen/boxes_margin"
android:hint="@string/ingredients"
android:inputType="text" />

<EditText
android:hint="@string/quantity"
android:id="@+id/quantityField"
android:inputType="number"
android:layout_height="match_parent"
android:layout_margin="@dimen/boxes_margin"
android:layout_width="wrap_content" />

<com.google.android.material.button.MaterialButton
android:id="@+id/add_ingredient_btn"
android:layout_height="wrap_content"
android:layout_margin="@dimen/boxes_margin"
android:layout_width="wrap_content"
android:text="@string/add"
app:icon="@drawable/ic_add_ingr_btn" />

</LinearLayout>


How can I implement the onClick method for the button, in the corresponding activity?










share|improve this question
























  • Do you want to just show a single EditText on the button click or want to add a new one every time the button is pressed?

    – Ajay Sivan
    Mar 22 at 12:03












  • I want to add a new one every time the button is pressed

    – Edoardo Tavilla
    Mar 25 at 10:11













1












1








1








I want to add an "ingredient" EditText view next to a "quantity" EditText view, pressing an "add" Button. Here's the starting layout code:



<LinearLayout android:id="@+id/ingredients_line_layout"
android:layout_below="@+id/title_line_layout"
android:layout_height="wrap_content"
android:layout_margin="@dimen/layout_margin"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<EditText
android:id="@+id/ingredientsField"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="@dimen/boxes_margin"
android:hint="@string/ingredients"
android:inputType="text" />

<EditText
android:hint="@string/quantity"
android:id="@+id/quantityField"
android:inputType="number"
android:layout_height="match_parent"
android:layout_margin="@dimen/boxes_margin"
android:layout_width="wrap_content" />

<com.google.android.material.button.MaterialButton
android:id="@+id/add_ingredient_btn"
android:layout_height="wrap_content"
android:layout_margin="@dimen/boxes_margin"
android:layout_width="wrap_content"
android:text="@string/add"
app:icon="@drawable/ic_add_ingr_btn" />

</LinearLayout>


How can I implement the onClick method for the button, in the corresponding activity?










share|improve this question
















I want to add an "ingredient" EditText view next to a "quantity" EditText view, pressing an "add" Button. Here's the starting layout code:



<LinearLayout android:id="@+id/ingredients_line_layout"
android:layout_below="@+id/title_line_layout"
android:layout_height="wrap_content"
android:layout_margin="@dimen/layout_margin"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<EditText
android:id="@+id/ingredientsField"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="@dimen/boxes_margin"
android:hint="@string/ingredients"
android:inputType="text" />

<EditText
android:hint="@string/quantity"
android:id="@+id/quantityField"
android:inputType="number"
android:layout_height="match_parent"
android:layout_margin="@dimen/boxes_margin"
android:layout_width="wrap_content" />

<com.google.android.material.button.MaterialButton
android:id="@+id/add_ingredient_btn"
android:layout_height="wrap_content"
android:layout_margin="@dimen/boxes_margin"
android:layout_width="wrap_content"
android:text="@string/add"
app:icon="@drawable/ic_add_ingr_btn" />

</LinearLayout>


How can I implement the onClick method for the button, in the corresponding activity?







android button android-edittext android-linearlayout material






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 22 at 13:21









Chandan Sharma

4281415




4281415










asked Mar 22 at 11:50









Edoardo TavillaEdoardo Tavilla

368




368












  • Do you want to just show a single EditText on the button click or want to add a new one every time the button is pressed?

    – Ajay Sivan
    Mar 22 at 12:03












  • I want to add a new one every time the button is pressed

    – Edoardo Tavilla
    Mar 25 at 10:11

















  • Do you want to just show a single EditText on the button click or want to add a new one every time the button is pressed?

    – Ajay Sivan
    Mar 22 at 12:03












  • I want to add a new one every time the button is pressed

    – Edoardo Tavilla
    Mar 25 at 10:11
















Do you want to just show a single EditText on the button click or want to add a new one every time the button is pressed?

– Ajay Sivan
Mar 22 at 12:03






Do you want to just show a single EditText on the button click or want to add a new one every time the button is pressed?

– Ajay Sivan
Mar 22 at 12:03














I want to add a new one every time the button is pressed

– Edoardo Tavilla
Mar 25 at 10:11





I want to add a new one every time the button is pressed

– Edoardo Tavilla
Mar 25 at 10:11












3 Answers
3






active

oldest

votes


















1














  1. You need to set OnClickListener for "add" button;

  2. Create a view you want to add;

  3. Find ingredients_line_layout layout and add your view to it.

See code below:



@Override
public void onCreate(@Nullable Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
View button = findViewById(R.id.add_ingredient_btn);
button.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
ViewGroup layout = (ViewGroup) findViewById(R.id.ingredients_line_layout);
EditText ingredient = new EditText(YourActivity.this);
ingredient.setHint(R.string.ingredients);
layout.addView(ingredient, 1);

);






share|improve this answer

























  • Thanks. It works, but how can I add each new EditText view in a different line of my LinearLayout?

    – Edoardo Tavilla
    Mar 25 at 10:59











  • @EdoardoTavilla Look at this line: layout.addView(ingredient, 1). Second param is a position inside LinearLayout. Change it to add a view to a position you want. In my example I use constant value but you can calculate position dynamically.

    – Andrew Churilo
    Mar 25 at 11:10


















0














Step 1: Write all views in the XML file, and hide "ingredient" EditText



<LinearLayout android:id="@+id/ingredients_line_layout"
android:layout_below="@+id/title_line_layout"
android:layout_height="wrap_content"
android:layout_margin="@dimen/layout_margin"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<EditText
android:hint="@string/quantity"
android:id="@+id/quantityField"
android:inputType="number"
android:layout_height="match_parent"
android:layout_margin="@dimen/boxes_margin"
android:layout_width="wrap_content" />

<EditText
android:id="@+id/ingredientsField"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="@dimen/boxes_margin"
android:hint="@string/ingredients"
android:visibility="gone"
android:inputType="text" />

<com.google.android.material.button.MaterialButton
android:id="@+id/add_ingredient_btn"
android:layout_height="wrap_content"
android:layout_margin="@dimen/boxes_margin"
android:layout_width="wrap_content"
android:text="@string/add"
app:icon="@drawable/ic_add_ingr_btn" />

</LinearLayout>


Step 2: Register onClick listener on add button.



addIngredientBtn.setOnClickListener(this);


Step 3: Visible "ingredient" on button click



@Override
public void onClick(View v)
ingredientsField.setVisibility(View.VISIBLE);



By this way, you can add EditText Dynamically.






share|improve this answer






























    0














    EditText etIngredient = new EditText(context); // pass Context here
    etIngredient.setLayoutParams(new LayoutParams(..., ...)); // two Arguments such as LayoutParams.WRAP_CONTENT
    layout.addView(etIngredient);


    You might wanna add weights to the layout holding the editText accordingly if there are two edit text beside each other.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55298983%2fadding-edittext-dynamically-by-pressing-a-button%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      1. You need to set OnClickListener for "add" button;

      2. Create a view you want to add;

      3. Find ingredients_line_layout layout and add your view to it.

      See code below:



      @Override
      public void onCreate(@Nullable Bundle savedInstanceState)
      super.onCreate(savedInstanceState);
      setContentView(R.layout.your_layout);
      View button = findViewById(R.id.add_ingredient_btn);
      button.setOnClickListener(new View.OnClickListener()
      @Override
      public void onClick(View v)
      ViewGroup layout = (ViewGroup) findViewById(R.id.ingredients_line_layout);
      EditText ingredient = new EditText(YourActivity.this);
      ingredient.setHint(R.string.ingredients);
      layout.addView(ingredient, 1);

      );






      share|improve this answer

























      • Thanks. It works, but how can I add each new EditText view in a different line of my LinearLayout?

        – Edoardo Tavilla
        Mar 25 at 10:59











      • @EdoardoTavilla Look at this line: layout.addView(ingredient, 1). Second param is a position inside LinearLayout. Change it to add a view to a position you want. In my example I use constant value but you can calculate position dynamically.

        – Andrew Churilo
        Mar 25 at 11:10















      1














      1. You need to set OnClickListener for "add" button;

      2. Create a view you want to add;

      3. Find ingredients_line_layout layout and add your view to it.

      See code below:



      @Override
      public void onCreate(@Nullable Bundle savedInstanceState)
      super.onCreate(savedInstanceState);
      setContentView(R.layout.your_layout);
      View button = findViewById(R.id.add_ingredient_btn);
      button.setOnClickListener(new View.OnClickListener()
      @Override
      public void onClick(View v)
      ViewGroup layout = (ViewGroup) findViewById(R.id.ingredients_line_layout);
      EditText ingredient = new EditText(YourActivity.this);
      ingredient.setHint(R.string.ingredients);
      layout.addView(ingredient, 1);

      );






      share|improve this answer

























      • Thanks. It works, but how can I add each new EditText view in a different line of my LinearLayout?

        – Edoardo Tavilla
        Mar 25 at 10:59











      • @EdoardoTavilla Look at this line: layout.addView(ingredient, 1). Second param is a position inside LinearLayout. Change it to add a view to a position you want. In my example I use constant value but you can calculate position dynamically.

        – Andrew Churilo
        Mar 25 at 11:10













      1












      1








      1







      1. You need to set OnClickListener for "add" button;

      2. Create a view you want to add;

      3. Find ingredients_line_layout layout and add your view to it.

      See code below:



      @Override
      public void onCreate(@Nullable Bundle savedInstanceState)
      super.onCreate(savedInstanceState);
      setContentView(R.layout.your_layout);
      View button = findViewById(R.id.add_ingredient_btn);
      button.setOnClickListener(new View.OnClickListener()
      @Override
      public void onClick(View v)
      ViewGroup layout = (ViewGroup) findViewById(R.id.ingredients_line_layout);
      EditText ingredient = new EditText(YourActivity.this);
      ingredient.setHint(R.string.ingredients);
      layout.addView(ingredient, 1);

      );






      share|improve this answer















      1. You need to set OnClickListener for "add" button;

      2. Create a view you want to add;

      3. Find ingredients_line_layout layout and add your view to it.

      See code below:



      @Override
      public void onCreate(@Nullable Bundle savedInstanceState)
      super.onCreate(savedInstanceState);
      setContentView(R.layout.your_layout);
      View button = findViewById(R.id.add_ingredient_btn);
      button.setOnClickListener(new View.OnClickListener()
      @Override
      public void onClick(View v)
      ViewGroup layout = (ViewGroup) findViewById(R.id.ingredients_line_layout);
      EditText ingredient = new EditText(YourActivity.this);
      ingredient.setHint(R.string.ingredients);
      layout.addView(ingredient, 1);

      );







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Mar 22 at 12:23

























      answered Mar 22 at 12:17









      Andrew ChuriloAndrew Churilo

      1,428814




      1,428814












      • Thanks. It works, but how can I add each new EditText view in a different line of my LinearLayout?

        – Edoardo Tavilla
        Mar 25 at 10:59











      • @EdoardoTavilla Look at this line: layout.addView(ingredient, 1). Second param is a position inside LinearLayout. Change it to add a view to a position you want. In my example I use constant value but you can calculate position dynamically.

        – Andrew Churilo
        Mar 25 at 11:10

















      • Thanks. It works, but how can I add each new EditText view in a different line of my LinearLayout?

        – Edoardo Tavilla
        Mar 25 at 10:59











      • @EdoardoTavilla Look at this line: layout.addView(ingredient, 1). Second param is a position inside LinearLayout. Change it to add a view to a position you want. In my example I use constant value but you can calculate position dynamically.

        – Andrew Churilo
        Mar 25 at 11:10
















      Thanks. It works, but how can I add each new EditText view in a different line of my LinearLayout?

      – Edoardo Tavilla
      Mar 25 at 10:59





      Thanks. It works, but how can I add each new EditText view in a different line of my LinearLayout?

      – Edoardo Tavilla
      Mar 25 at 10:59













      @EdoardoTavilla Look at this line: layout.addView(ingredient, 1). Second param is a position inside LinearLayout. Change it to add a view to a position you want. In my example I use constant value but you can calculate position dynamically.

      – Andrew Churilo
      Mar 25 at 11:10





      @EdoardoTavilla Look at this line: layout.addView(ingredient, 1). Second param is a position inside LinearLayout. Change it to add a view to a position you want. In my example I use constant value but you can calculate position dynamically.

      – Andrew Churilo
      Mar 25 at 11:10













      0














      Step 1: Write all views in the XML file, and hide "ingredient" EditText



      <LinearLayout android:id="@+id/ingredients_line_layout"
      android:layout_below="@+id/title_line_layout"
      android:layout_height="wrap_content"
      android:layout_margin="@dimen/layout_margin"
      android:layout_width="match_parent"
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">

      <EditText
      android:hint="@string/quantity"
      android:id="@+id/quantityField"
      android:inputType="number"
      android:layout_height="match_parent"
      android:layout_margin="@dimen/boxes_margin"
      android:layout_width="wrap_content" />

      <EditText
      android:id="@+id/ingredientsField"
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:layout_margin="@dimen/boxes_margin"
      android:hint="@string/ingredients"
      android:visibility="gone"
      android:inputType="text" />

      <com.google.android.material.button.MaterialButton
      android:id="@+id/add_ingredient_btn"
      android:layout_height="wrap_content"
      android:layout_margin="@dimen/boxes_margin"
      android:layout_width="wrap_content"
      android:text="@string/add"
      app:icon="@drawable/ic_add_ingr_btn" />

      </LinearLayout>


      Step 2: Register onClick listener on add button.



      addIngredientBtn.setOnClickListener(this);


      Step 3: Visible "ingredient" on button click



      @Override
      public void onClick(View v)
      ingredientsField.setVisibility(View.VISIBLE);



      By this way, you can add EditText Dynamically.






      share|improve this answer



























        0














        Step 1: Write all views in the XML file, and hide "ingredient" EditText



        <LinearLayout android:id="@+id/ingredients_line_layout"
        android:layout_below="@+id/title_line_layout"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/layout_margin"
        android:layout_width="match_parent"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">

        <EditText
        android:hint="@string/quantity"
        android:id="@+id/quantityField"
        android:inputType="number"
        android:layout_height="match_parent"
        android:layout_margin="@dimen/boxes_margin"
        android:layout_width="wrap_content" />

        <EditText
        android:id="@+id/ingredientsField"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_margin="@dimen/boxes_margin"
        android:hint="@string/ingredients"
        android:visibility="gone"
        android:inputType="text" />

        <com.google.android.material.button.MaterialButton
        android:id="@+id/add_ingredient_btn"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/boxes_margin"
        android:layout_width="wrap_content"
        android:text="@string/add"
        app:icon="@drawable/ic_add_ingr_btn" />

        </LinearLayout>


        Step 2: Register onClick listener on add button.



        addIngredientBtn.setOnClickListener(this);


        Step 3: Visible "ingredient" on button click



        @Override
        public void onClick(View v)
        ingredientsField.setVisibility(View.VISIBLE);



        By this way, you can add EditText Dynamically.






        share|improve this answer

























          0












          0








          0







          Step 1: Write all views in the XML file, and hide "ingredient" EditText



          <LinearLayout android:id="@+id/ingredients_line_layout"
          android:layout_below="@+id/title_line_layout"
          android:layout_height="wrap_content"
          android:layout_margin="@dimen/layout_margin"
          android:layout_width="match_parent"
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto">

          <EditText
          android:hint="@string/quantity"
          android:id="@+id/quantityField"
          android:inputType="number"
          android:layout_height="match_parent"
          android:layout_margin="@dimen/boxes_margin"
          android:layout_width="wrap_content" />

          <EditText
          android:id="@+id/ingredientsField"
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:layout_margin="@dimen/boxes_margin"
          android:hint="@string/ingredients"
          android:visibility="gone"
          android:inputType="text" />

          <com.google.android.material.button.MaterialButton
          android:id="@+id/add_ingredient_btn"
          android:layout_height="wrap_content"
          android:layout_margin="@dimen/boxes_margin"
          android:layout_width="wrap_content"
          android:text="@string/add"
          app:icon="@drawable/ic_add_ingr_btn" />

          </LinearLayout>


          Step 2: Register onClick listener on add button.



          addIngredientBtn.setOnClickListener(this);


          Step 3: Visible "ingredient" on button click



          @Override
          public void onClick(View v)
          ingredientsField.setVisibility(View.VISIBLE);



          By this way, you can add EditText Dynamically.






          share|improve this answer













          Step 1: Write all views in the XML file, and hide "ingredient" EditText



          <LinearLayout android:id="@+id/ingredients_line_layout"
          android:layout_below="@+id/title_line_layout"
          android:layout_height="wrap_content"
          android:layout_margin="@dimen/layout_margin"
          android:layout_width="match_parent"
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto">

          <EditText
          android:hint="@string/quantity"
          android:id="@+id/quantityField"
          android:inputType="number"
          android:layout_height="match_parent"
          android:layout_margin="@dimen/boxes_margin"
          android:layout_width="wrap_content" />

          <EditText
          android:id="@+id/ingredientsField"
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:layout_margin="@dimen/boxes_margin"
          android:hint="@string/ingredients"
          android:visibility="gone"
          android:inputType="text" />

          <com.google.android.material.button.MaterialButton
          android:id="@+id/add_ingredient_btn"
          android:layout_height="wrap_content"
          android:layout_margin="@dimen/boxes_margin"
          android:layout_width="wrap_content"
          android:text="@string/add"
          app:icon="@drawable/ic_add_ingr_btn" />

          </LinearLayout>


          Step 2: Register onClick listener on add button.



          addIngredientBtn.setOnClickListener(this);


          Step 3: Visible "ingredient" on button click



          @Override
          public void onClick(View v)
          ingredientsField.setVisibility(View.VISIBLE);



          By this way, you can add EditText Dynamically.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 22 at 12:05









          Chandan SharmaChandan Sharma

          4281415




          4281415





















              0














              EditText etIngredient = new EditText(context); // pass Context here
              etIngredient.setLayoutParams(new LayoutParams(..., ...)); // two Arguments such as LayoutParams.WRAP_CONTENT
              layout.addView(etIngredient);


              You might wanna add weights to the layout holding the editText accordingly if there are two edit text beside each other.






              share|improve this answer



























                0














                EditText etIngredient = new EditText(context); // pass Context here
                etIngredient.setLayoutParams(new LayoutParams(..., ...)); // two Arguments such as LayoutParams.WRAP_CONTENT
                layout.addView(etIngredient);


                You might wanna add weights to the layout holding the editText accordingly if there are two edit text beside each other.






                share|improve this answer

























                  0












                  0








                  0







                  EditText etIngredient = new EditText(context); // pass Context here
                  etIngredient.setLayoutParams(new LayoutParams(..., ...)); // two Arguments such as LayoutParams.WRAP_CONTENT
                  layout.addView(etIngredient);


                  You might wanna add weights to the layout holding the editText accordingly if there are two edit text beside each other.






                  share|improve this answer













                  EditText etIngredient = new EditText(context); // pass Context here
                  etIngredient.setLayoutParams(new LayoutParams(..., ...)); // two Arguments such as LayoutParams.WRAP_CONTENT
                  layout.addView(etIngredient);


                  You might wanna add weights to the layout holding the editText accordingly if there are two edit text beside each other.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 22 at 12:15









                  Shahnavaz AnsariShahnavaz Ansari

                  112




                  112



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55298983%2fadding-edittext-dynamically-by-pressing-a-button%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                      SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                      은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현