Show or hide an image in Android Studio

Asked

Viewed 455 times

1

I’m making an app that reads a value in case (on or off).

Based on this value it returns an image. If the value was "lit", the application would show me an image of a lamp on, and if the value was "erased", the application would show me an image of a lamp off.

  • Put a [mcve]. Any questions read [Ask] or then do our [tour].

3 answers

3

In xml, work component properties:


To make visible

  android:visibility="visible" :

To keep only the image space occupied in xml, but invisible:

 android:visibility="invisible"

To remove the visibility of the occupied space and image:

android:visibility="gone"

In java code, call the setVisibility method:

To make visible:

imageView.setVisibility(View.VISIBLE);

To keep only the image space occupied in xml, but invisible:

imageView.setVisibility(View.INVISIBLE);

To remove the visibility of the occupied space and image:

 imageView.setVisibility(View.GONE);
  • 1

    well complete, that’s right...

1

This is a matter of programming logic.
To make an image visible use:

imageView.setVisibility(View.VISIBLE);

To make an image invisible use:

imageView.setVisibility(View.GONE);

0

I think I understand your question.

They are two images of a lamp side by side. When one is lit the other has to be off. If that’s right then see the code below.

//Declare as variáveis;

private ImageView imgLampada1;

private ImageView imgLampada2;

//Recupere os ids no métodos onCreate da activity e configure as imagens das lampadas.

//Você também já deve ter as suas duas imagens (lampada_acesa.img e lampada_apagada.img) na sua pasta drawable.

imgLampada1 = findViewById(R.id.imgLampada1);

imgLampada2 = findViewById(R.id.imgLampada2);

//Configure as imagens nos ImageViews

imgLampada1.setImageResourse(R.drawable.lampada_acesa.img);

imgLampada2.setImageResourse(R.drawable.lampada_apagada.img);

//Crie os eventos de clicks dos dois imageViews acima.

imgLampada1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Quando clicakado apaga imgLampada1 e acende imgLampada2.

imgLampada1.setImageResourse(R.drawable.lampada_apagada.img);

imgLampada2.setImageResourse(R.drawable.lampada_acesa.img);


            }
        });


imgLampada1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Quando clicakado apaga imgLampada2 e acende imgLampada1.

imgLampada1.setImageResourse(R.drawable.lampada_acesa.img);

imgLampada2.setImageResourse(R.drawable.lampada_apagada.img);


            }
        });

I hope that somehow this helps you. If not, you can help someone else and get schooled too.

Browser other questions tagged

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