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

Android - How to make an Activity for each item in a ListView and pass Java Object to next screen
Groovy make methods visible from one script to another
assign null default value for optional query param in route - Play Framework
ClassNotFoundExeption when embedding applet in webpage
JAVA servlet get result from database return to client
java.lang.NullPointer Exception in getResources().getString(R.string.value);
How to get an iPOJO component instance configuration?
Java Bouncing Ball Wall
Read an Excel file (saved in several blobs) sequentially into one InputStream
Change cursor to hand over certain co-ordinates in a JFrame
Create Index File(TOC) for merged pdf using itext library in java
Adding AspectJ to pom.xml changed Java version with Maven, why?
How to replace a text in regex?
How private constructors achieve publication safety in Java
HotSwaping code into “mvnDebug tomcat:run”
Java: packaging class files

Categories

HOME
hook
omnet++
mfc
nullpointerexception
dot
webstorm
yarn
paw-app
rdf
echarts
basic
gnupg
kentor-authservices
izpack
dax
collectd
vb.net-2010
hapi
percona
pc
google-static-maps
pugjs
invantive-sql
nhibernate-envers
scichart
functional-testing
buildbot
facebook-access-token
web-api-testing
opentype
tasklet
newline
bpel
fabric8
key-value-observing
galsim
pdb
siesta-swift
http-get
column-family
xmlreader
fedex
http-live-streaming
jsch
user-accounts
upstart
komodoedit
pango
pubmed
git-diff
ifs
pillow
pdfclown
mako
color-profile
color-picker
grails-tomcat-plugin
firebaseui
ready-api
captivenetwork
maven-tomcat-plugin
prerequisites
ora-00900
sframe
composite
angular-local-storage
truevault
clicktag
gulp-less
html-helper
project-planning
oracle-warehouse-builder
xsockets.net
excel-2003
buster.js
delphi-6
mhtml
browser-detection
newtonscript
appconkit
netdna-api
mod-auth
psi
radcombobox
h.323
sustainable-pace
private-members

Resources

Encrypt Message