Swap text for a Drawable image - java

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
}

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.

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.

ImageView will not update to display the new image, even though I tell it to

I have an array of Bitmap images, and when I try to update the ImageView through a function, nothing happens. When I run this function, I want the ImageView's image to be replaced with the next frame. The first image always gets set, however, when I run the function again, the image never changes. Can someone help me out to find where I am going wrong? Isn't it possible to update the ImageView's picture? Thank you for the help, here is my code:
public void setTheImageView() {
if (videoPreview.getVisibility() != View.GONE) {
Log.e("IF", "If statement reached");
videoPreview.setVisibility(View.GONE);
imagePreview.setImageBitmap(framesArray.get(countFrame));
imagePreview.setVisibility(View.VISIBLE);
} else {
Log.e("ELSE", "Else statement reached");
imagePreview.setImageBitmap(framesArray.get(countFrame));
}
countFrame++;
Log.e("FRAME", "the count frame is at " + countFrame);
}
Everything else is running correctly, the countFrame variable is changing, and the proper if and else statements are being reached.

Java go to next image and back with same Button

I'm trying to display another image with 1 button and go back to previous image with same button.
private void button2ActionPerformed(java.awt.event.ActionEvent evt)
{
if (labelIcon == labelIcon)
centerLabel.setIcon(labelicon2);
if (labelIcon == labelicon2)
centerLabel.setIcon(labelIcon);
}
I'm stuck with if changing only to another image and not going back. Do I need to get label properties somehow (don't know how) to execute second if statement or i need some kind of loop?
I can do it with 2 buttons but I think it can be done with 1. Am I wrong?
Add an int that keeps track of which image you are on, then if it's on 1, you change to 2, if it's on 2, you change to 1.
i.e.
int imageNumber = 1;
...
if (imageNumber == 1)
{
//change image to image 2
//also change imageNumber to 2
}
else if (imageNumber == 2)
{
//change image to image 1
//also change imageNumber to 1
}
if (labelIcon == labelIcon)
this code will always return TRUE becouse you are comparing the same object.
I think you ment something like
Icon labelIcon = centerLabel.getIcon();
if (labelIcon == this.labelIcon)
centerLabel.setIcon (labelicon2);
if (labelIcon == this.labelicon2)
centerLabel.setIcon(labelIcon);
}
Since the previous three answers all have their faults, here's mine. Use a boolean to check if the button has been clicked.
private boolean clicked = false;
private void button2ActionPerformed(ActionEvent evt) { //importing stuff is always nice...
if (!clicked) {
centerLabel.setIcon (labelicon2);
clicked = true;
} else {
centerLabel.setIcon(labelIcon);
clicked = false;
}
}

Having problems updating the visibility of a button within a popup

I'm trying to get it so that when the user clicks popbtnnext within the popup, the program checks if the rest of the array flaggedwordsused is null. If not, the button is set to be visible and the program moves on. If it is null, the program updates the button to be invisible.
popbutnext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
f++;
for (int g= f+1; g<flagwordsused.length; g++) {
if (flagwordsused[g] != null) {
popbutnext.setVisibility(0);
break;
}
else{
poptv1.setText(""+f);
popbutnext.setVisibility(1);
}
}
}
});
flagwordsused is an array of 20 elements, f in my test case starts at 0 with the string "very" occupying element zero and all other values are null. I have a textView updating with values of f so that I know the program is getting to the else statement. That value is updating just fine but the visibility of the button never changes. Any help you could give me would be appreciated. The popuplayout was created in java, not xml if that makes any difference.
You should use the constants in View to set visibility instead. Makes the code easier to read too:
To make popbutnext invisible:
popbutnext.setVisibility(View.INVISIBLE);
To make popbutnext invisible and not take any space when building the layout:
popbutnext.setVisibility(View.GONE);
To make popbutnext visible:
popbutnext.setVisibility(View.VISIBLE);
The reason that it does not work for you is because the values are wrong. Invisible is 4, gone is 8, visible is 0. However, I still recommend to use the constants in the View class - you never know, they might change in coming Android versions. Take a look at this link, and the visibility parameter.

Categories