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

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.

Related

getText() always return empty string from dynamically created Chip component?

I'm trying to dynamically create some choice chip components based on an ArrayList of String from some computation and following are the code to create the chips and adding them to a ChipGroup created in layout XML file.
if (mChipGroup.getChildCount() == 0 ){
int i = 0;
for (Classifier.Recognition res: results){
Chip resultChip = new Chip(getDialog().getContext());
ChipDrawable chipDrawable =
ChipDrawable.createFromAttributes(
getActivity(),
null,
0,
R.style.Widget_MaterialComponents_Chip_Choice);
resultChip.setId(i++);
resultChip.setChipDrawable(chipDrawable);
resultChip.setText(res.getTitle());
mChipGroup.addView(resultChip);
}
}
The Chips displayed correctly with the text but when I tried to call getText() on the chips, it always return empty String but not the text contained by the chips. I tested this by setting the OnCheckedChangeListener on the ChipGroup and making a Toast with the text (though it didn;'t work). When I tried to display only the checkedId it works.
mChipGroup.setOnCheckedChangeListener(new ChipGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(ChipGroup group, int checkedId) {
Chip chip = group.findViewById(checkedId);
if(chip != null){
Toast.makeText(getContext(), chip.getText().toString(),Toast.LENGTH_SHORT).show();
}
}
});
My current workaround is to have a variable holding the array results and use ArrayList.get(selectedChipId.getTitle()). but don't think it should be that way though
I also found that it is able to get text from Chips added in layout file but not run-time added Chips. Tried with both 1.1.0/alpha06 and 1.1.0/alpha07 release but am having no luck. Would like to have some advice if possible. Thank you very much.
So, it seems like a bug as per answered in here and here. Current workaround is to use ((ChipDrawable) chip.getChipDrawable()).getText() instead.

Swap text for a Drawable image

The code contains an alternating "Play" and "Pause" button. It works fine, but would it be possible to customize them with a Drawable image?
I've tried using
btControl.getCompoundDrawables(R.id.imgplay)
but I did not succeed.
if (btControl.getText().equals("►")) {
this.startService(intent);
cmPasstime.setBase(SystemClock.elapsedRealtime());
cmPasstime.start();
btControl.setText("■");
} else if (btControl.getText().equals("■")) {
this.stopService(intent);
cmPasstime.stop();
btControl.setText("►");
} else if (btControl.getText().equals("►")) {
this.startService(intent);
cmPasstime.start();
btControl.setText("■");
}
Is there any way to insert two images in place of "►" and "■"?
The best way is to set tag to button as identifier and take decision on its value instead of getting drawable. Like
if (((String)btControl.getTag()).equals("playing")) {
btControl.setBackgroundResource(R.drawable.paused);
btControl.setTag("paused");
// Your Code
} else if (((String)btControl.getTag()).equals("paused")) {
btControl.setBackgroundResource(R.drawable.playing);
btControl.setTag("playing");
// Your Code
}

How to set BackgroundTintList of a button to default?

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)

If Statement that checks something's background color?

I have a small application that randomizes a background color (red, green or blue) and sets them for 9 backgrounds.
The app also randomizes a color an user has to click to get a point.
My question is, is there any way in Java in AndroidStudio that I can make an if statement that somehow takes the value of an object's background color?
if ( color_user_has_to_pick == background_color) {
point++
}
else {
nothing }
You can try the following way to get the background color of a view and the checking if that matches with user picked color
ColorDrawable viewColor = (ColorDrawable) view.getBackground();
int colorId = viewColor.getColor();
if(someColorId == colorId) {
//....
}
If there's a set number of background colors, I would just compare the resource ids since it's pretty convenient:
if (R.id.blue === ((ColorDrawable) view.getBackground()).getColor()) {
// Your logic
}
But, as an alternative you could disregard the colour comparison and just create a data structure to hold the current values of the game (the state), assign identifiers to these colour values and compare said identifiers.

blackberry - bring field to the front within NegativeMarginVerticalFieldManager

I'm stuck with a problem of bringing a field to the front as it's drawing, has anyone countered a problem like this before?
In plain words, the problem is that I'm using NegativeMarginVerticalFieldManager from the Advanced UI samples, I use it to display a highlighted text under each panel button, the text should go in front of the content under the panel, but now it's going in the back of it.
So, does any one know how to make it appear in the front?
The order of paint is different. Could you override paint in your fields and check order of painting?
Unfortunately there z-order is connected to order how fields were added to manager and I don't know how to change it.
What you could do - override paint in manager and call paint for children in your needed order:
public class MyManager extends NegativeMarginVerticalFieldManager {
protected void paint(Graphics g) {
for (int i = 1; i < getFieldCount(); i++) {
paintChild(getField(i), g);
}
if (getFieldCount() > 0)
paintChild(getField(0), g);
}
}
This is hack - I'm painting first field after all other.

Categories