amshel
amshel

Reputation: 1

Changing 2 or more edittexts to textview android

Hello I'm new to android and there is a project I'm undertaking where i have multiple EditText that I want to convert to Textviews. I require a method to help me convert all of them by the click of a button

Main activity

public class MainActivity extends AppCompatActivity {

    EditText[] editTextArray = new EditText[]{

            (EditText) findViewById(R.id.editText),
            (EditText) findViewById(R.id.editText3),
            (EditText) findViewById(R.id.editText4),
            (EditText) findViewById(R.id.editText5),
            (EditText) findViewById(R.id.editText6),
            (EditText) findViewById(R.id.editText7),
            (EditText) findViewById(R.id.editText8),
            (EditText) findViewById(R.id.editText9),
            (EditText) findViewById(R.id.editText10),
            (EditText) findViewById(R.id.editText11),
            (EditText) findViewById(R.id.editText12),
            (EditText) findViewById(R.id.editText13),
            (EditText) findViewById(R.id.editText14),
            (EditText) findViewById(R.id.editText15),
            (EditText) findViewById(R.id.editText16),
            (EditText) findViewById(R.id.editText17),
            (EditText) findViewById(R.id.editText18),
    };
    TextView[] textViews = new TextView[]
            {
                    (TextView) findViewById(R.id.textView15),
                    (TextView) findViewById(R.id.textView20),
                    (TextView) findViewById(R.id.textView21),
                    (TextView) findViewById(R.id.textView22),
                    (TextView) findViewById(R.id.textView23),
                    (TextView) findViewById(R.id.textView24),
                    (TextView) findViewById(R.id.textView26),
                    (TextView) findViewById(R.id.textView27),
                    (TextView) findViewById(R.id.textView28),
                    (TextView) findViewById(R.id.textView29),
                    (TextView) findViewById(R.id.textView30),
                    (TextView) findViewById(R.id.textView31),
                    (TextView) findViewById(R.id.textView32),
                    (TextView) findViewById(R.id.textView33),
                    (TextView) findViewById(R.id.textView34),
                    (TextView) findViewById(R.id.textView35),
                    (TextView) findViewById(R.id.textView36),

            };


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


        for (int x = 0; x < editTextArray.length; x++) {
            editTextArray[x].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String output = x.getText().toString();
                    textViews.setText(output);

                }
            });

        }
    }
}

Upvotes: 0

Views: 46

Answers (1)

arungiri_10
arungiri_10

Reputation: 988

Rather adding textview or edittext from xml, you need to add them dynamically.

Have a look at this link

How can I add a TextView to a LinearLayout dynamically in Android?

With this approach you will have a good control on textview and edittext.

For ex: On click of a button, hide the edittext and in that position show textview.

Upvotes: 1

Related Questions