like said in the title I’d like to change a button’s backgroun (using setBackgroundResource() ), but when I do that, I can’t see the text I’m puting on it (with setText() ), I found similar subject, but only with xml anwser. I need to do this in code…
Thank’
Class Code:
public class CityButton extends Button {
private double ratioX;
private double ratioY;
private City city;
public CityButton(Context context, City city, double ratioX, double ratioY) {
super(context);
this.city = city;
Rect boundRect = this.getBackground().getBounds();
this.setHeight(boundRect.height());
this.setWidth(boundRect.height());
this.ratioX = ratioX;
this.ratioY = ratioY;
}
public void addReinforcements(int value) {
city.addReinforcements(value);
reinforcementView.setText("" + city.getReinforcements());
}
public void setOffset(int posX, int posY) {
int offsetX = (int) (posX * this.ratioX) - 5;
int offsetY = (int) (posY * this.ratioY) - 5;
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(this.getLayoutParams());
params.setMargins(offsetX, offsetY, offsetX + this.getWidth(), offsetY + this.getHeight());
this.setLayoutParams(params);
}
I think
setBackgroundResourcemay have the side effect of losing your offset settings. Try to callsetOffseteach time after you change background: