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
Related
I'm trying to make my first app which has a set of 15 buttons. when you press a button the color changes between two numbers.Then if you press a specificity different "commit" button the buttons won't change colors any more.
My question right now is how would I be able to iterate through the buttons on the screen? I believe I need a assign the buttons a "name", "type", or something like it, and then find all instances where that happens but I cannot find the relevant getter/setter methods.
Here is my code so far:
public void clickHandler(View view) {
Button btn = (Button) view;
int id = btn.getId();
boolean clicked = isChosen(btn);
if( clicked == true && id != 1) {
btn.setBackgroundColor(-3355444);
}
else{
btn.setBackgroundColor(-16777216);
}
}
public boolean isChosen(Button btn){
ColorDrawable buttonColor = (ColorDrawable) btn.getBackground();
int colorId = buttonColor.getColor();
if(colorId == -16777216){
return true;
}
else return false;
}
public boolean player = true;
public void changeTurn(View view) {
TextView t = (TextView) findViewById(R.id.textView3);
if(player == false) {
t.setText("Player 2's turn");
player = true;
}
else{
t.setText("Player 1's turn");
player = false;
}
Hope this piece of code helps
final int buttonCount = 15;
LinearLayout mLayout = (LinearLayout) findViewById(R.id.pager);
for (int i = 0; i < buttonCount; i++) {
TextView textView = new TextView(this);
textView.setTag(String.valueOf(i));
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int buttonID = Integer.valueOf(view.getTag().toString());
updateColorForButtonAtPosition(buttonCount);
}
});
mLayout.addView(textView);
}
private void updateColorForButtonAtPosition (int buttonCount){
// add your functionality here
}
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()) {
}
}
}
});
}
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()){
}
}
}
});
}
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() {}
}
I have a array which I get from another Activity. I want to display it in the current Activity as CheckBox items and then have ability to check some of them and delete by button clicking. I implement it next way:
public void addFiles()
{
LinearLayout layout = (LinearLayout) findViewById(R.id.filesList);
if(!FileManagerActivity.finalAttachFiles.isEmpty())
{
for (int i=0; i<FileManagerActivity.finalAttachFiles.size();i++)
{
View line = new View(this);
line.setLayoutParams(new LayoutParams(1, LayoutParams.MATCH_PARENT));
line.setBackgroundColor(0xAA345556);
informationView= new CheckBox(this);
informationView.setTextColor(Color.BLACK);
informationView.setTextSize(16);
informationView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
informationView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.file_icon, 0, 0, 0);
informationView.setText(FileManagerActivity.finalAttachFiles.get(i).getName().toString());
informationView.setId(i);
layout.addView(informationView, 0);
layout.addView(line, 1);
layout.postInvalidate();
}
}
}
, and call it in the onCreate() method. it can be added successfully. Then I get items which were checked like this:
btnFilesRemove.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
LinearLayout layout = (LinearLayout) findViewById(R.id.filesList);
View v = null;
for(int i=0; i<FileManagerActivity.finalAttachFiles.size(); i++) {
v = layout.getChildAt(i);
CheckBox checkBox = (CheckBox) findViewById(i);
boolean checked=checkBox.isChecked();
if (checked)
{
Log.i("was checked",checkBox.getText().toString());
ViewGroup parent = (ViewGroup) v.getParent();
parent.removeView(v);
FileManagerActivity.finalAttachFiles.remove(i);
Log.i("this is view,baby",v.toString());
}
}
}
});
But items can't be deleted. And I'm thinking - what is better - to implement it in this way or maybe it will be more efficient with ListView using. I mean to create ArrayAdapter e.t.c..