Android: How do I compare ImageSpan drawable to XML resource drawable? - java

I have a question which I cannot find an answer for too long and I would appreciate if you can help.
I have a TextEdit which I add icons (Emojis) into it between the text.
The Icons are stored in an ImageSpan[] array named imagesInTxt.
Then I loop through the ImageSpan[] and I am trying to compare its drawable or whatever constant value that it can offer to the resource from the XML, Eg: R.Drawable.Smiley that was used as icons in the first place.
The problem is that the unique identifier that returns from getResources().getDrawable(R.Drawable.smiley) does not match the identifier than I get from imagesInTxt[x].getDrawable(), although we are talking about the same icon exactly.
I don't want and don't see a way to use .SetTag() and .GetTag() because I am dealing with ImageSpan[] from within the EditText and not the button itself, so this solution is not helping me.
I don't want to compare the ImageSpan[] directly to the ImageView button. I need to compare it to the original resource that I used (Eg: R.Drawable.xxxx) in any way possible.
How can I achieve that ?
If it is impossible, what can you suggest as an alternative solution ?
ImageSpan[] imagesInTxt = txt.getSpans(0, txt.length(), ImageSpan.class);
for (int x = 0; x < itemSpans.length; x++) { // Go through the ICONS
Drawable myIcon = getResources().getDrawable(R.Drawable.smiley);
if(imagesInTxt[x].getDrawable().equals(myIcon)) {
Icon = ";-)";
}
myIcon = getResources().getDrawable(R.drawable.sad);
if(imagesInTxt[x].getDrawable().equals(myIcon)) {
Icon = ";-(";
}
}

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.

Java Android - Making Array of images (same image)

So I've tried my best but I have not found anything close to what I would like. I would like to know if this is possible without making 50 instances in my xml file. What I think i've come close to though:
ArrayList<Bitmap> bitmapArray = new ArrayList<Bitmap>();
for(int i=0;i<50;i++){
bitmapArray.add(Bitmap.createBitmap(Bmp1,(int) image.getX(),(int) image.getY(), bullpick.getWidth(), bullpick.getHeight()));
bulletArray[i].setImageBitmap(bitmapArray.get(i));
}
I've also tried making an ImageView array of one imageView but that would only leave me with one imageview. Any help is much appreciated!
Edit: For anyone who doesn't know what I'm talking about, i'm trying to make an array of 50 imageviews of the same image, but anything that would end up with an array of 50 images would be what I'm after. Thanks!
Put your image in drawable folder.
Then create a simple loop:
ArrayList<ImageView> imageViews= new ArrayList<ImageView>();
for (int i = 0; i < 50; i++) {
ImageView imageView = new ImageView(getApplicationContext());
imageView.setImageResource(R.drawable.yourimage);
imageViews.add(imageView);
}
This would create an arraylist of 50 imageviews.

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);

Programatically setting a random image to an ImageButton

I'm trying to have the picture used by my ImageButton to be selected randomly.
I think this code should work but there seems to be a problem when passing the resource as a String.
ImageButton getClickTime = (ImageButton) findViewById(R.id.clicker);
Random generator = new Random();
int generatedRandom = generator.nextInt(10) + 1;
String randomImage = "R.drawable.bg" + (String.valueOf(generatedRandom)) ;
Drawable replaceImage = getResources().getDrawable((int) randomImage);
getClickTime.setImageDrawable((Drawable) replaceImage);
I seem to be getting in a bit of a mess with casts of ints, Strings, drawables and CharSequences.
If I manually type in a randomly selected image resource, it works. But if I pass the String to a text-box I can see that it's written exactly the same as when I type it in manually.
Can anyone see what I'm doing wrong here?
Thanks in advance
Your problem is that you are misunderstanding how Android uses resource ids. The R file holds int ids that map to resources contained in your app. You are trying to reference a drawable resource by casting its String reference into an int. That is impossible and doesn't make sense.
What I would suggest is that you create an int[] that holds the ids of all of your drawables that you want to be randomly selected.
int[] imageIds = {
R.drawable.bg1,
R.drawable.bg2,
R.drawable.bg3,
R.drawable.bg4,
R.drawable.bg5
// etc for as many images you have
};
Then, randomly select one of those drawable ids and set that your to ImageButton.
ImageButton getClickTime = (ImageButton) findViewById(R.id.clicker);
Random generator = new Random();
int randomImageId = imageIds[generator.nextInt(imageIds.length)];
getClickTime.setImageResource(randomImageId);
If you want to get the id of a resource you should use something like this:
int id = context.getResources().getIdentifier("resource_name", "drawable", context.getPackageName());
Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), id);
You may want to use an array of all images and get it by random index:
int[] all = {
R.drawable.bg1,
R.drawable.bg2,
};

How to disable OnClickListerners

I am developing an app where the user has to match the image and corresponding name of it correctly.
My problem is when the user selects the image first and selects the wrong name it will display wrong answer and if he selects the answer it will be displayed correct answer.
The user should not have to re-select the image again
I have made the onClickListerner's null but it wont work some of my code is as follows,
txt_tag[0] = (TextView) findViewById(R.id.txt_tag1);
txt_tag[0].setOnClickListener(this);
txt_tag[0].setTypeface(tf);
txt_tag[1] = (TextView) findViewById(R.id.txt_tag2);
txt_tag[1].setOnClickListener(this);
txt_tag[1].setTypeface(tf);
txt_tag[2] = (TextView) findViewById(R.id.txt_tag3);
txt_tag[2].setOnClickListener(this);
txt_tag[2].setTypeface(tf);
txt_tag[3] = (TextView) findViewById(R.id.txt_tag4);
txt_tag[3].setOnClickListener(this);
txt_tag[3].setTypeface(tf);
img[0] = (ImageButton) findViewById(R.id.img1);
img[0].setOnClickListener(this);
img[1] = (ImageButton) findViewById(R.id.img2);
img[1].setOnClickListener(this);
img[2] = (ImageButton) findViewById(R.id.img3);
img[2].setOnClickListener(this);
img[3] = (ImageButton) findViewById(R.id.img4);
img[3].setOnClickListener(this);
btn_nxt = (Button) findViewById(R.id.btn_next);
btn_nxt.setOnClickListener(this);
and I have called an method inside that method where I have made all onClickListerner's null
txt_tag[0].setOnClickListener(null);
txt_tag[1].setOnClickListener(null);
txt_tag[2].setOnClickListener(null);
txt_tag[3].setOnClickListener(null);
img[0].setOnClickListener(null);
img[1].setOnClickListener(null);
img[2].setOnClickListener(null);
img[3].setOnClickListener(null);
Can anyone tell me where I am going wrong or any modifications I can do to it.
Thanks in advance
Try using
txt_tag[0].setClickable(false);
txt_tag[1].setClickable(false);
..
img[0].setClickable(false);
img[1].setClickable(false);
..
Your question is not that clear.. but if you want your image and text tag not clickable.. make them android:clickable="false" in xml or setClickable(false);
If I were you I would be checking that logic in a listener. So if the quiestion (if it's a quiz) is in the state "ANSWERED", don't react to event.
Your question is unclear, but I understand it as follows:
You have a bunch of ImageViews and a bunch of TextViews and a Mapping between them.
You want to be able to first select an ImageView, then a TextView. If they match, "correct answer" will be displayed somewhere, if not, "wrong answer" will be displayed
If you click on a TextView before an ImageView is selected, nothing happens
If you click on a Textview and another TextView is already selected, nothing happens
If that is correct, you can do this like this: You keep two variables
int selectedImage = -1;
int selectedText = -1;
In your OnClickListener you update their values like this:
if (source instanceof ImageViews) {
selectedImage = getArrayIndex(source); // I guess you already have a method to retrieve the index
selectedText = -1; // reset textSelection
} else {
if (selectedText < 0) {
selectedText = getArrayIndex(source);
}
}
updateAnswerTextView(); // here you check if the two selections (selectedText and selectedImage) match and display the corresponding string.
Instead, you could just iterate through the TextView array and call
setClickable(false);
on every element as soon as one is clicked. If a new image is selected, you will have to set them to clickable again.
EDIT: And I agree with Rob, you should not remove your Listeners to achieve this behaviour.
Here's my suggestion, if you want you code up specific behaviour you can use the onClickListener callback to achieve what you want.
In the listener, check the state of the image; if it is already selected and you want to ignore the event then you just exit from your callback.
I think setting the onClickListener to null is the wrong thing to do.

Categories