Dynamically add new views

Asked

Viewed 582 times

0

I have an Activity that contains the cardView of the image at the end of the question that has 3 edittext. I was wondering how I can do so that when I click on some button, a new cardview with the same layout is added again, so I can recover your data. I tried to follow the directions on this one question, but I was unsuccessful.

inserir a descrição da imagem aqui

The cardview is inside another Relativelayout and its layout is as follows:

 <android.support.v7.widget.CardView
android:id="@+id/infoContato"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/infoCadastro"
android:layout_marginTop="8dp">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="8dp">

    <RelativeLayout
        android:id="@+id/tituloInfoContato"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true">

        <ImageView
            android:id="@+id/imageViewIcTelefone"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_centerVertical="true"
            android:contentDescription="@string/dicaTelefone"
            android:src="@drawable/ic_phone" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="8dp"
            android:layout_toEndOf="@+id/imageViewIcTelefone"
            android:layout_toRightOf="@+id/imageViewIcTelefone"
            android:text="@string/lblInfoContato"
            android:textAppearance="?android:attr/textAppearanceSmall" />

    </RelativeLayout>

    <!-- Telefone -->
    <android.support.design.widget.TextInputLayout
        android:id="@+id/txtInputAddCadastroTelefone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/tituloInfoContato">

        <EditText
            android:id="@+id/edtaddTelefone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/dicaTelefone"
            android:inputType="phone"
            android:singleLine="true" />

    </android.support.design.widget.TextInputLayout>

    <!-- Celular -->
    <android.support.design.widget.TextInputLayout
        android:id="@+id/txtInputAddCadastroCelular"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/txtInputAddCadastroTelefone">

        <EditText
            android:id="@+id/edtaddCelular"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/dicaCelular"
            android:inputType="phone"
            android:singleLine="true" />

    </android.support.design.widget.TextInputLayout>

    <!-- Email -->
    <android.support.design.widget.TextInputLayout
        android:id="@+id/txtInputAddEmail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/txtInputAddCadastroCelular">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAllCaps="true"
            android:hint="@string/Email"
            android:inputType="textEmailAddress"
            android:singleLine="true" />

    </android.support.design.widget.TextInputLayout>

</RelativeLayout>

  • Could you post the code that you performed and didn’t work? I see that your layout will be much simpler if you change the RelativeLayout external by a LinearLayout with orientation="vertical", may even make it easier to add Views dynamically.

  • I tried this View view=LayoutInflater.from(getApplicationContext()).inflate(R.layout.bloco_cardview, null); RelativeLayout layout = (RelativeLayout)findViewById(R.id.layout_contato);&#xA;layout.addView(view);&#xA;

  • I redid the code using a LinearLayoutas the most external layout as you said, worked, now just need to figure out how to recover the data entered in edittext :)

  • 1

    In that case just keep the reference

  • I am seeing how to do this right now, if you have any tips thank you.

  • I think I got it, every time I add a new block, I add his edittexts into one ArrayList<EditText>, then to receive the amount recovered by Arraylist.

Show 1 more comment

1 answer

1

After receiving a great help from the user Wakin, he suggested me change my top layout for a RelativeLayout, what worked very well and then to recover the values of Edittext I created an Arraylist where every click I added Edittext to the list. Finally to recover the value of Edittext was simply to call the object inside the list.

The final code is as follows::

// Lista que recebe o editText
List<EditText> alleds = new ArrayList<EditText>;

@Override
public void onClick(View v) {

    // View que recebeu o layout do meu cardview
    View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.card_layout,null);

    // LinearLayout superior ao meu cardview que recebeu o layout
    linearLayout.addView(view);

    // Exemplo de um dos EditTexts do cardView
    EditText edtCelular = (EditText)view.findViewById(R.id.edtAddCelular);

    // Adicionando o editText à lista
    alleds.add(edtCelular);

    //Exibindo o valor do campo em um Toast
    Toast.makeText(this,alleds.get(0).getText().toString(),Toast.LENGTH_SHORT).show();

}

Browser other questions tagged

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