tccpg288
tccpg288

Reputation: 3352

Add Defined TextInputLayout Dynamically and Programatically

I am trying to add a TextInputLayout with an EditText based on a number that the user selects from a Spinner. I already have the TextInputLayout attributes defined in XML and was hoping to simple just add them programmatically based on the number that the user selects from the spinner:

XML:

<FrameLayout
    android:background="@drawable/image_border"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight=".525">

    <Button
        android:id="@+id/add_image_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Click to Add Image" />
</FrameLayout>

<LinearLayout
    android:orientation="vertical"
    android:padding="4dp"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight=".475">

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <EditText
            android:id="@+id/create_poll_question_editText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:imeOptions="actionDone"
            android:singleLine="true"
            android:hint="@string/create_poll_question" />
    </android.support.design.widget.TextInputLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/how_many_answers_textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/how_many_answers_text"
            android:textColor="@color/black"
            android:textSize="16sp" />

        <Spinner
            android:id="@+id/number_of_answers_spinner"
            android:layout_width="wrap_content"
            android:layout_gravity="bottom"
            android:layout_height="24dp"
            android:background="@android:drawable/btn_dropdown" />

    </LinearLayout>

  <!--Want to Add Programatically -->
    <android.support.design.widget.TextInputLayout
        android:id="@+id/create_poll_answer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <EditText
            android:id="@+id/create_poll_answer_editText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:imeOptions="actionDone"
            android:singleLine="true"
            />
    </android.support.design.widget.TextInputLayout>

</LinearLayout>

Here is the code I am currently using, but it does not add dynamically based on the view I already created:

public class YourItemSelectedListener implements AdapterView.OnItemSelectedListener {

    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        String selected = parent.getItemAtPosition(pos).toString();
        Toast.makeText(getActivity().getApplicationContext(), selected, Toast.LENGTH_SHORT).show();
        for (int i = 0; i < Integer.parseInt(selected); i++) {
            ViewGroup layout = (ViewGroup) mRootView.findViewById(R.id.create_poll_linearlayout);
            EditText editText = new EditText(getActivity());
            editText.setHint("Poll Answer");
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            editText.setLayoutParams(layoutParams);
            TextInputLayout newAnswer = new TextInputLayout(getActivity());
            newAnswer.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            newAnswer.addView(editText, layoutParams);
            layout.addView(newAnswer);
        }

    }

Upvotes: 0

Views: 1299

Answers (2)

Saini
Saini

Reputation: 734

Add

android:id="@+id/create_poll_linearlayout"

to your root LinearLayout.

Upvotes: 1

Kushan
Kushan

Reputation: 5984

Easiest way to do such dynamic injections of view is to use ButterKnife library by JakeWharton

http://jakewharton.github.io/butterknife/

You can use it like in your activity's declaration part:

@BindView(R.id.title) TextInputLayout textInputLayout;

Then bind it inside onCreate() like:

ButterKnife.bind(this);

This will roughly be equivalent to inflating and finding the view by id.

Moreover, the library helps to dynamically set drawables,etc. with ease as well

Upvotes: 1

Related Questions