Change XML shape color while app is running

chadtango

New Member
Is it possible to change rectangle (drawn in xml) color in Java code while app is running?My rectangle.xml:\[code\]<?xml version="1.0" encoding="UTF-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listview_background_shape"> <stroke android:width="2dp" android:color="#ffffff" /> <padding android:left="20dp" android:top="20dp" android:right="20dp" android:bottom="20dp" /> <solid android:color="#006600" /></shape>\[/code\]Drawn in main.xml by:\[code\]<View android:id="@+id/myRectangleView" android:layout_width="wrap_content" android:layout_height="100dp" android:background="@drawable/rectangle"/>\[/code\]I've tried this way:\[code\] GradientDrawable sd; View viewrectangle; viewrectangle = (View) findViewById(R.id.myRectangleView); sd = (GradientDrawable) viewrectangle.getBackground(); sd.setColor(0xffffff00); sd.invalidateSelf();\[/code\]It only works when I put it inside OnCreate method.I want to change rect color by a button, so I put this code inside button's onClick() method. But when I click button while app is running color doesn't change. Any suggestions?
 
Back
Top