-2

Right now I have an idea for a project and I would like to know if anyone can help me on the same logic.

As such I need to create or generate a number of EditText according to amount you enter, ie, to select or enter a number such as 5, show me 5 EditText layout for type 5 values​​. They know that the form could accomplish this? Any ideas please?

I guess it must be a way to do it with a loop, but not like carrying this calculation Java to XML. Thank you.

Qix - MONICA WAS MISTREATED
  • 14,451
  • 16
  • 82
  • 145
Yoshi
  • 19
  • 6

1 Answers1

0

Here is an example of generating EditText items based on the number you enter. You can ignore the scrollview in the layout file I just put it in case someone added a lot of EditText items that would go off the screen.

MainActivity.java

package com.example.test;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class MainActivity extends Activity {

    public static final String TAG = MainActivity.class.getSimpleName();

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

        final EditText count = (EditText) findViewById(R.id.count);
        final TextView output = (TextView) findViewById(R.id.output);
        final Button generate = (Button) findViewById(R.id.generate);
        final Button values = (Button) findViewById(R.id.values);
        final LinearLayout container = (LinearLayout) findViewById(R.id.container);

        generate.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                int numberOfControlsToGenerate = 0;

                try {
                    numberOfControlsToGenerate = Integer.parseInt(count.getText().toString().trim());
                } catch (NumberFormatException e) {
                    Log.e(TAG, e.getMessage(), e);
                }

                if (numberOfControlsToGenerate > 0) {

                    if (container.getChildCount() > 0) {
                        container.removeAllViews();
                    }

                    for (int counter = 0; counter < numberOfControlsToGenerate; counter++) {
                        addEditText(container);
                    }
                }
            }
        });

        values.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                String[] editTextValues = new String[container.getChildCount()];

                StringBuilder editTextValuesBuilder = new StringBuilder();

                for (int counter = 0; counter < container.getChildCount(); counter++) {
                    EditText child = (EditText) container.getChildAt(counter);
                    editTextValues[counter] = child.getText().toString().trim();
                    editTextValuesBuilder.append(editTextValues[counter]).append("\n");
                }

                output.setText(editTextValuesBuilder.toString());
            }
        });
    }

    private void addEditText(LinearLayout container) {
        final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        EditText editTextToAdd = new EditText(this);
        editTextToAdd.setLayoutParams(params);
        container.addView(editTextToAdd);
    }
}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:padding="16dp"
    tools:context="${packageName}.${activityClass}" >

    <EditText
        android:id="@+id/count"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:imeOptions="actionDone"
        android:inputType="number"
        android:textSize="20sp" />

    <Button
        android:id="@+id/generate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Go" />

    <Button
        android:id="@+id/values"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Get Values" />

    <TextView
        android:id="@+id/output"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
        </LinearLayout>
    </ScrollView>

</LinearLayout>
Willie Nel
  • 363
  • 2
  • 9
  • Willie Nel, thank you very much! You've helped me a lot, thanks for sharing your knowledge. Greetings from Venezuela. – Yoshi Jul 30 '14 at 06:13
  • I'm new to this, I'm learning. I have a question, as I can get all the data that has been inserted into the text edit? the edit text and numeric type I have, I need a way to get those values ​​and work with them, try using an array but I get errors. could help me? – Yoshi Jul 31 '14 at 01:17
  • I've updated the example to show you how to retrieve those values and save them in a String array. You can ignore the StringBuilder part, I just put that in to print out the values. – Willie Nel Jul 31 '14 at 05:07
  • Willie thank you very much for your answer and help, but do not know why your code gives me error and my application closes. tried it before? Thanks again – Yoshi Aug 02 '14 at 21:54
  • achieve and fix it. Thank you very much! – Yoshi Aug 02 '14 at 22:10