RedaLe
RedaLe

Reputation: 59

How to change an ImageView visibility from a different activity after pressing a button?

I'm developing a simple android game divided into levels. I want a check icon (ImageView) to appear next to a level button (on level select menu) when that level is completed. A level is completed after pressing a button, as follows (InsideLevelActivity):

    final EditText level1editText=(EditText)findViewById(R.id.level1editText);        
    Button level1completeButton=(Button)findViewById(R.id.level1completeButton);

    level1completeButton.setOnClickListener(new View.OnClickListener()
        public void onClick(View v)
        {
            final String edittext=level1editText.getText().toString();

            if(edittext.trim().equals("Complete level"))
             {   

              {
                Intent visible1 = new Intent();
                visible1.putExtra("iconvisible",0);
                startActivity(visible1);

                    {
                      LayoutInflater inflater = getLayoutInflater();
                      View view = inflater.inflate(R.layout.activity_level1completed,
                      (ViewGroup) findViewById(R.id.img11_toast));

                Toast toast = new Toast(Level1Activity.this);
                toast.setView(view);
                toast.show();

                { onBackPressed(); {

                        return;
                    }


            }   

            }
            else
            {
                    Toast.makeText(Level1Activity.this,
                    "Type Complete level.", Toast.LENGTH_LONG).show();
            }

And then returns to level select menu activity. I'm trying to retrieve data this way (LevelMenuActivity):

ImageView logocheckicon1=(ImageView)findViewById(R.id.logocheckicon1);
logocheckicon1.setVisibility(View.GONE);

Intent visible1 = getIntent();
int state = Integer.parseInt(visible1.getExtras().get("iconvisible").toString());
complete1.setVisibility(iconvisible);

I've tried many approaches for the last couple of days, including this one (how to pass data between two activities). I've even tried to make the check icon (ImageView) invisible, and making it visible again this way.

Also, the same check icon will appear next to every completed level. Is it possible to acomplish this with only one ImageView (without creating 10 different IDs of the same drawable)?

Thank you in advance.

EDIT: I apologize if i wasn't clear enough. I tought there was some way to change the visibility of an image located, for instance, on MainActivity with an intent inside the button on another activity. Thank you for your answers.

EDIT2: Added the code of another unsuccessful try.

Upvotes: 2

Views: 1858

Answers (2)

Chamath
Chamath

Reputation: 249

You can pass the image through intents. First convert your image to a byte array and send it with the intent.

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);     
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); 
byte[] b = baos.toByteArray();

Intent intent = new Intent(this, ActivityB.class);
intent.putExtra("picture", b);
startActivity(intent);

then you can retrieve this image from the next avtivity.

Bundle extras = getIntent().getExtras();
byte[] b = extras.getByteArray("picture");

Bitmap bmp = BitmapFactory.decodeByteArray(b, 0, b.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);

image.setImageBitmap(bmp);

Hope this will help you.

Upvotes: 1

Md Abdul Gafur
Md Abdul Gafur

Reputation: 6201

To Pass image from one activity to another activity. At First convert image into Bitmap then base64 then convert string then pass it via intent or save share-preference.

  public boolean saveImage(Context context, Bitmap realImage) {

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
         realImage.compress(Bitmap.CompressFormat.JPEG, 100, baos);   
         byte[] b = baos.toByteArray(); 

        String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);

    }

Then take this image in another activity via intent or get from share-preference

public Bitmap getFacebookImageBitmap(Context context)
{
     Bitmap bitmap = null;

     String saveimage=from intent or share-preference string.
     if( !saveimage.equalsIgnoreCase("") ){
            byte[] b = Base64.decode(saveimage, Base64.DEFAULT);
             bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);

        }
    return bitmap;
}

Thanks

Upvotes: 1

Related Questions