1

I wrote the following application where a 3 by 4 grid of buttons displayed and user can change the grid dimensions by clicking menu items. The problem is that when the user clicks on one of the items the previous buttons do not go away, the "deleteAllButtons" function doesn't seem to work. Can anyone tell me why they are not going away and how I can fix this issue please? Thank you.

package com.example.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {

    private static final int MENU_ITEM_ITEM1 = 1;
    LinearLayout.LayoutParams params;
    LinearLayout linearLayout;
    int _row;
    int column;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        linearLayout = new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.VERTICAL);  //Can also be done in xml by android:orientation="vertical"
        params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
        params.weight = 1.0f;
        params.gravity = Gravity.TOP;
        //layout.setBackgroundColor(0xFFFFFFFF);
        _row=3;
        column=4;
        update();
    }

    public void deleteAllButtons(){
        for (int i = 0; i < _row; i++){
            for (int j = 0; j < column; j++){
                linearLayout.removeView(findViewById(j + 1 + (i * column)));
            }
        }

    }

    public void update(){
        for (int i = 0; i < _row; i++) {
            LinearLayout row = new LinearLayout(this);
            row.setLayoutParams(params);

            for (int j = 0; j < column; j++) {
                Button btnTag = new Button(this);
                btnTag.setLayoutParams(params);
                btnTag.setText("Button " + (j + 1 + (i * column)));
                btnTag.setId(j + 1 + (i * column));
                if ((i+j) % 2 == 0) {
                    btnTag.setBackgroundColor(0xFFFF0000);
                } else {
                    btnTag.setBackgroundColor(0x00000000);
                }
                btnTag.setBackgroundResource(R.drawable.ic_android_black_24dp);
                row.addView(btnTag);
            }
            linearLayout.addView(row);
        }

        setContentView(linearLayout);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(Menu.NONE, MENU_ITEM_ITEM1, Menu.NONE, "Item name");
        menu.add(Menu.NONE, 2, Menu.NONE, "Item name");
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case MENU_ITEM_ITEM1:
                _row=4;
                column=5;
                deleteAllButtons();
                update();
                return true;
            case 2:
                _row=6;
                column=3;
                deleteAllButtons();
                update();
            default:
                return false;
        }
    }
}
Christian Temple
  • 121
  • 1
  • 1
  • 7

1 Answers1

1

You're updating the _row and column values in onOptionsItemSelected before calling deleteAllButtons, but you're using these values to delete the buttons in the deleteAllButtons function, update it to this:

switch (item.getItemId()) {
    case MENU_ITEM_ITEM1:
        deleteAllButtons();
        _row=4;
        column=5;
        update();
        return true;
    case 2:
        deleteAllButtons();
        _row=6;
        column=3;
        update();
    default:
        return false;
}

UPDATE

Looks like you've only buttons in the LinearLayout and you want to remove all of them, you can do this :

public void deleteAllButtons(){
    linearlayout.removeAllViews();
}

instead of looping and deleting them individually.

Ashish Ranjan
  • 5,523
  • 2
  • 18
  • 39
  • looks like you've multiple bugs, let me have a look at the code again. – Ashish Ranjan Nov 02 '17 at 16:45
  • Your updated answer solves the second part of my question, thanks. However, I also want to listen to your opinion on the first part: Even without the deleteallbuttons function or your suggested modification, new buttons should take over some or all of the old buttons' ids, since each id can only be associated with one object. Therefore, even in this case, the old buttons should go away but what happens is that all of the old buttons are preserved and new buttons are all generated right below them. How could that possibly be? Are there anything I'm missing? – Christian Temple Nov 02 '17 at 21:16
  • each id is not associated with only one view, you can use same id for any no. views, but in that case findviewbyid will return you the first view with the given id – Ashish Ranjan Nov 02 '17 at 21:21
  • read [this](https://stackoverflow.com/questions/1714297/android-view-setidint-id-programmatically-how-to-avoid-id-conflicts) for more information on it. – Ashish Ranjan Nov 02 '17 at 21:25