1. Are you ready for the Galaxy S20? Here is everything we know so far!

Trying to change the colour of a resource

Discussion in 'Android Development' started by Jim Calvert, Aug 16, 2017.

  1. Jim Calvert

    Jim Calvert Lurker
    Thread Starter

    Hello everyone. This is my First post and I am a real Android newbie.
    But I have coded in various other platforms for 50+ years.

    My device is a Samsung A5 but that is irrelevant.

    I am working with a sample APP on Github called AccelerometerPlay that has 5 small white balls running around the screen with the level of your device. Very simple, I have it compiled and running nicely on my phone.

    Here is my frustrating problem: I have searched for hours for a way to programmatically change the colours of the balls from white to other colours.
    The balls begin as a PNG resource as below:

    class ParticleSystem {
    static final int NUM_PARTICLES = 6;
    private Particle mBalls[] = new Particle[NUM_PARTICLES];
    private Particle mBallX;

    ParticleSystem() {
    /*
    * Initially our particles have no speed or acceleration
    */
    for (int i = 0; i < mBalls.length; i++) {
    mBalls = new Particle(getContext());
    mBalls.setBackgroundResource(R.drawable.ball); <<<<< I want to change the colour about here.
    mBalls.setLayerType(LAYER_TYPE_HARDWARE, null);
    addView(mBalls, new ViewGroup.LayoutParams(mDstWidth, mDstHeight));
    }
    }
    They are in an array of drawables in a Particle system.
    BUT, I cannot find any method anywhere that will change their colours.
    And, a couple of times I thought I did find one but could not find the IMPORT to bring it into the APP>

    Does anyone have a clue where to look.
    Many thanks.
    Jim Calvert
     



    1. Download the Forums for Android™ app!


      Download

       
  2. Jim Calvert

    Jim Calvert Lurker
    Thread Starter

    LV426, thanks for your two links.
    Unfortunately they are much help for my situation.
    And again, like so many other sample codes, there are NO Import statements .

    I am having a heck of a time finding what Import will provide the various methods in question.
    Is there a scientific way to figure this out ??
    I am beginning to slowly learn that changing the color of anything in Android is tough and inflexible and usually dependent on dynamically frozen XML code.

    Thanks
     
  3. Deleted User

    Deleted User Guest

    You don't have to manually work out what imports are needed by your code. Android Studio will do it automatically as you type in the code. I never ever have to type one single import statement when I'm writing code. And if you press CTRL+ALT+O it will manually trigger an organise import.
     
Loading...

Share This Page

Loading...