Christian Temple
Christian Temple

Reputation: 131

unable to remove all the buttons in a linear layout deleteAllButtons doesn't work

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;
        }
    }
}

Upvotes: 1

Views: 37

Answers (1)

Ashish Ranjan
Ashish Ranjan

Reputation: 5543

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.

Upvotes: 1

Related Questions