user2844485
user2844485

Reputation: 1192

Create edittext view dynamically like Google Keep

I'm trying to create dynamically generated EditText views like the Google Keep app so when the user starts to enter text in one edit text view, a new blank one is generated below it.

I have a RecyclerView set up with an adapter that will contain the edit text views.

What's the best way to approach this?

Upvotes: 0

Views: 730

Answers (2)

Arma
Arma

Reputation: 152

In your MainActivity.java:

package com.junglesofts.strsend;

import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.os.Bundle;

public class MainActivity extends ActionBarActivity {

    public int i = 1;

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

        TableLayout ll = (TableLayout) findViewById(R.id.tblMain);
        View mTableRow = null;
        mTableRow = (TableRow) View.inflate(getApplicationContext(), R.layout.mrowrayout, null);
        EditText txtNew = (EditText)mTableRow.findViewById(R.id.txtNew);
        txtNew.setId(i);
        txtNew.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                ++i;
                TableLayout ll = (TableLayout) findViewById(R.id.tblMain);
                View mTableRow = null;
                mTableRow = (TableRow) View.inflate(getApplicationContext(), R.layout.mrowrayout, null);
                EditText txtNew = (EditText)mTableRow.findViewById(R.id.txtNew);
                txtNew.setId(i);
                txtNew.setOnClickListener(this);
                mTableRow.setTag(i);
                ll.addView(mTableRow);
            }
        });

        mTableRow.setTag(i);
        ll.addView(mTableRow);
    }
}

In your activity_main.xml:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/tblMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.junglesofts.strsend.MainActivity">



</TableLayout>

And in layout folder, create a new layout and rename it to mrowrayout.xml and in it:

<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayoutRow"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <EditText android:id="@+id/txtNew"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter your text!"/>

</TableRow>

I tested it and it works for me.

Upvotes: 1

Phillip Dunley
Phillip Dunley

Reputation: 103

The best way to do this is to have a TextView and below the textView have a list view.

Everytime the user enters something on the TextView and presses save, add the new entry to the arrayList which you are using to populate the ListView. Then refresh the ListView. That should do the trick.

Upvotes: 3

Related Questions