LinearLayout
has a field and an "X" button of delete. When you press the "X" button it has to delete only the field that is on the side of it. But only get it to delete 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;
}
}
}