0

Assuming I have two activities Activity A and Activity B. What I want is a code that when the Imageview on Activity B is clicked the alpha value of button in activity A changes and another Imageview in Activity A disappears. The codes for these two as far as I know is something like this

b2.getBackground().setAlpha(40);  //b2 is the button in Activity A
v2.setImageResource(android.R.color.transparent);  //v2 is the imageView in Activity A 

I don't know how to do this in different activity, so I looked up on net and found a similar question here however it changes the background image of activity not the properties

 Intent act2= new Intent(ResultActivity2.this,Lylevel1.class);
                    act2.putExtra("myImageResource",**b2.getBackground().setAlpha(40)**);
                    startActivity(act2);

the part in ** is giving me error, anyone who can help?

melissa
  • 375
  • 1
  • 8
  • 20

1 Answers1

0

Parameters of a method are always data. You can't pass functions in Java as a method parameter. So if you call a method inside putExtra, the return value will be set as an extra, not the function itself.

So instead of saying

act2.putExtra("myImageResource",**b2.getBackground().setAlpha(40)**);

you should better use something like

act2.putExtra("alphaForImageView", 40);
act2.putExtra("hideImageView", true);

And in your second activity, you can get the values from the intent like this:

int newAlpha = getIntent().getExtras().getInt("alphaForImageView");
b2.getBackground().setAlpha(newAlpha);

boolean hideImageView = getIntent().getExtras().getBoolean("hideImageView");
if (hideImageView) {
    v2.setVisibility(View.INVISIBLE);
}
Headcracker
  • 522
  • 4
  • 19
  • @FrançoisL. Please read my answer carefully, and not only the first two sentences. What I mean is that you can't pass a function and expect that the function itself will be passed as parameter, like you can do in Kotlin. – Headcracker Jan 24 '18 at 10:57
  • Thank you it worked , can you please explain what to do with imageview property as well? – melissa Jan 24 '18 at 20:02
  • 1
    That works similarly, see my edited answer. – Headcracker Jan 25 '18 at 07:25