Add new views dynamically

0

I have an activity that contains the cardview of the image at the end of the question that has 3 edittext. I wanted to know how I can do that when I click on any button, a new cardview with the same layout is added again, so I can retrieve your data. I tried to follow the other question , but I did not succeed.

ThecardviewisinsideanotherRelativeLayoutanditslayoutisasfollows:

<android.support.v7.widget.CardViewandroid: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>

    
asked by anonymous 01.02.2016 / 20:38

1 answer

1

After receiving great help from the user Wakin , he suggested changing my top layout to a RelativeLayout , which worked very well and then to retrieve the values of EditText I created an ArrayList where at each click I added EditText to the list. Finally to recover the value of EditText was to simply call the object inside the list.

The final code looks like this:

// 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();

}
    
02.02.2016 / 02:15