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

spring security is remember me user authenticated by username
replacing certain characters in string in java
Android / Java: Show GUI on “off-screen” (Always-On Display…)
Solrj - cannot inherit from final class error due to HTTPSolrServer
Install4j: run batch file/ script file for rollback action
Preferences in Java - error 5
How to design a class hierarchy which gives information about the subclasses without instantiating it
Spring one error for multiple fields [duplicate]
Struts 2 <s:combo … />, <s:radio … />, <sx:datetimepicker …/>, … Error [duplicate]
Button that Plays Sound in Android Studio not Playing
Text to speak causing application to crash (Android studio)
Failed to resolve: com.mcxiaoke.volley:library:1.0.19 in Android Studio
Getting class addresses on reading a file java
Import Apache Commons Math library
How make a generic type in java?
Android - No error but failed to insert fragment

Categories

HOME
testng
deployment
webpack
image-processing
homebrew
android-4.4-kitkat
framework7
youtube-api-v3
v8
gps
retrofit
networkx
in-app-purchase
webrequest
medical
awesome-wm
adobe-analytics
etl
zebra-printers
msp430
hapi
django-admin
jsdoc
pugjs
reactcsstransitiongroup
firefox-webextensions
visual-composer
jprofiler
lldb
emulator
swiftlint
extjs5
crystal-reports-2010
sparse-matrix
status
kannel
virtualdub
filezilla
webkitspeechrecognition
primitive
nat
phonegap
exuberant-ctags
host
io-redirection
qwerty
event-driven
code-search-engine
bootstrapper
ensembles
hp-ux
pdf-reactor
g-code
fakeiteasy
keydown
sencha-touch-2.3
babel-core
multipeer-connectivity
aurelia-validation
csound
flutterwave
dwscript
nxt
ado.net-entity-data-model
django-debug-toolbar
player
firebaseui
persist
bluemix-app-scan
revolution-r
independentsoft
wapiti
freedesktop.org
lemon
collapse
cannon.js
qdialog
wordml
varargs
riak-cs
nsight
bigbluebutton
kineticjs
typekit
document-classification
project-planning
responsive-slides
mesa
imdbpy
tidy
jmapviewer
coverflow
ora-00911
mylyn
plasma
e4x
broken-links
gdata-api
manchester-syntax
pysimplesoap
armcc
buildr
amazon-appstore
custom-backend
transactionscope
mod-auth
blackberry-playbook
hamachi
xtype
sql-server-profiler
digest-authentication
.nettiers
thunderbird-lightning
meego
thread-local-storage
zune
paul-graham





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