Delete Imagebutton in a Scrollview on Android

Asked

Viewed 34 times

0

We are developing a communication board for people with a certain degree of difficulty, to be able to communicate through a communication board. In case it might unintentionally add more than one image ( get duplicated ), then I would need to delete this last button.

As the use is for those who have some degree of difficulty, we thought to leave the delete function on a button ( as indicated in the image circled in red ), that when pressed, delete the last added Imagebutton.

Any idea how to do that? It’s added to a Linearlayout, a Scroolview.

Below is the code to add the button

//Função para Criar Botões
    protected void criaBotoes(final int idBotao, final int posicaoX, final int posicaoY, final int altura, final int largura, int tipoLayout, int tipoBotao, String pathArquivo, final int tagBotao) {
        AssetManager manager = getAssets(); //Pasta onde ficam os arquivos a serem carregados ( Imagens )

        //Carrega Imagens dinamicamente
        InputStream open = null;
        try {
            open = manager.open(pathArquivo);                 //Seta nome para carregar o arquivo
            Bitmap bitmap = BitmapFactory.decodeStream(open); //Seta objeto para controle de imagem

            //Cria Layout Params ( "Fatias no layout para adiocionar cada botão )
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

            //Seta posições e tamanho do Botão
            params.leftMargin = posicaoX;
            params.topMargin  = posicaoY;
            params.width      = altura;
            params.height     = largura;

            ImageButton botaoImg = new ImageButton(this); //Cria Botão Dinamicamente
            botaoImg.setId(idBotao);                      //Seta propriedade ID para cada usuário, para ser usado no "Click"
            botaoImg.setTag(tagBotao);                    //**
            botaoImg.setBackgroundResource(0);            //Deixa botão sem bordas

            botaoImg.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    int id  = v.getId();
                    int tag = (int) v.getTag();
                    //...
                }
            });

            botaoImg.setImageBitmap(bitmap);  //Seta objeto de imagem para o Botão
            botaoImg.setLayoutParams(params); //Seta layout para o botão ( "Fatias"  para cada botão )

            ll.addView(botaoImg); //Adiciona Botão no Layout (Linear)
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

criaBotoes(seqBotoesSel, posX, posY, altura, largura, 1, 1, pathAquivo, 0);  
sv.addView(ll); //adiciona botão na ScrollView

inserir a descrição da imagem aqui

  • misunderstand...

  • There is a saying that says: "It is better to be safe than sorry". So what you should do is to prevent a repeated button from being inserted. However, whether you want to prevent or remedy, the approach you are using to manage the buttons is not the most appropriate one to facilitate that option. Consider using a Listview/Recyclerview for this purpose.

  • Hi, thanks for the call... while I was out here for lunch, I thought about it, why not just check to not let it add duplicate?... this is easy, I can save the name of the last figure added, and if the next is equal I do not let add... until then quiet, but may have a situation that selected wrong ( like when we wrote and need to delete ), in this case I return to the previous situation, need to exclude the latter...

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.