Add new fields dynamically

Asked

Viewed 1,017 times

9

I’m creating a app calculating credits from a school report.

DOUBT: How to add a new EditText when the user clicks on the "+" button next to it and sums up the values of each EditText.

inserir a descrição da imagem aqui

  • A very simple way would be to work with Listview, I created a Listviewadapter that contains the fields you want and add a new line every click on the button "+".

2 answers

7


Simple example of EditText with ListView:

XML - { main.xml }

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <Button
        android:id="@+id/btnAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:onClick="onClick"
        android:text="Add" />

    <ListView
        android:id="@+id/listViewMain"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:descendantFocusability="beforeDescendants"></ListView>
</LinearLayout>

XML - { item_edittext.xml }

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/ItemCaption"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="2dip"
        android:singleLine="true"></EditText>

</LinearLayout>

Java - { Main.class }

public class Main extends AppCompatActivity implements View.OnClickListener {
    private Button btnAdd;

    private ArrayList arrTemp = new ArrayList();

    private ArrayList array = new ArrayList();
    MyListAdapter myListAdapter;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btnAdd = (Button) findViewById(R.id.btnAdd);
        btnAdd.setOnClickListener(this);

        array.add("teste");

        arrTemp.add(array);

        myListAdapter = new MyListAdapter();
        ListView listView = (ListView) findViewById(R.id.listViewMain);
        listView.setAdapter(myListAdapter);

    }

    public void onClick(View view) {
        array.add("teste");
        myListAdapter.notifyDataSetChanged();
    }

    private class MyListAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            if (array != null && array.size() != 0) {
                return array.size();
            }
            return 0;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return array.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final ViewHolder holder;
            if (convertView == null) {

                holder = new ViewHolder();
                LayoutInflater inflater = Main.this.getLayoutInflater();
                convertView = inflater.inflate(R.layout.item_edittext, null);

                holder.editText1 = (EditText) convertView.findViewById(R.id.ItemCaption);

                convertView.setTag(holder);

            } else {

                holder = (ViewHolder) convertView.getTag();
            }

            holder.ref = position;

            return convertView;
        }

        private class ViewHolder {
            EditText editText1;
            int ref;
        }
    }
}
  • Is there a name for this function I asked for? Something I can dig deeper to understand the detailed workings?

  • 1

    I always search in English, so p/ this your case I would search in Google so: dynamically add edittext android ... one of the ways to do this dynamically is to actually put it in a list, because imagine if you put 30 edittext's, Then it would exceed your screen, unless you insert a scrollview, but it would not be good practice. Make it a habit to search in English, you will have more success in the response. Hugs. Vlw. Flw @Brunoassisdetassis

4

I believe you can add a new Botton, with addView.

Example: "linearLayout.addView(Botton);"

Example I have here:

View view = getActivity().getLayoutInflater().inflate(R.layout.scroll_crews, (ViewGroup) getView(), false);
LinearLayout linearLayout = (LinearLayout) getView().findViewById(R.id.scroll_crew_liner);
View layoutScroll = view.findViewById(R.id.scroll_crews_linearlayout);    
TextView textCrewJob = (TextView) view.findViewById(R.id.textCrewJob);    
TextView textCrewNome = (TextView) view.findViewById(R.id.textCrewNome);
ImageView imgPagerCrews = (ImageView) view.findViewById(R.id.imgPagerCrews);
ProgressBar progressBarCrew = (ProgressBar) view.findViewById(R.id.progressBarCrews);    
linearLayout.addView(layoutScroll);

In the example, I inflate an xml, and with it I edit what I need and after that I add it in another layout.

Where you need to add the Botton, from a findViewById() in the layout and with it, speaks a layout.addView();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.