Haytham Tahboub
Haytham Tahboub

Reputation: 33

Adding EditText in inner class

I want to add an EditText to the current Activity (MainActivity.java & activity_main.xml) from a button onClick listener and I have no idea how to get that layout by-code, I've tried this and it didn't work.

package com.example.hythm.ui_practise;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Button PlusButton=(Button) findViewById(R.id.ButtonOpPlus);
        final Button ButtonNo1=(Button) findViewById(R.id.no1);
        final Button ButtonNo2=(Button) findViewById(R.id.no2);
        final Button ButtonNo3=(Button) findViewById(R.id.no3);
        final Button ButtonNo4=(Button) findViewById(R.id.no4);
        final Button ButtonNo5=(Button) findViewById(R.id.no5);
        final Button ButtonNo6=(Button) findViewById(R.id.no6);
        final Button ButtonNo7=(Button) findViewById(R.id.no7);
        final Button ButtonNo8=(Button) findViewById(R.id.no8);
        final Button ButtonNo9=(Button) findViewById(R.id.no9);
        final TextView ResultTextView=(TextView)findViewById(R.id.Result);
        final Button CalculateButton=(Button) findViewById(R.id.Calculate);
        final TextView tempv=new TextView(this);
        CalculateButton.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        EditText NoOfInputsEditText=(EditText)findViewById(R.id.NoOfInputs);
                        int size = Integer.parseInt(NoOfInputsEditText.getText().toString()); // total number of TextViews to add
                        **RelativeLayout myLayout=(RelativeLayout)R.layout.activity_main;
                        EditText newEditText=new EditText(getBaseContext());
                        myLayout.addView(newEditText);**
                    }
                }
        );
    }
}

Upvotes: 0

Views: 95

Answers (2)

Gaurav Kumar
Gaurav Kumar

Reputation: 146

If you want to add a multiple EditText into layout on button click you can try this:

   // root_layout is the relative layout 
   final RelativeLayout layout = findViewById(R.id.root_layout);

    findViewById(R.id.test_button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            RelativeLayout.LayoutParams[] params = new RelativeLayout.LayoutParams[4];
            EditText[] dynamicEditText = new EditText[4];
            for (int i = 1; i < 4; i++) {
                params[i] = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

                dynamicEditText[i] = new EditText(MainActivity.this);
                dynamicEditText[i].setId(i);
                dynamicEditText[i].setText("Edit Text "+i);
                if (i > 1) {
                    params[i].addRule(RelativeLayout.BELOW, dynamicEditText[i - 1].getId());
                }
                dynamicEditText[i].setLayoutParams(params[i]);
                layout.addView(dynamicEditText[i]);


            }

        }
    });
}

Upvotes: 2

Bethan
Bethan

Reputation: 971

Before going for the fix, there is an another pretty simple solution.

Any how you need an edit text when clicking on a button (CalculateButton), so create a view of edit text in the xml itself where ever the place you need it in the screen and set visibility to Gone.

When clicking on the CalculateButton, Just visible the EditText.

Ex:

EditText newEditText;
//Declare this as public variable, because when we use this inside any override methods, the override methods will not let the private variable changes the state until you changes it to final.
newEditText =(EditText) findViewById(R.id.newEditText);

CalculateButton.setOnClickListener(
    new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            EditText NoOfInputsEditText=(EditText)findViewById(R.id.NoOfInputs);
            int size = Integer.parseInt(NoOfInputsEditText.getText().toString()); // total number of TextViews to add
            newEditText.setVisibility(View.VISIBLE);
        }
    }
);

Upvotes: 1

Related Questions