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

LPC1347 USB4Java
OpenAS2 AS2ReceiverHandler: HTTP connection error on inbound message
How to read the Web Browser Console in Selenium?
Dandelion Datatables tag dt inside fragment
Spring HATEOAS ControllerLinkBuilder methodOn increasing response times significantly
Calling web service: javax.net.ssl.SSLException: Received fatal alert: protocol_version
Getting failed.org.hibernate.MappingException when trying to get a FactorySession
while loop iterating only once while reading from inputstream
We should use constants as descriptive variable name to initialize the default value of some field?
How does the prim's algorithm run when compared with Kruskal's algorithm in terms of efficiency? [closed]
Why do I get unidentified label? (Java/Android)
Angular with Spring Security Login and Authentication
Import excel to tableview (javafx)
access JComponents into another java non GUI class
Calculating the mean pixel value of an Image: Java
How to allow others to add custom enums to a class through an API

Categories

HOME
amazon-ec2
urbancode
oracle11g
minimum-spanning-tree
push-notification
hp-exstream
iot
platform-builder
objectgears
youtube-dl
getelementsbytagname
frameworks
jxls
ip
google-project-tango
cross-browser
ezpublish
opengl-es-2.0
medical
slurm
windows-10-universal
spring-xd
decimal
quartz-scheduler
dosgi
code-review
beyondcompare
nodatime
jprofiler
sox
devops
captiveportal
blazemeter
crosstab
one-to-many
javacv
fish
pepper
facet
react-css-modules
google-cloud-nl
skia
yadcf
data-manipulation
titanium-mobile
gtrendsr
perlin-noise
mapdb
web-mining
retina-display
multilingual
splice
eclipse-scout
elmah
google-closure
node-gyp
spring-security-kerberos
no-www
flashair
long-polling
user-accounts
azure-application-gateway
termination
zip4j
react-scripts
hendrix
celery-task
powercli
tropo
magma
windows-mobile-6.5
akka-cluster
spring-cache
firmata
url-masking
login-control
hill-climbing
qcustomplot
whois
pagerank
energy
execute
sniffer
c3
codeigniter-url
knuth
cctv
jquery-layout
htmlcleaner
kgdb
meteor-velocity
cloud-connect
jsctypes
tidy
html4
random-seed
flashvars
tridion-worldserver
ocx
gwt-ext
spyware
psi
invite
file-comparison
krl
sql-server-profiler
mongomapper
fluent-interface
.nettiers
bespin
fixed-width
multi-tier
zend-tool
microsoft-virtualization
backcolor
defensive-programming
paul-graham

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile