0

Can a property such as First Color be set using code?

I would like to do something like this:

btnMyButton.drawable = "StatelistDrawable"
btnMyButton.drawable.EnabledDrawable = "GradientDrawable"
btnMyButton.drawable.EnabledDrawable.firstcolor = "255, 199, 199"
btnMyButton.drawable.EnabledDrawable.secondcolor = "255, 79, 79"
CharlesB
  • 86,532
  • 28
  • 194
  • 218
Emad-ud-deen
  • 4,734
  • 21
  • 87
  • 152

1 Answers1

1

If I understand your question correctly you need to create a GradientDrawable as below:

GradientDrawable gradient = new GradientDrawable( GradientDrawable.Orientation.TOP_BOTTOM, new int[] {0xFFRRGGBB,0xFFRRGGBB}); gd.setCornerRadius(0f);

Where RRGGBB is the color code in hex (eg 99CC00)

And then set the drawable as the background of your button:

btnMyButton.setBackgroundDrawable(gradient);

Leyths
  • 71
  • 4
  • Thank you for the sample code. I will try it later. Do you know if Basic4Android has a decimal to Hex function I can use? Something like decToHex(99CC00) – Emad-ud-deen Nov 09 '11 at 19:03
  • Ah I didn't realise you were using the Basic4Android framework, the sample code above may not work unless you can include snippets of Android SDK Java code in your application. As for converting between decimals and hex, [this](http://www.roseindia.net/java/java-conversion/DecimalToHexadecimal.shtml) page should help. Apologies if my code isn't useful. – Leyths Nov 09 '11 at 19:46
  • Don't worry. I will see if it works anyway most likely tomorrow and will let you know what happens. Thanks. – Emad-ud-deen Nov 09 '11 at 22:24
  • I think I need to wait until my skills have developed with Basic4Android to see how to embed java into the coding. Thanks for trying anyway. :-) – Emad-ud-deen Nov 10 '11 at 11:58
  • You cannot directly embed Java into Basic4android code. You can however compile it into a custom library and reference that. – agraham Nov 10 '11 at 19:23
  • Thanks agraham, When I get further into the "Getting Started" manual I'm hoping there is a tutorial on how to do that. – Emad-ud-deen Nov 13 '11 at 09:53