Get text from dynamically created EditText on click of a button - android

I am creating a dynamic layout in which I have 2 EditText and one Button. On click of a button a new same king of layout is created. I want , when I click on the button I should be able to get data from edittexts and store them and then add new layout.I am not able to get data from edittext , it is coming up blank.
Below is my code . I am also adding screenshot of my layout
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
ll_newView = (LinearLayout)findViewById(R.id.ll_newView);
scrollView = (ScrollView)findViewById(R.id.scrollView);
bt_addView = (Button)findViewById(R.id.bt_addView);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
LinearLayout one = new LinearLayout(MainActivity2.this);
//=====First EditText======//
params.weight = 5.0f;
params.setMargins(16,8,8,16);
EditText et_one = new EditText(MainActivity2.this);
et_one.setPadding(10,10,10,10);
et_one.setHint("Edit Text 1");
et_one.setBackground(getResources().getDrawable(R.drawable.edit_text_border));
et_one.setLayoutParams(params);
one.addView(et_one);
//=====Second EditText======//
EditText et_two = new EditText(MainActivity2.this);
et_two.setPadding(10,10,10,10);
et_two.setHint("Edit Text 2");
et_two.setBackground(getResources().getDrawable(R.drawable.edit_text_border));
et_two.setLayoutParams(params);
one.addView(et_two);
Button bt_plus = new Button(MainActivity2.this);
bt_plus.setText("+");
one.addView(bt_plus);
ll_newView.addView(one);
bt_plus.setOnClickListener(getOnClickDoSomething(et_one.getText().toString().trim(),et_two.getText().toString().trim()));
}
View.OnClickListener getOnClickDoSomething(final String one1,final String two1) {
return new View.OnClickListener() {
public void onClick(View v) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
LinearLayout one = new LinearLayout(MainActivity2.this);
Log.e("========","=====one ="+one1+"=========two = "+two1);
//=====First EditText======//
params.weight = 5.0f;
params.setMargins(16,8,8,16);
EditText et_one = new EditText(MainActivity2.this);
et_one.setPadding(10,10,10,10);
et_one.setHint("Edit Text 1");
et_one.setBackground(getResources().getDrawable(R.drawable.edit_text_border));
et_one.setLayoutParams(params);
one.addView(et_one);
//=====Second EditText======//
EditText et_two = new EditText(MainActivity2.this);
et_two.setPadding(10,10,10,10);
et_two.setHint("Edit Text 2");
et_two.setBackground(getResources().getDrawable(R.drawable.edit_text_border));
et_two.setLayoutParams(params);
one.addView(et_two);
Button bt_plus = new Button(MainActivity2.this);
bt_plus.setText("+");
one.addView(bt_plus);
ll_newView.addView(one);
bt_plus.setOnClickListener(getOnClickDoSomething(et_one.getText().toString().trim(),et_two.getText().toString().trim()));
Log.e("========","=====one ="+et_one.getText().toString().trim()+"=========two = "+ et_two.getText().toString().trim());
}
};
}

You are passing to the getOnClickDoSomething function the text of the newly created editText, so understandably it will be blank. What you may want is to change getOnClickDoSomething, so that it accepts reference to the editTexts themselves, and later you can retrieve the their current text. (You will have to be careful not to leak views, as you have anonymous classes holding on to the Views.)
Example: Just change the following lines as follows
bt_plus.setOnClickListener(getOnClickDoSomething(et_one, et_two)); }
View.OnClickListener getOnClickDoSomething(final EditText one1,final EditText two1)
Log.e("========","=====one ="+one1.getText().toString().trim(),+"=========two = "+two1.getText().toString().trim());

Related

Android: Showing a toast message when the checkbox is checked for the dynamically created checkboxes

When an ImageButton is clicked a checkBox is created dynamically in my app.
The problem I'm facing is with toast messages not showing or let's say the listener I am using for the checkboxes to show a toast message when the checkbox is checked is working only for the current/latest checkbox that has been created. Not for the checkboxes that had been created previously.
Problem,
I saw that the problem could be with the variable (toDoCheckBox) I am using to save the newly created checkbox instance and every time it's getting overriding dynamically as I create newer checkboxes. So for the previous checkboxes, there is no reference pointing to them in order for the listener to work. (the problem could be this or not, i don't know for sure) Even if this is the problem i don't know how to solve it
What i tried is that,
creating the instance of checkbox on both onCreate as well as the createCheckBox method so that I could assign the listener in both the places.
on the onCreate()
on the createCheckbox()
Only the latest created checkbox works and shows the toast messages
Can someone help me or guide me with the problem,
I want the toast message to show for every checkboxes
i posted only the code that could be of some use
private CheckBox toDoCheckBox;
private EditText defaultEditText;
private List<CheckBox> listCheckBoxes;
private int previousViewId = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.defaultEditText = findViewById(R.id.defaultEditText);
toDoCheckBox = new CheckBox(this);
this.listCheckBoxes = new ArrayList<>();
this.checkBoxListener = new View.OnClickListener(){
#Override
public void onClick(View view){
if(toDoCheckBox.isChecked()){
toDoCheckBox.setChecked(true);
Toast.makeText(MainActivity.this,"You did a good work, keep it up!",Toast.LENGTH_SHORT).show();
}
}
};
toDoCheckBox.setOnClickListener(checkBoxListener);
}
public void createCheckBox() {
//CREATING A CHECKBOX
toDoCheckBox = new CheckBox(this);
toDoCheckBox.setText(defaultEditText.getText().toString());
toDoCheckBox.setTextSize(20);
toDoCheckBox.setId(View.generateViewId());
ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) defaultEditText.getLayoutParams();
ConstraintLayout.LayoutParams newParams = new ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.WRAP_CONTENT,
ConstraintLayout.LayoutParams.WRAP_CONTENT
);
newParams.width = params.width;
newParams.height = params.height;
//Constraints
newParams.startToStart = params.startToStart;
newParams.endToEnd = params.endToEnd;
newParams.topToTop = params.topToTop;
newParams.topToBottom = params.topToBottom;
//Margins
newParams.leftMargin = params.leftMargin;
newParams.topMargin = params.topMargin;
newParams.bottomMargin = params.bottomMargin;
newParams.rightMargin = params.rightMargin;
constraintLayout.addView(toDoCheckBox, -1, newParams);
previousViewId = toDoCheckBox.getId();
defaultEditText.setVisibility(View.INVISIBLE);
createEditText();
toDoCheckBox.setOnClickListener(checkBoxListener);
this.listCheckBoxes.add(toDoCheckBox);
totalCheckBox++;
}
Try to use the local checkbox object instead of the global toDoCheckBox.

Is there a way to change background color behind a button (button.setBackgroundResource)

I'm a new Android dev student, and I'm trying to create a dynamic layout witch contains a TextView and a button inside the same row.
but I have a little problem.
I set my Button in my Drawables ressources by
button.setBackgroundResource(R.drawable.ic_comment_black_48px);
and now, I cannot change the backgroundcolor behind it.
I have created a newLinearlayout inside my main LinearLayout and have created a new textView and a new Button.
I have put them inside the LinearLayout's child and put it inside the main.
That's work but not the background color behind my button.
is there a way to do that?
my xml layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/grey"
android:id="#+id/historyLayout">
</LinearLayout>
my complete activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
mLinearLayout = findViewById(R.id.historyLayout);
mMoodSaved = new ArrayList(7); // Define the max size of my ArrayList
loadData();
for (int i = 1; i <= 7; i++) {
final TextView textView = new TextView(this);
mLinearLyt = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
mLinearLyt.setOrientation(LinearLayout.HORIZONTAL);
textView.setHeight(300);
textView.setWidth(400);
textView.setBackgroundColor(Color.RED);
textView.setTextColor(Color.BLACK);
textView.setTextSize(12);
textView.setText(String.valueOf(i));
mLinearLyt.setBackgroundColor(Color.YELLOW);
mLinearLyt.addView(textView);
mLinearLyt.setLayoutParams(params);
ImageButton button = new ImageButton(this);
LinearLayout.LayoutParams param2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
param2.setMargins(20,50,0,0);
param2.height = 100;
param2.width = 100;
button.setLayoutParams(param2);
button.setBackgroundResource(R.drawable.ic_comment_black_48px);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
soundConfirm();
Toast.makeText(getApplicationContext(), textView.getText(), Toast.LENGTH_LONG).show(); //Display Toast Message
}
});
mLinearLyt.addView(button);
mLinearLayout.addView(mLinearLyt);
}
}
Since this an ImageButton, set
button.setImageResource(R.drawable.ic_comment_black_48px)
instead of setBackgroundResource.

Find child of a LinearLayout inside a LinearLayout

I have a LinearLayout ("ll") that is already created in xml and the app dynamically creates another LinearLayout inside of it and creates an EditText and a Button inside of that view. The button makes the whole LinearLayout destroy itself along with the EditText and Button inside it (the whole system is a player name entering activity). Anyway, I am trying to find a way to get the text from all of the EditTexts. I have tried using a for loop on "ll" and using ll.getChildAt() but I can't seem to use .getChildAt() on whatever ll.getChildAt() generates because getChildAt() generates a "View" not a "LinearLayout." I basically just need a way to search two children in, rather than just one. Also, if there is just a better way I should be doing this, let me know. I'm open to suggestions.
Here's my code if it will help:
NewGameCreate.java
public class NewGameCreate extends Activity {
int numOfPlayers = 0;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_game_create);
}
public void newPlayer(View view) {
numOfPlayers++;
final LinearLayout ll = findViewById(R.id.playerView);
final LinearLayout llNew = new LinearLayout(getApplicationContext());
llNew.setOrientation(LinearLayout.HORIZONTAL);
llNew.setId(numOfPlayers);
ll.addView(llNew);
EditText newName = new EditText(this);
newName.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1));
newName.setHint("Enter Player Name");
newName.setId(numOfPlayers);
newName.setWidth(0);
llNew.addView(newName);
final Button delete = new Button(this);
delete.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 0));
delete.setText("Delete");
delete.setId(numOfPlayers);
delete.setWidth(0);
delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int id = delete.getId();
ll.removeViewInLayout(findViewById(id));
Drawable back = ll.getBackground();
ll.setBackgroundColor(00000000);
ll.setBackground(back);
ll.invalidate();
}
});
llNew.addView(delete);
}
public void startGame(View view){
LinearLayout ll = findViewById(R.id.playerView);
List text = new ArrayList();
for(int loop = 0; loop < ll.getChildCount(); loop++) {
//this is the code in question and where I want to get the text from
//all my EditTexts
LinearLayout inner = ll.getChildAt(loop);
}
}
}
I think I found the answer to it. You need to change a little bit of code in the startGame() method I m providing the code for startGame below.
public void startGame(View view) {
LinearLayout ll = findViewById(R.id.playerView);
List text = new ArrayList();
for (int loop = 0; loop < ll.getChildCount(); loop++) {
//this is the code in question and where I want to get the text from
//all my EditTexts
LinearLayout inner = (LinearLayout) ll.getChildAt(loop);
for (int j = 0; j < inner.getChildCount(); j++) {
if (inner.getChildAt(j) instanceof EditText) {
EditText textET = (EditText) inner.getChildAt(j);
Log.d("TAG",textET.getText().toString());
}
}
}
}
In the above code you were able to get the first child only but as you have added a linearLayout with orientation Horizontal in a parent LinearLayout with orientation Vertical, you have written code for the child of parent layout i.e playerView. I have modified the code to get the elements of the child Linear layout and Log prints all the text from the EditText.
Hope that helps!!

Save dynamically created tablelayout on screen rotation

I am working on an app that uses 5 tabs and each tab has a different fragment. In all of these fragments I have from 1 up to 10 dynamically created tablelayouts and each one of them can have as many rows as the user wants. Each row may have up to 16 columns of EditText, Spinner, Checkbox. My problem is that when I rotate my phone I lose all the data inside these tablelayouts. My ViewPagerAdapter that hosts these fragments extends FragmentStatePagerAdapter. I am not losing any data outside the tables. I understand that when I rotate the phone they are recreated but I want to keep my data. I do not want to create a database because when I finish adding data in my app I want to save everything to a .txt file. This is code for one of my tables
table18 = (TableLayout) view.findViewById(R.id.blg_parameter18_table);
TableRow.LayoutParams tlparams18 = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
//Create header
TableRow table18HeaderRow = new TableRow(view.getContext());
table18HeaderRow.setLayoutParams(tlparams18);
TextView table18HeaderColumn0 = new TextView(view.getContext());
setTableHeaderView(table18HeaderRow, table18HeaderColumn0, R.string.blg_parameter18_0, tlparams18);
TextView table18HeaderColumn1 = new TextView(view.getContext());
setTableHeaderView(table18HeaderRow, table18HeaderColumn1, R.string.blg_parameter18_1, tlparams18);
TextView table18HeaderColumn2 = new TextView(view.getContext());
setTableHeaderView(table18HeaderRow, table18HeaderColumn2, R.string.blg_parameter18_2, tlparams18);
TextView table18HeaderColumn3 = new TextView(view.getContext());
setTableHeaderView(table18HeaderRow, table18HeaderColumn3, R.string.blg_parameter18_3, tlparams18);
TextView table18HeaderColumn4 = new TextView(view.getContext());
setTableHeaderView(table18HeaderRow, table18HeaderColumn4, R.string.blg_parameter18_4, tlparams18);
TextView table18HeaderColumn5 = new TextView(view.getContext());
setTableHeaderView(table18HeaderRow, table18HeaderColumn5, R.string.blg_parameter18_5, tlparams18);
TextView table18HeaderColumn6 = new TextView(view.getContext());
setTableHeaderView(table18HeaderRow, table18HeaderColumn6, R.string.blg_parameter18_6, tlparams18);
TextView table18HeaderColumn7 = new TextView(view.getContext());
setTableHeaderView(table18HeaderRow, table18HeaderColumn7, R.string.blg_parameter18_7, tlparams18);
TextView table18HeaderColumn8 = new TextView(view.getContext());
setTableHeaderView(table18HeaderRow, table18HeaderColumn8, R.string.blg_parameter18_8, tlparams18);
TextView table18HeaderColumn9 = new TextView(view.getContext());
setTableHeaderView(table18HeaderRow, table18HeaderColumn9, R.string.blg_parameter18_9, tlparams18);
table18.addView(table18HeaderRow);
createRowTable18(table18, table18NumOfRows, tlparams18);
public void createRowTable18(TableLayout table18, int numOfRows, TableRow.LayoutParams tlparams18) {
TableRow table18Row = new TableRow(table18.getContext());
table18Row.setLayoutParams(tlparams18);
table18Row.setId(numOfRows);
CheckBox table18RowColumn1 = new CheckBox(getContext());
CheckBox table18RowColumn2 = new CheckBox(getContext());
CheckBox table18RowColumn3 = new CheckBox(getContext());
CheckBox table18RowColumn4 = new CheckBox(getContext());
CheckBox table18RowColumn5 = new CheckBox(getContext());
CheckBox table18RowColumn6 = new CheckBox(getContext());
EditText table18RowColumn7 = new EditText(getContext());
final TextView table18RowColumn8 = new TextView(getContext());
EditText table18RowColumn9 = new EditText(getContext());
Spinner spinner18 = new Spinner(getContext());
ArrayAdapter<CharSequence> adapter18 = ArrayAdapter.createFromResource(getContext(),
R.array.blg_parameter18_1_array, android.R.layout.simple_spinner_item);
adapter18.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner18.setAdapter(adapter18);
table18Row.addView(spinner18);
spinner18.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selection = (String) parent.getItemAtPosition(position);
if (!TextUtils.isEmpty(selection)) {
if (selection.equals(getString(R.string.blg_parameter_18_1_0))) {
table18RowColumn8.setText(null);
} else if (selection.equals(getString(R.string.blg_parameter_18_1_a))) {
table18RowColumn8.setText("kWh");
} else if (selection.equals(getString(R.string.blg_parameter_18_1_b))) {
table18RowColumn8.setText("lt");
} else if (selection.equals(getString(R.string.blg_parameter_18_1_c))) {
table18RowColumn8.setText("lt");
} else if (selection.equals(getString(R.string.blg_parameter_18_1_d))) {
table18RowColumn8.setText("Nm^3");
} else if (selection.equals(getString(R.string.blg_parameter_18_1_e))) {
table18RowColumn8.setText("Nm^3");
} else if (selection.equals(getString(R.string.blg_parameter_18_1_f))) {
table18RowColumn8.setText("kg");
} else if (selection.equals(getString(R.string.blg_parameter_18_1_g))) {
table18RowColumn8.setText("kWh");
}
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
setTableRowCheckBox(table18Row, table18RowColumn1);
setTableRowCheckBox(table18Row, table18RowColumn2);
setTableRowCheckBox(table18Row, table18RowColumn3);
setTableRowCheckBox(table18Row, table18RowColumn4);
setTableRowCheckBox(table18Row, table18RowColumn5);
setTableRowCheckBox(table18Row, table18RowColumn6);
table18RowColumn7.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
setTableRowEditView(table18Row, table18RowColumn7, tlparams18);
setTableRowTextView(table18Row, table18RowColumn8, tlparams18);
table18RowColumn7.setInputType(InputType.TYPE_CLASS_DATETIME | InputType.TYPE_DATETIME_VARIATION_NORMAL);
setTableRowEditView(table18Row, table18RowColumn9, tlparams18);
table18.addView(table18Row);
}
public void addRow18() {
createRowTable18(table18, table18NumOfRows, tlparams);
table18NumOfRows++;
}
public void removeRow18() {
if (table18NumOfRows > 0) {
table18.removeViewAt(table18NumOfRows);
table18NumOfRows--;
} else {
Toast.makeText(getContext(), "Δεν υπάρχουν γραμμές στον πίνακα", Toast.LENGTH_SHORT).show();
}
}
When I complete inserting data they are too many to save and recall them one by one everytime i rotate the phone. Any ideas?
When your orientation changes, Android destroys your current activity and creates a new activity again, so you must manage your state.
You can use onSaveInstanceState to save the state and then get the same it in onCreate, for example:
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable("MyObject", myObject);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
final MyDataType myData = savedInstanceState.getString("MyObject"));
// fill your layout...
}
}
You can manage the state of your fragments too, please check here. Fragments can be "retain his instance" please read this or this.
Here another great post.
In some projects where I worked, when the application was not prepared for screen rotation, we had to disable this configuration.
I hope this will be useful for you.
Android will automatically save and restore your tables and the associated text views, but you need to do a couple of things.
Make sure each view you want to save has an id. Only views will an id will be saved. See generateViewId and setId.
Note: In order for the Android system to restore the state of the views in your activity, each view must have a unique ID, supplied by the android:id attribute.
Even with an id set, Android will not by default save the value of a TextView presumably because TextViews tend to be static. You can force a save/restore of a TextView by using freezesText.
android:freezesText
If set, the text view will include its current complete text inside of its frozen icicle in addition to meta-data such as the current cursor position. By default this is disabled; it can be useful when the contents of a text view is not stored in a persistent place such as a content provider. For EditText it is always enabled, regardless of the value of the attribute.
There may be some odds and ends to tend to, but that should be the bulk of the changes needed.

How to remove dynamically created views on button click

public void Add_text() {
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setId(i);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView product = new TextView(getActivity());
product.setText(" Product" + 5 + " ");
ll.addView(product);
EditText qty = new EditText(getActivity());
qty.setText(i + "");
qty.setId(i);
qty.setWidth(120);
ll.addView(qty);
Button btn = new Button(getActivity());
ll.addView(btn);
btn.setLayoutParams(params);
btn.setOnClickListener(o);
ly.addView(ll);
i++;
}
I wrote the above code to create the textfields and buttons dynamically; But now I need to remove 2 textfields and a button when the button is clicked. How do I do that?
Try following code.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
LinearLayout linearParent = (LinearLayout) v.getParent().getParent();
LinearLayout linearChild = (LinearLayout) v.getParent();
linearParent.removeView(linearChild);
}
});
Explanation
Here first take "GrandParent" of any view.
Then take its "Parent" view
With reference to "GrandParent" remove that "Parent" view.
this will remove all views which that "Parent" holds. As per your code, your "ll" will be "linearChild" here. And "ly" will be "linearParent" here. So whole "ll" will be removed from "ly" which you have added dynamically.
If you want to permanently remove the views you created.
OnClick(View view){
ly.removeAllViews()
}
If you do not want to permanently remove the views you created.
OnClick(View view){
ly.setVisibility(View.GONE); //This will hide the all views
qty.setVisibility(View.GONE);//This will hide the EditText qty
product .setVisibility(View.GONE);//This will hide the TextView product
}
So use appropriate code line which you want.
EDIT:
Use this code for your situation:
public void Add_text() {
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setId(i);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView product = new TextView(getActivity());
product.setText(" Product" + 5 + " ");
ll.addView(product);
EditText qty = new EditText(getActivity());
qty.setText(i + "");
qty.setId(i);
qty.setWidth(120);
ll.addView(qty);
Button btn = new Button(this);
ll.addView(btn);
btn.setLayoutParams(params);
ly.addView(ll);
i++;
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View button) {
qty.setVisibility(View.GONE);//This will hide the EditText qty
product .setVisibility(View.GONE);//This will hide the TextView product
}
});
}
i think you got to use this method on your LinearLayout :
public void removeView (View view)
first you call :
EditText et = (EditText)linearLayout.findViewById(yourEditText.getId());
then call the remove view method :
linearLayout.removeView (et) ;
and to remove all of the Views that are in the LinearLayout do the following :
public void removeAllViews ()
like the following :
linearLayout.removeAllViews()
and give me some feedback
Hope that Helps .
you can simply use qty.setVisibility(View.GONE) on the onClickListener() of the Button of your choice. like this.
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
qty.setVisibility(View.GONE); //for temporary purpose
//or u can also do this
layout.removeView(qty); //removes permanently
}
});
The benefit of using View.GONE is that you can get the View back if you want but layout.removeView(qty) will remove the view permanently and you have to re add the view again.
[EDIT] 1. changed to View.GONE instead of View.INVISIBLE because of reasons explained here
Hope I answered your question. :)
just use index for which you want to remove your view from linear layout
Linearlayout.removeViewAt();
if you want again that view then you can call addViewAt()in same way.
I hope it will help you.
Add this line
mLayout.removeViewAt(mLayout.getChildCount()-1);

Categories

Resources