Forum Discussion

MarekFric's avatar
MarekFric
Occasional Contributor
9 years ago

Layout background color

Hi all,

 

Is it possible to get layout background color of android app ?

  • Layouts may not always have a solid background color, but when they do, here are two ways to get at it. If you have an instrumented app, you can use the layout getBackground().getColor() methods. getColor() returns an integer that includes the the alpha (amount of transparency) and the red/green/blue components of the color.

     

    I created a "Hello world" in Android Studio and changed the background color to a slightly dark red.

     

    redapp.png

     

    The color  is #CC0000 in hex, a slightly lower red intensity, no blue, and no green. Here's the script (I left out the path to the view, because it's quite long and doesn't help):

     

      var argb = layout.getBackground().getColor();

      // strip the alpha channel
      var rgb = argb & 0X00FFFFFF;

      // a hex string
      var hex = rgb.toString(16);

      // shift and mask to get color component values
      var R = ((rgb >> 16) & 0xFF);
      var G = ((rgb >> 8) & 0xFF);
      var B = ((rgb) & 0xFF);

     

    If you don't have an instrumented app, you can get a screenshot from the mobile Desktop object and get the color at any point in the image using the Pixels() method:

     

      var desktop = Mobile.Device().Desktop;
      var rgb2 = desktop.Picture().Pixels(desktop.Width / 2, desktop.Height / 2);

     

    The debugger local values look like this:

    The argb integer comes back from getColor(), rgb is an integer with the alpha channel removed, and R is an integer with the Red value. The 204 is equivalent to hex "cc".

     

    locals.png

     

    Note: getBackground() is in Android API 11+ 

     

    -Noel Rice

    Falafel Software

    http://blog.falafel.com/31-days-testcomplete-mobile/

  • Layouts may not always have a solid background color, but when they do, here are two ways to get at it. If you have an instrumented app, you can use the layout getBackground().getColor() methods. getColor() returns an integer that includes the the alpha (amount of transparency) and the red/green/blue components of the color.

     

    I created a "Hello world" in Android Studio and changed the background color to a slightly dark red.

     

    redapp.png

     

    The color  is #CC0000 in hex, a slightly lower red intensity, no blue, and no green. Here's the script (I left out the path to the view, because it's quite long and doesn't help):

     

      var argb = layout.getBackground().getColor();

      // strip the alpha channel
      var rgb = argb & 0X00FFFFFF;

      // a hex string
      var hex = rgb.toString(16);

      // shift and mask to get color component values
      var R = ((rgb >> 16) & 0xFF);
      var G = ((rgb >> 8) & 0xFF);
      var B = ((rgb) & 0xFF);

     

    If you don't have an instrumented app, you can get a screenshot from the mobile Desktop object and get the color at any point in the image using the Pixels() method:

     

      var desktop = Mobile.Device().Desktop;
      var rgb2 = desktop.Picture().Pixels(desktop.Width / 2, desktop.Height / 2);

     

    The debugger local values look like this:

    The argb integer comes back from getColor(), rgb is an integer with the alpha channel removed, and R is an integer with the Red value. The 204 is equivalent to hex "cc".

     

    locals.png

     

    Note: getBackground() is in Android API 11+ 

     

    -Noel Rice

    Falafel Software

    http://blog.falafel.com/31-days-testcomplete-mobile/

    • MarekFric's avatar
      MarekFric
      Occasional Contributor

      Wow. It works like a charm :smileyhappy:
      Thank you very much for your help!