java


Java android change color in xml file


I have this xml where I already set stroke color but I sometimes I want to change stoke color programmatically. The file name is dummy.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffffff" />
<stroke
android:width="1dp"
android:color="#ff000000" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
<corners
android:bottomLeftRadius="7dp"
android:bottomRightRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp" />
</shape>
I want to change color (stroke color) programmatically, how I can do this?
I use this xml here :
<LinearLayout
android:layout_marginTop="2dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/a"
android:layout_gravity="center"
android:background="#drawable/dummy"
android:layout_marginBottom="2sp"
android:orientation="horizontal"
android:padding="5dp">
<RelativeLayout
android:id="#+id/serverStatusWrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:background="#drawable/shadow_green"
android:padding="2dp">
<ImageView
android:id="#+id/serverStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:antialias="true"
android:src="#drawable/ic_settings_input_antenna_white_24dp" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/locationStatusWrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/shadow_red"
android:padding="2dp">
<ImageView
android:id="#+id/locationStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:antialias="true"
android:src="#drawable/ic_warning_white_24dp" />
</RelativeLayout>
</LinearLayout>
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.a);
You can change the stroke as
GradientDrawable drawable = (GradientDrawable)linearLayout.getBackground();
drawable.setStroke(3, Color.RED);
And can change solid color as
GradientDrawable drawable = (GradientDrawable)linearLayout.getBackground();
drawable.setColor(Color.RED);
Hope this helps
If you have drawable file for a "view" like this
Then you can change
a. Stroke color :
GradientDrawable drawable = (GradientDrawable)view.getBackground();
drawable.setStroke(3, Color.RED); // set stroke width and stroke color
b. Solid color :
GradientDrawable drawable = (GradientDrawable)view.getBackground();
drawable.setColor(Color.RED); // set solid color
If you have drawable file for a "view" like this
Then you can change the individual item attributes by taking separate drawable objects by there positions.
StateListDrawable drawable = (StateListDrawable)view.getBackground();
DrawableContainerState dcs = (DrawableContainerState)drawable.getConstantState();
Drawable[] drawableItems = dcs.getChildren();
GradientDrawable gradientDrawableChecked = (GradientDrawable)drawableItems[0]; // item 1
GradientDrawable gradientDrawableUnChecked = (GradientDrawable)drawableItems[1]; // item 2
now to change stroke or solid color :
//solid color
gradientDrawableChecked.setColor(Color.BLUE);
gradientDrawableUnChecked.setColor(Color.RED);
//stroke
gradientDrawableChecked.setStroke(1, Color.RED);
gradientDrawableUnChecked.setStroke(1, Color.BLUE);

Related Links

Why does getRemoteHost() return an IP address upon deploying the application?
How to use progress bar with Zip writer?
Overiding the colour and size of the SplitPane divider on JavaFX8 isn't working using CSS3
Can i store some string value into a text file in R.drawable folder in android
Wildcards in java Generics
Trigger browser back when cursor is not on a text input or area
Dynamically creating instance names?
Exception in thread main, out of bounds and parsing Date class
How to get the External/Public IP Address in android? [duplicate]
Simple cipher and I/O
How to load a Hibernate association along with the parent entity
Java re-assignment of an inherited field outside of a constructor
How to remove a particular word from a text that satisfies a condition? [closed]
Reflection to change value of a private static final field for unit testing
how to convert 2d byte[][] array (binary image array) to image java
gradle confusion dependency with duplicate sub-project names

Categories

HOME
openlayers
date
deployment
pivotal-cloud-foundry
opengl-es-2.0
node-notifier
synchronization
izpack
modelica
n-gram
flask-wtforms
ghc
systemc
angular-ui
reactcsstransitiongroup
code-review
dbext
physics-engine
fish
apache-commons-io
bootstrap-duallistbox
pingfederate
traffic
social-media
replaceall
jspresso
hybridauth
wallpaper
main
disassembling
opshub
temporary-files
fedex
grid.mvc
core-plot
nomethoderror
http-redirect
checkboxlist
python-c-api
typescript1.8
togetherjs
pearson
webdriverjs
pycaffe
android-fonts
angular-cache
moveit
bluemix-app-scan
tform
system.management
computer-algebra-systems
drawbitmap
cakephp-3.1
umbraco6
nsmutabledictionary
tablelayout
srs
uv-mapping
yui-compressor
coderush
ticoredatasync
eclipse-templates
php-parser
xfbml
radcombobox
fluent-interface
asp.net-mvc-areas
zend-translate
digest-authentication
forums
vc90
perfect-hash
webkit.net
javap
premature-optimization
private-members
wsdl.exe
lzh

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App