8

How can I change the font color, size and button color from a Java file in Eclipse, not from an XML file?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sheraz amin
  • 1,149
  • 2
  • 14
  • 23

4 Answers4

10

For font size you can write it as

tv.setTextSize(20.0f);

For font color you can write it as

tv.setTextColor(Color.BLUE);

For button background color you can write it as

btn.setBackgroundColor(Color.CYAN);
Lavanya
  • 3,903
  • 6
  • 31
  • 57
  • can i use the same code for spinner ??? It is working fine for button but not for spinner as i think the spinner's text is changeable and changes after selection .i hope u understand what i want to say... – sheraz amin Aug 23 '11 at 08:52
  • well have a look at this... i just googled and i found this... I think it is possible to change for spinner items too. Please take a look at this http://stackoverflow.com/questions/4989817/set-the-textsize-to-a-text-in-spinner-in-android-programatically – Lavanya Aug 23 '11 at 09:13
5
button.setTextColor(Color.WHITE);
button.setBackgroundColor(Color.BLUE);
Onuray Sahin
  • 4,093
  • 4
  • 33
  • 34
  • can i use the same code for spinner ??? It is working fine for button but not for spinner as i think the spinner's text is changeable and changes after selection .i hope u understand what i want to say... – sheraz amin Aug 23 '11 at 08:01
  • You can refer to this question: http://stackoverflow.com/questions/4880413/text-color-of-a-closed-spinner – Onuray Sahin Aug 23 '11 at 12:32
2

In the "Inherited XML Attributes" paragraph of the official documentation you can see either XML attributes (that you can change from XML) or related methods (which you can use in your Java code).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Andrei Buneyeu
  • 6,662
  • 6
  • 35
  • 38
1

Others already posted answers on how to do it in Java, but I guess these could help you as well.

How to do it in XML:

Community
  • 1
  • 1
corecase
  • 1,278
  • 5
  • 18
  • 29