-2

I am just trying to do some basic coding. I have a button which when clicked I want it to change the tint color of a drawable image I have. But I am not sure how I can change the tint when the button is pressed. Any help would be most welcome!

@Override 
protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


    Button myYellow_Button = (Button) findViewById(R.id.Yellow_Button);


        myYellow_Button.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {


                box.setBackground(colorPrimary);

            }


}

I am not sure how to reference the XML script to change the Tint, I have experimented with SetBackground but I haven't got anywhere with it.

Satan Pandeya
  • 3,747
  • 4
  • 27
  • 53
Ravon
  • 11
  • 3

1 Answers1

0

You need to get a binding to your ImageView by using myImageView = (ImageView) findViewById(R.id.myImageViewId);.

Then, in the onClick() method your button click listener, you need to do something like : myImageView.setColorFilter(getContext().getResources().getColor(R.color.whatevercolor));.

Another solution is to have 2 versions of your drawable, with the 2 colors you want, and change the ImageView.Image field dynamically :

myImageView.setImage(new Image(R.drawable.yourSecondDrawable));
Maxime Flament
  • 721
  • 1
  • 7
  • 24
  • I managed to do the solution number 2 already, but I want to give the user more options that just pre-saved images. which is why I want there to be a color spectrum available once I figure out how to connect the button to the xml; – Ravon May 18 '17 at 20:07
  • Be aware that using solution number 2 will make your app slower and bigger (in storage), because you manipulate images instead of color values. – Maxime Flament May 18 '17 at 20:36
  • Hi Maxime I agree, but it was a good place to start and build on. – Ravon May 22 '17 at 07:43
  • Maxime when I try the code you posted I get an issue with the 'set.ColorTint' and the 'yellow', it is not recognised, here is the code I have: final ImageView box = (ImageView) findViewById(R.id.Test_Box); Button myYellow_Button = (Button) findViewById(R.id.Yellow_Button); myYellow_Button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { box.setColorTint(getResources().getString(R.color.yellow)); } }); – Ravon May 22 '17 at 07:47
  • Thanks Maxine, in the end I used the following, but only after fiddling with the code you posted; – Ravon May 23 '17 at 22:45
  • box.setColorFilter(getResources().getColor(android.R.color.white)); – Ravon May 23 '17 at 22:45
  • but I think the getColor is old code...but it works ! :) – Ravon May 23 '17 at 22:46
  • Yes it's deprecated. Use ResourceCompat.getColor(getContext(), R.color.yourcolor) instead – Maxime Flament May 24 '17 at 08:10
  • This doesn't work I think it needs something after the getContext() as it does not recognise the the symbol variable R after it. also do I need to put 'android' in there for it to recognise the color white? – Ravon May 25 '17 at 07:16