user3717434
user3717434

Reputation: 225

Adding TextInputLayout and EditText dynamically (binding)

I want to add a new EditText within a TextInputLayout just like how they are added statically in the xml file. (So at the beggining there are two TextInputLayouts and when user press the button it should add new ones). I saw other questions and i know it's about parent layout, but i can't make it work. By the way i'm using binding. In my case, parent should be ScrollView, i think. For the EditText i tried both RelativeLayout and LinearLayout but launches error in any case.

Here the xml file.

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/scrollView">

        <RelativeLayout
            android:id="@+id/activity_multi_opt_poll"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin"
            tools:context="com.xyz.MyApp.EntryActivity">

            <android.support.design.widget.TextInputLayout
                android:id="@+id/nameInputLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Name">

                <android.support.design.widget.TextInputEditText
                    android:id="@+id/nameEditText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
            </android.support.design.widget.TextInputLayout>

            <android.support.design.widget.TextInputLayout
                android:id="@+id/questionInputLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/nameInputLayout"
                android:hint="Question">

                <android.support.design.widget.TextInputEditText
                    android:id="@+id/questionEditText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
            </android.support.design.widget.TextInputLayout>

            <android.support.design.widget.TextInputLayout
                android:id="@+id/opt1InputLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/questionInputLayout"
                android:hint="1. Option">

                <android.support.design.widget.TextInputEditText
                    android:id="@+id/opt1EditText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
            </android.support.design.widget.TextInputLayout>


            <android.support.design.widget.TextInputLayout
                android:id="@+id/opt2InputLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/opt1InputLayout"
                android:hint="2. Option">

                <android.support.design.widget.TextInputEditText
                    android:id="@+id/opt2EditText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
            </android.support.design.widget.TextInputLayout>

            <android.support.design.widget.FloatingActionButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:clickable="true"
                app:fabSize="normal"
                app:srcCompat="@android:drawable/ic_input_add"
                android:id="@+id/addFAB"
                android:layout_marginEnd="21dp"
                android:layout_below="@+id/opt2InputLayout"
                android:layout_alignParentEnd="true" />
        </RelativeLayout> 
    </ScrollView>
</layout>

And the function to add new

public class EntryActivity extends AppCompatActivity {
    private ActivityEntryBinding binding;
    private List optionsViews = new ArrayList();
    private int count = 3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_entry);


        binding.addFAB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                binding.scrollView.addView(createNewOptionEntry());

            }
        });
    }

    private TextInputLayout createNewOptionEntry() {

        ScrollView.LayoutParams lparams = new ScrollView.LayoutParams(
                ScrollView.LayoutParams.MATCH_PARENT, ScrollView.LayoutParams.MATCH_PARENT);

        TextInputLayout textInputLayout = new TextInputLayout(this);
        textInputLayout.setLayoutParams(lparams);
        textInputLayout.setHint(count++ + ". Option");


        LinearLayout.LayoutParams lparams2 = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        final TextInputEditText editText = new TextInputEditText(this);
        editText.setLayoutParams(lparams2);
        int id = View.generateViewId();
        /*to be able to get dynamically generated editText's input*/
        optionsViews.add(id);
        editText.setId(id);

        textInputLayout.addView(editText, lparams);

        return textInputLayout;
    }


}

at line textInputLayout.addView(editText, lparams) gives me error:

 E/AndroidRuntime: FATAL EXCEPTION: main
 Process: mattoncino.pollo, PID: 23572
 java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams
   at android.support.design.widget.TextInputLayout.updateInputLayoutMargins(TextInputLayout.java:371)
   at android.support.design.widget.TextInputLayout.addView(TextInputLayout.java:271)
   at android.view.ViewGroup.addView(ViewGroup.java:3540)
   at mattoncino.pollo.MultiOptPollActivity.createNewOptionEntry(MultiOptPollActivity.java:95)
   at mattoncino.pollo.MultiOptPollActivity.access$000(MultiOptPollActivity.java:27)
   at mattoncino.pollo.MultiOptPollActivity$1.onClick(MultiOptPollActivity.java:48)
   at android.view.View.performClick(View.java:4463)
   at android.view.View$PerformClick.run(View.java:18770)
   at android.os.Handler.handleCallback(Handler.java:808)
   at android.os.Handler.dispatchMessage(Handler.java:103)
   at android.os.Looper.loop(Looper.java:193)
   at android.app.ActivityThread.main(ActivityThread.java:5333)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:515)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
   at dalvik.system.NativeStart.main(Native Method)

Upvotes: 0

Views: 1670

Answers (1)

user3717434
user3717434

Reputation: 225

Ok, now i think i resolved the problem with layouts. 1)TextInputLayout's parent has FrameLayout and i get it from the parent. 2)EditText's parent is TextnputLayout which is a subclass of LinearLayout, so EditText should set its layout as LinearLayout. I think this part is ok. But now it gives me error saying that ScrollView can host only one direct child. So i should add it to the RelativeLayout actually?

    private TextInputLayout createNewOptionEntry() {

        View parent = (View) binding.opt1InputLayout.getParent();

        FrameLayout.LayoutParams fLayout  = (FrameLayout.LayoutParams) parent.getLayoutParams();

        TextInputLayout textInputLayout = new TextInputLayout(this);
        textInputLayout.setLayoutParams(fLayout);
        textInputLayout.setHint(count++ + ". Option");

        LinearLayout.LayoutParams lLayout = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        final TextInputEditText editText = new TextInputEditText(this);
        editText.setLayoutParams(lLayout);
        int id = View.generateViewId();
        optionsViews.add(id);
        editText.setId(id);

        textInputLayout.addView(editText);

        return textInputLayout;
    }

Upvotes: 0

Related Questions