0

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);

                }
            });

        }
    }
}
Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71
amshel
  • 1
  • 1

1 Answers1

1

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.

Community
  • 1
  • 1
arungiri_10
  • 988
  • 1
  • 11
  • 23
  • Its for data collection so each day ill be having different sets of data – amshel Aug 22 '16 at 12:35
  • @amshel i think there should be a much better way to do this even if you have a dynamic set of data everyday. Maybe you can tell us more of the problem that you have and everyone can suggest a better answer to it. – Bundeeteddee Aug 23 '16 at 07:58
  • @Bundeeteddee The app is for collecting daily readings of data. about 17 sets, now i have 17 edit boxes which when data is input and i click the the submit button i get 17 text views of the inputted data – amshel Aug 24 '16 at 07:55
  • @amshelok so you need to show 17 edit boxes everyday, user goes through all 17 and press one submit button, and it should show the result in 17 textviews? In that case @arungiri 's recommendation would be a better way to go. The `EditText` boxes (17 of them can be in xml if thats fixed) should be sitting in a container (`LinearLayout` or whichever container you want, so you can hide this a lot easier). For showing the `TextView`s upon clicking the button, have an empty container (again, `LinearLayout` or whatever you like) that you can dynamically create `TextView` to insert into. – Bundeeteddee Aug 24 '16 at 08:32