How do I remove a view created in java code by the id of this view?

Asked

Viewed 1,035 times

2

How do I remove a view by Id. Because in my application it has an "add" button to add a group of components that "group" is a LinearLayout has a field and a delete "X" button. When the "X" button is pressed it has to delete only the field that is on its side. But I could only get him to exclude the last group, because .removeView does not remove by the "Id" of the group. Can anyone help me if it is possible?

Xml code:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<LinearLayout        
    android:id="@+id/lnr"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="vertical" >

    <!-- Botão "+" para add campos -->
    <Button
        android:id="@+id/add"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_gravity="end"
        android:text="+"
        android:textSize="30sp" />

    <!-- Container -->
    <LinearLayout
        android:id="@+id/container"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >                     

    </LinearLayout>        

</LinearLayout>

Java code:

package com.example.teste;

import android.support.v7.app.Actionbaractivity;

import android.annotation.Suppresslint; import android.os.Bundle; import android.view.Menu; import android.view.Menuitem; import android.view.View; import android.view.View.Onclicklistener; import android.widget.Button; import android.widget.Edittext; import android.widget.Linearlayout;

@Suppresslint("Newapi") public class Mainactivity extends Actionbaractivity Implements Onclicklistener {

int id = 1;
Button add, del;
EditText campo;
LinearLayout grupo, container;

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

    add = (Button)findViewById(R.id.add);
    add.setOnClickListener(this);

    container = (LinearLayout)findViewById(R.id.container);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onClick(View v) {

    campo = new EditText(this);
    campo.setLayoutParams(new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT));
    campo.setText(" Campo  " + id + " ............");

    del = new Button(this);
    del.setLayoutParams(add.getLayoutParams());
    del.setText("X");
    del.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            container.removeView(grupo);
        }
    });

    grupo = new LinearLayout(this);
    grupo.setLayoutParams(new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT));

    if (v.getId() == R.id.add) {

        campo.setId(id);
        del.setId(id);
        grupo.setId(id);

        grupo.addView(campo);
        grupo.addView(del);

        container.addView(grupo);

        id = id + 1;

    }

}

}

1 answer

1


The problem of the code is to always use the same reference to the "group" object, to solve this just create a new object at each function activation.

Mainactivity.java

package com.example.teste;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

@SuppressLint("NewApi")
public class MainActivity extends ActionBarActivity implements OnClickListener {

private LinearLayout mContainer;
private int mIncrementalId = 1;
private Button mAddButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //Buscando container que recebe novas linhas
    setContentView(R.layout.activity_main);
    mContainer = (LinearLayout) findViewById(R.id.container);
    //Buscando botao para usar layout
    mAddButton = (Button) findViewById(R.id.add);
    mAddButton.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    //Criando novo grupo de linhas
    //Sempre usando um novo objeto para o grupo
    final LinearLayout novoGrupo = new LinearLayout(this);
    novoGrupo.setLayoutParams(new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT));

    //Criando novo campo de entrada de texto
    EditText novoCampo = new EditText(this);
    novoCampo.setLayoutParams(new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT));
    novoCampo.setText(" Campo  " + mIncrementalId + " ............");

    //Criando novo botao responsavel por remover o novo grupo
    Button delButton = new Button(this);
    delButton.setLayoutParams(mAddButton.getLayoutParams());
    delButton.setText("X");
    delButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            mContainer.removeView(novoGrupo);
        }
    });

    //Adicionando views ao grupo
    novoGrupo.addView(novoCampo);
    novoGrupo.addView(delButton);

    //Adicionando a view principal
    mContainer.addView(novoGrupo);

    //Incrementando id
    mIncrementalId++;


}

}

content_main.xml

<LinearLayout
    android:id="@+id/lnr"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <!-- Botão "+" para add campos -->
    <Button
        android:id="@+id/add"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_gravity="end"
        android:onClick="onClickAdd"
        android:text="+"
        android:textSize="30sp" />


    <!-- Container -->
    <LinearLayout
        android:id="@+id/container"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

    </LinearLayout>

</LinearLayout>

  • Valew thanks, recently a colleague suggested the use of tags that also worked, in which case he would remove by tag. Each time a group was created a tag would be set on the delete button "del.setTag(group);". And in the remove function "container.removeView((View) v.getTag());". I don’t know which is the best way, or which consumes the least processing resource. I think it would also be possible if I used a vector and removed it by the address of that vector, but I think it consumes more resources than the ones mentioned above.

Browser other questions tagged

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