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

Subselect to retrieve righthand-side of in-statement using CriteriaBuilder (JPA)
How can I find the android log file?
Should I instantiate a new Facebook variable for each activity, or use static Facebook variable
How do I return a (custom) SOAPFault from an Axis web service?
Is lazy=“false” the only solution for “org.hibernate.LazyInitializationException: could not initialize proxy – no Session”
How to solve “This element has no attached source and the Javadoc could not be found in the attached Javadoc”?
Problem, nothing is displayed while trying to read xml
high level java GUI framework [closed]
GZIPInputStream and Characterset
JVM and timezones
Setter for nested interface
VisualVM stack primitive types variable values when doing a thread dump
Singleton Object created for each request
Java string align to right
How do i compare values of BigInteger to be used as a condition in a loop?
In Android, how do I query MediaStore only for files in a specific path? Or alternatively, only display files in a certain path?

Categories

HOME
date
clips
gitlab
hive
magnific-popup
google-api-php-client
plot
vmware
cplex
sqlite-net-extensions
dxl
survey
opengl-es-2.0
azure-storage-tables
dacpac
spring-xd
zend-framework3
task
windows-server-2012
carthage
jsprit
seaborn
progressive-web-apps
tapestry
format-specifiers
django-storage
pingfederate
traffic
bootstrap-material-design
fgetcsv
tooltipster
filezilla
spring-mybatis
elasticsearch-net
replaceall
large-file-upload
google-cloud-nl
delicious-api
r-raster
socialengine
frame
galsim
framemaker
xmlreader
android-kernel
bower-install
http-digest
main
dosbox
dartium
multilingual
temporary-files
elasticsearch-plugin
unoconv
quadratic-programming
jvm-languages
eclipse-gef
xml-documentation
react-scripts
togetherjs
forever
dwscript
orthogonal
account-kit
cubes
medium.com
probability-density
kendonumerictextbox
msys
citrus-pay
security-testing
sailfish-os
apache-commons-fileupload
inmobi
dia
camanjs
network-interface
php-5.4
has-many-through
balanced-payments
stxxl
ms-project-server-2010
jquery-mobile-dialog
sublist
mt
javaspaces
dotnethighcharts
jquery-ui-layout
adk
subgurim-maps
joyent
createwindow
nhibernate.search
avatar
boost-smart-ptr
nerddinner
wsdl.exe

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App