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;
}
}
}
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.
– Gustavo Almeida Cavalcante