How to set BackgroundTintList of a button to default? - java

I'm changing the BackgroundTintList property of my button with the following line.
myButton.setBackgroundTintList(getColorStateList(R.color.green));
As a result my Button changes it's color from grey to green, and this is what I'd like to achieve.
My problem is that later on I'd like to set back the original grey color of the button, but I have no idea how to do it. I have tried to get the BackgroundTintList property of the button at the very beginning of my code (before I change it) but the following line returns NULL
ColorStateList buttonBackgroundTint = myButton.getBackgroundTintList();
Once I have set the BackgroundTintList to green, setting it to NULL changes my button to white and not to its default grey.
What would be the way to set my button to grey again?

You can try this line:
myButton.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#d8d8d8")));
if you want to change the button color back to its default/original color.

I haven't found any way to do this easily. The only way I could accomplish your goal was to hold on to the original background Drawable, create a clone of it, manually tint the clone, and then swap back and forth between these new drawables.
private Drawable original;
private Drawable tinted;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
this.original = button.getBackground();
this.tinted = button.getBackground().getConstantState().newDrawable().mutate();
ColorStateList custom = getResources().getColorStateList(R.color.my_button, getTheme());
tinted.setTintList(custom);
...
}
Then later on I can either write button.setBackground(original) or button.setBackground(tinted) to swap between the two.

I just created a new Button and get backgroundTintList
actionSearch.backgroundTintList = MaterialButton(requireContext()).backgroundTintList

if you want add custom color
view.backgroundTintList = ColorStateList.valueOf(getColor(context, R.color.color)

Related

Set Background Color Temporarily on Button

Does anybody know how to set the background color temporarily on a Button.
buttons[randomI][randomJ].setBackgroundColor(Color.rgb(155, 17, 30));
Here is what I have but I only want to set the background color of this for a certain period of time. I understand that a way to go about this is to remove the background color after a certain period of time but I don't know how to remove background color. I referenced: How to get JButton default background color? and the solutions did not work for me.
In the top answer:
btn.setBackground(new JButton().getBackground());
JButton does not exist for me and using new Button().getBackground says that it can not resolve constructor. So is there any way to temporarily set the background color?
Store the previous color of the Button:
int color = 0; // Black default
Drawable drawable = buttons[randomI][randomJ].getBackground();
if (drawable instanceof ColorDrawable) {
color = ((ColorDrawable) drawable).getColor();
}
Set the new color temporarily:
buttons[randomI][randomJ].setBackgroundColor(Color.rgb(155, 17, 30));
and after some period restore the initial color:
buttons[randomI][randomJ].setBackgroundColor(color);

Layout is copying over itself after updating when setting background colour programmatically

I'm giving the option to set a custom background colour for an activity, and so I don't have the colour set in the layout xml. When a custom colour is set this works fine, however when using the default black, or when the custom colour option is off the layout is copying over itself.
Here is the method
public void setCustomBackground() {
View someView = findViewById(R.id.currentTextView);
View root = someView.getRootView();
if (customBackground) {
if (timerIsInActive) {
int activeColour;
activeColour = sharedPreferences.getInt("activeColour", 0);
root.setBackgroundColor(activeColour);
} else if (timerIsInRelax) {
int relaxColour;
relaxColour = sharedPreferences.getInt("relaxColour", 0);
root.setBackgroundColor(relaxColour);
}
} else {
root.setBackgroundColor(0);
}
}
I've tried setting the colour in the xml layout, which stops the issue, but also stops the ability to change the colours.
Is this some bug with the SDK, or am I doing something stupid?
EDIT: I should also mention that when the custom colour is set as black, there is no issue with that either.
I'm still not sure exactly why the issue was happening in the first place, however changing
root.setBackgroundColor(0);
to
root.setBackgroundColor(Color.parseColor("#000000"));
appears to have fixed the issue.

Libgdx | Scene2d | ImageButton setColor not working

I have an ImageButton. The texture for it is basically a white square, with black text in the center. I want to be able to dynamically change the color of this button. The problem is that ImageButton.setColor does not do anything. I can call tint on the ImageButtonStyle which does work, but I want to be able to change the color later down the road if for instance the player clicks on the button. Thanks! Here is some code :
ImageButton.ImageButtonStyle style_button_music = new ImageButton.ImageButtonStyle();
style_button_music.imageChecked = new SpriteDrawable(new Sprite((Texture) Game.assetManager.get("button_music.png")));
style_button_music.imageUp = new SpriteDrawable(new Sprite((Texture) Game.assetManager.get("button_music.png")));
style_button_music.imageDisabled = new SpriteDrawable(new Sprite((Texture) Game.assetManager.get("button_music.png")));
button_music = new ImageButton(style_button_music);
button_music.setColor(new Color(22f/255f, 100f/255f, 255f/255f, 1f));
table.setFillParent(true);
table.setDebug(true);
table.top();
table.pad(100);
table.add(button_music).width(200).height(200);
stage.addActor(table);
Use
button_music.getImage().setColor(Color color)
The setColor() on ImageButton is just inherited method from Actor but it doesn't do anything.
Actor color doesn't cascade down to children (except for the alpha component). Since the Image of an ImageButton is a child of the Button, the Image does not inherit the color of the Button.
However, the way in which you're currently using it, I think you could use a plain Button, and set the background image instead. That does get tinted.
style_button_music.checked = new TextureRegionDrawable(new TextureRegion(Game.assetManager.get("button_music.png")));
style_button_music.up = style_button_music.checked;
style_button_music.disabled = style_button_music.checked;
You should probably be using TextureRegionDrawable instead of SpriteDrawable. It's a much lighter-weight object, and it's rare to need the extra overhead of Sprites for buttons.
If you do need to use an actual ImageButton, and you're recoloring it dynamically, you could subclass ImageButton and use it's act method to transfer it's color to its child. That way you can use ColorActions with it.
#Override
public void act (float delta) {
super.act(delta);
Color color = getColor();
getImage().setColor(color.r, color.g, color.b, 1f); //leave opaque, alpha transferred in draw()
}

LibGDX ImageButton image not to scale?

The red square is the button's boundary, while the image remains centered at 32x32px. I've tried using button.getImage() to set position and size to the button's values, but it doesn't seem to have any effect.
Just use a regular Button. It also contains a Drawable for its background, but that one is always stretched to fill the button. See the JavaDoc for ImageButton:
... If the image is the size of the button, a Button without any children can be used, where the Button.ButtonStyle.up, Button.ButtonStyle.down, and Button.ButtonStyle.checked nine patches define the image.
Note that these don't actually need to be nine patches; any Drawable will do.
Probably super late but have you tried:
yourButton.getImage().setFillParent(true);
Extending on the answer of #xitnesscomplex:
You can use yourButton.getImage().setFillParent(true); to set the size.
To set an offset is somewhat counter-intuitive
ImageButton.ImageButtonStyle style = new ImageButton.ImageButtonStyle();
style.imageUp = new TextureRegionDrawable(new Texture(Gdx.files.internal(btn.png")));
ImageButton yourButton = new ImageButton(style);
style.unpressedOffsetX = -new_brush.getWidth()/4.0f;
style.unpressedOffsetY = -new_brush.getHeight()/4.0f;
style.pressedOffsetX = -new_brush.getWidth()/4.0f;
style.pressedOffsetY = -new_brush.getHeight()/4.0f;
style.checkedOffsetX = -new_brush.getWidth()/4.0f;
style.checkedOffsetY = -new_brush.getHeight()/4.0f;
yourButton.getImage().setFillParent(true);

Set size for checkbox image in libgdx UI

I can't figure out how to manage checkbox images size. Of course, it is possible to create different size of image in my Texture atlas and take appropriate one, but I don't want to do that.
Here is my code:
AtlasRegion checkboxOn = AssetsHelper.textures.findRegion("checked");
AtlasRegion checkboxOff = AssetsHelper.textures.findRegion("unchecked");
CheckBoxStyle checkBoxStyle = new CheckBoxStyle();
checkBoxStyle.font = AssetsHelper.font66yellow;
checkBoxStyle.checkboxOff = checkboxOff;
checkBoxStyle.checkboxOn = checkboxOn;
CheckBox cbSound = new CheckBox(" Sound", checkBoxStyle);
cbSound object doesn't have such methods to rezise image of checkbox, but there is method getImage(), but seems it doesn't work too.
This is not working:
cbSound.getImage().width = 120;
cbSound.getImage().height = 120;
FYI: for example, when I wanted to draw image I did like that:
batch.draw(textureRegion, 0, 0, widthIwant, heightIwant);
But in CheckBox class there is overrided only this (without setting width and height):
public void draw (SpriteBatch batch, float parentAlpha) {
image.setRegion(isChecked ? style.checkboxOn : style.checkboxOff);
super.draw(batch, parentAlpha);
}
Question: how can I change width and height of checkbox image?
Thanks in advance.
The libgdx widgets are using drawables for drawing images. A drawable gets automatically scaled to fit the cell it is in. So in order to change the image size, change the cell size:
cbSound.getCells().get(0).size(widht, height);
For better results, you should use a nine patch for the drawable.
You need to set the image scaling type. Also method getImageCell is more correct than method getCells().get(0). Default is none.
CheckBox soundCB = new CheckBox("Sound", uiskin);
soundCB.getImage().setScaling(Scaling.fill);
soundCB.getImageCell().size(GameConfig.WIDTH/6);
soundCB.left().pad(PAD);
soundCB.getLabelCell().pad(PAD);
//...
content.add(soundCB).width(GameConfig.WIDTH/1.5f).row(); //add to table

Categories