Reputation: 147
Is it possible to change rectangle (drawn in xml) color in Java code while app is running?
My rectangle.xml:
<?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>
Drawn in main.xml by:
<View
android:id="@+id/myRectangleView"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:background="@drawable/rectangle"/>
I've tried this way:
GradientDrawable sd;
View viewrectangle;
viewrectangle = (View) findViewById(R.id.myRectangleView);
sd = (GradientDrawable) viewrectangle.getBackground();
sd.setColor(0xffffff00);
sd.invalidateSelf();
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?
Upvotes: 0
Views: 2144
Reputation: 16393
You could try a color filter. I've used it before to change the color of buttons (note that they started out as standard gray), if you start with another color it could be a very different outcome. Anyway, an example of how I did it:
Import the PorterDuff graphics stuff:
import android.graphics.PorterDuff;
In the class define the item you want to color filter and set the filter:
Button atdButton = (Button) convertView.findViewById(R.id.attendbutton);
if (atdState[position].equals("P")) {
atdButton.getBackground().setColorFilter(0xFF00FF00, // Set filter to green
PorterDuff.Mode.MULTIPLY);
} else if (atdState[position].equals("T")) {
atdButton.getBackground().setColorFilter(0xFFFFFF00, // Set filter to yellow
PorterDuff.Mode.MULTIPLY);
} else if (atdState[position].equals("E")) {
atdButton.getBackground().setColorFilter(0xFFFF6600, // Set filter to orange
PorterDuff.Mode.MULTIPLY);
} else if (atdState[position].equals("U")) {
atdButton.getBackground().setColorFilter(0xFFFF0000, // Set filter to red
PorterDuff.Mode.MULTIPLY);
} else {
atdButton.getBackground().clearColorFilter();
}
Upvotes: 0
Reputation: 2360
Used this code and it worked, alternatively consider redrawing the viewrectangle using viewrectangle.invalidate(), but it shouldn't be nescarry:
View viewrectangle;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
viewrectangle = (View) findViewById(R.id.myRectangleView);
}
public void doClick(View v) {
GradientDrawable sd = (GradientDrawable) viewrectangle.getBackground();
sd.setColor(0xffffff00);
sd.invalidateSelf();
}
In this example the "doClick()" method is set in the main.xml:
<Button android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="Button"
android:onClick="doClick"/>
Upvotes: 2
Reputation: 7071
You can put this code in a separate method, and that method you can call from onClick of button..
Upvotes: 0