Remove the Button from the LinearLayout - android

I am creating a CheckBox list and Button from ArrayList<Integer>, which I am getting as response from the server, programmatically. For updating purpose I am trying to delete the CheckBox list as well as the Button before I add the new one. With the current state the Button is not being deleted I noticed that when the ArrayList becomes empty the Button was not deleted.
How can I delete the Button in the case the table is empty since too there is some cases where the table becomes empty?
#Override
public void onAsyncTaskFinished(ArrayList<Integer> result) {
remove_elements();
createCheckboxList(result);
}
private void remove_elements() {
LinearLayout parent = (LinearLayout) findViewById(R.id.lila);
for (int i : items) {
CheckBox ch = (CheckBox) findViewById(i);
if (ch != null) {
parent.removeView(ch);
} else {
System.out.println("The checkbox is null");
}
}
Button btn = (Button) findViewById(1);
if (btn != null) {
parent.removeView(btn);
} else {
System.out.println("The button is null");
}
}
private void createCheckboxList(final ArrayList<Integer> items) {
this.items = items;
final ArrayList<Integer> selected = new ArrayList<Integer>();
LinearLayout ll = (LinearLayout) findViewById(R.id.lila);
for (int i = 0; i < items.size(); i++) {
CheckBox cb = new CheckBox(this);
cb.setText(String.valueOf(items.get(i)));
cb.setId(items.get(i));
ll.addView(cb);
}
Button btn = new Button(this);
btn.setLayoutParams(new LinearLayout.LayoutParams(500, 150));
btn.setText("submit");
btn.setId(1);
ll.addView(btn);
btn.setOnClickListener(new View.OnClickListener() {}
}

Only create the button if items.size() > 0
if (items.size() > 0){
Button btn = new Button(this);
btn.setLayoutParams(new LinearLayout.LayoutParams(500, 150));
btn.setText("submit");
btn.setId(1);
ll.addView(btn);
btn.setOnClickListener(new View.OnClickListener() {}
}

Related

How can get dynamic button text from another dynamic button OnClickListener event

I want get second dynamic button text from another dynamic button OnClickListener event:
Here is define some dynamic buutons:
LinearLayout lv=(LinearLayout)findViewById(R.id.lv);
for (int k = 1; k <= str[0].length(); k++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(100, 100);
btnTopEn = new Button(this);
btnTopEn.setId(k);
final int id_ = btnTopEn.getId();
btnTopEn.setText(" ");
lv.addView(btnTopEn, params);
btnTopEn = ((Button) findViewById(id_));
final Button finalBtnT = btnTopEn;
btnTopEn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
finalBtnT.setText("");
}
});
}
Now I want text of second button from OnClickListener Event:
TableLayout layout = (TableLayout)findViewById(R.id.TableL);
String stt="RZCEADHPTAUJTSFR";
int l=0;
for (int f=0; f<=1; f++) {
TableRow tr = new TableRow(this);
for (int c=0; c<=7; c++) {
btnCEn = new Button (this);
String ss=(String.valueOf(stt.charAt(l)));
btnCEn.setText(ss);;
final Button finalBtnB = btnCEn;
btnCEn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int m=2;
btnTopEn = ((Button) findViewById(m));
final Button finalBtnT = btnTopEn;
if (finalBtnT.getText().equals("")) {
String stGetText=finalBtnB.getText().toString();
finalBtnT.setText(stGetText);
break;
}
}
}
});
TableRow.LayoutParams lp = new TableRow.LayoutParams(100,100);
tr.addView(btnCEn, lp);
}
layout.addView(tr);
}
I wrote some code in OnClickListener event but none happen!
What is the value of str in the first loop ?
Also you are setting the text to a space .
btnTopEn.setText(" ");
And while checking you check for empty :
if (finalBtnT.getText().equals("")){
}
Try changing to
if (finalBtnT.getText().toString().trim().equals("")){
}

Set the visibility to GONE

I am creating a checkbox list with a button programmatically. For updating purpose I need to delete the old checkbox list and Button before creating the new one in the remove_elements method. How can I set their visibility to GONE in the remove_elements method? With the current state any checkbox list and button is always being added without deleting the old one.
code in the MainActivity:
ArrayList<Integer> items = new ArrayList<Integer>();
LinearLayout ll;
.
.
.
.
#Override
public void onAsyncTaskFinished(ArrayList<Integer> result) {
remove_elements();
createCheckboxList(result);
}
private void remove_elements() {
for (int i : items) {
CheckBox ch = (CheckBox) findViewById(i);
if (ch != null) {
ch.setVisibility(View.GONE);
} else {
System.out.println("The checkbox is null");
}
}
Button btn = (Button) findViewById(1);
if (btn != null) {
btn.setVisibility(View.GONE);
} else {
System.out.println("The button is null");
}
}
private void createCheckboxList(final ArrayList<Integer> items) {
this.items = items;
final ArrayList<Integer> selected = new ArrayList<Integer>();
ll = (LinearLayout) findViewById(R.id.lila);
for (int i = 0; i < items.size(); i++) {
CheckBox cb = new CheckBox(this);
cb.setText(String.valueOf(items.get(i)));
cb.setId(items.get(i));
ll.addView(cb);
}
Button btn = new Button(this);
btn.setLayoutParams(new LinearLayout.LayoutParams(500, 150));
btn.setText("submit");
btn.setId(1);
ll.addView(btn);
btn.setOnClickListener(new View.OnClickListener() {
}
}
As I recall the setVisibility-method only changes the way the element is shown. View.GONE means it won't take up any space in the layout but it will still be there.
To remove it completely you'll have to first get the parent layout which contains the elements, in your example I'm guessing it is the one with the id R.id.lila, and then call removeView.
private void remove_elements() {
LinearLayout parent = (LinearLayout) findViewById(R.id.lila);
for (int i : items) {
CheckBox ch = (CheckBox) findViewById(i);
if (ch != null) {
parent.removeView(ch);
} else {
System.out.println("The checkbox is null");
}
}
There is also a removeAllViews()-method you could use if the checkboxes are the only views in the LinearLayout

Android: The method getId() in the type View is not applicable for the arguments (int)

I am trying to access the cb in the if statement but I am getting cb cant be resolved
I have tried declare Checkbox cb as class variable but I am getting The method getId() in the type View is not applicable for the arguments (int).
I tried to declare it as method local variable like final CheckBox cb; but I am getting two errors: The first one The final local variable cb may already have been assigned at this line cb = new CheckBox(this); and the second one The method getId() in the type View is not applicable for the arguments (int)
how can I fix that?
private void createCheckboxList(final ArrayList<Integer> items) {
//final CheckBox cb;
final LinearLayout ll = (LinearLayout) findViewById(R.id.lila);
for (int i = 0; i < items.size(); i++) {
CheckBox cb = new CheckBox(this);
cb.setText(String.valueOf(items.get(i)));
cb.setId(i);
ll.addView(cb);
}
Button btn = new Button(this);
btn.setLayoutParams(new LinearLayout.LayoutParams(500, 150));
btn.setText("submit");
ll.addView(btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i : items) {
if (cb.getId(i).isChecked()) {
}
}
}
});
}
The reference cb wont exist outside the 'for' loop. Since you know its id you can create a new checkbox reference and use findviewById(); to get refer to the same checkbox
cb.getId() returns and integer not a checkbox reference
final LinearLayout ll = (LinearLayout) findViewById(R.id.lila);
for (int i = 0; i < items.size(); i++) {
final CheckBox cb = new CheckBox(this);
cb.setText(String.valueOf(items.get(i)));
cb.setId(i);
ll.addView(cb);
}
Button btn = new Button(this);
btn.setLayoutParams(new LinearLayout.LayoutParams(500, 150));
btn.setText("submit");
ll.addView(btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i < items.size()) { \\
Checkbox ch=(Checkbox) findViewById(i); \\
if (ch.isChecked()) {
}
}
}
});
}

Get the value of the checkboxes after clicking the transmit button

I have checkbox list and I am trying to get the result after the user selected multiple chexkboxes and clicked the transmit button
how can I get the multiple checkboxes values after clicking the trasmit button?
I appreciate any help.
private void createRadioButton(final ArrayList<Integer> items) {
final LinearLayout ll = (LinearLayout) findViewById(R.id.lila);
for (int i = 0; i < items.size(); i++) {
CheckBox cb = new CheckBox(this);
cb.setText(items.get(i) + "");
cb.setId(i);
ll.addView(cb);
}
Button btn = new Button(this);
btn.setLayoutParams(new LinearLayout.LayoutParams(500, 150));
btn.setText("submit");
ll.addView(btn);
}
Edit 1:
private void createRadioButton(final ArrayList items) {
final LinearLayout ll = (LinearLayout) findViewById(R.id.lila);
for (int i = 0; i < items.size(); i++) {
CheckBox cb = new CheckBox(this);
cb.setText(items.get(i) + "");
cb.setId(i);
ll.addView(cb);
}
Button btn = new Button(this);
btn.setLayoutParams(new LinearLayout.LayoutParams(500, 150));
btn.setText("submit");
ll.addView(btn);
//here I am getting this error: OnClickListener cannot be resolved to a type
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
for(int i: items){
//here I am getting `cb cannot be resolved`
if(cb.getId(i).isChecked()){
}
}
}
});
}

Dynamically create button using data in array and setOnClicklistener

im trying to add the OnClickListener to the button that it generated by the value in array, but m fail to do so, any suggestion here?
long lprice = Long.parseLong(searchId.getText().toString());
List<String> buttonNameOne = dbc.getItemNameRbPrice(lprice);
List<String> buttonPriceOne = dbc.getPriceRbPrice(lprice);
List<String> buttonDateOne = dbc.getCurrentDateRbPrice(lprice);
for(int i = 0 ; i < buttonNameOne.size() ; i ++)
{
Button btn = new Button(this);
btn.setId(2000+i);
btn.setText(buttonNameOne.get(i) + i);
linearButton.addView(btn, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Log.v("Value of element "+i, buttonNameOne.get(i));
Log.v("Value of element "+i, buttonPriceOne.get(i));
Log.v("Value of element "+i, buttonDateOne.get(i));
}
What you want to do is add the btn.setOnClickListener
for(int i = 0; i < buttonnameOne.size(); i++)
{
Button btn = new Button(this);
btn.setId(2000+i);
btn.setText(buttonNameOne.get(i) + i));
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(view v) {
// TODO Whatever you want onclick to do
}
});
//Log stuff
linearButton.addView(btn, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}

Categories

Resources