View background color android

400+ Android & Flutter Code

Example code for android + flutter app developers.

Saturday, January 3, 2015

How to change TextView background color in android

TextView display text on android application. android developer can change TextView background color both programmatically at run time in java file and statically by declarative syntax in xml layout file or xml xml resource file.

the following example code demonstrate us how can we set or change the TextView background color by declarative syntax. in this example we did not make any changes on java file, so we does not include java file here.

in this exampe code we include few TextView widgets. we define their background color by various way. the simple way to define TextView background color is its android:background attribute. we can assign a Hexadecimal color string for this attribute to generate TextView background color.

Hexadecimal color is specified with an RGB value and optional alpha channel. in this example code we uses the following hexadecimal color format to define Textview background color, those are #RGB, #ARGB, #RRGGBB and #AARRGGBB. in this hexadecimal color format ‘A’ describe alpha channel, ‘R’ describe red channel, ‘G’ describe green channel and ‘B’ describe blue channel.

Color is a simple resource which we can reference using the value provided in the name attribute not the name of xml file. so we can combine color resources with other simple resources in a single xml file. ech color resource is a name value pair. resource file saved on res/values folder.

Читайте также:  Хорошая читалка для планшета андроид

in this example we create a resource file to store color names. this xml resource file name is colors.xml and its located at res/values/colors.xml. we saved our preferred colors name and value in the xml file to use them on app widgets.

finally. we can define TextView background color from xml resource file by android:background attribute as android:background=»@color/AliceBlue». the specified TextView will display AliceBlue background on app.

Set TextView background color programmatically

Источник

Оцените статью