I'm using Robotium to test a screen containing ImageButton switches in an Android app. I have two ImageButtons that use two PNG images (on.png and off.png) to display the state of the switches. I'm trying to find a method that will return the file name of the currently displayed image so I can run a test like:
Get the image being displayed by ImageButton1
Verify that the image is "on.png"
Set ImageButton1 to editable state
Select ImageButton1
Get the image being displayed by ImageButton1
Verify that the image is "off.png"
How do I verify that the correct image file is being displayed by the ImageButton in the correct situation?
BTW, the image is being set in the XML element.
I suggest you to take a look at:
View.#getDrawableState
It should work like:
boolean on = imageButton.getDrawableState()[your.R.id.state_on] == 1;
boolean off = imageButton.getDrawableState()[your.R.id.state_off] == 1;
You can also check similar question
ImageButton1.getResource().toString();
Related
I have a image "myXmlImage" in my .xml file
In the .kt file, I want to paste another "newImage" image as well as a little text "newText".
myXmlImage.setImageDrawable(newImage.drawable)
myXmlImage.imageMatrix = newImage.imageMatrix
So far it has been working very well and the new image is in place.
I'm completely stuck on how to paste the little "newText" into it
Any ideas will be greatly appreciated, thanks in advance.
Perhaps there's more than what you've described here. But if those are really your only specifications, then what you're asking for is really easy.
I'm assuming that your xml file has a root of ConstraintLayout. If it doesn't, then you're going to either want to change it or at least wrap your ImageView inside of a ConstraintLayout.
Use the layout editor to place a TextView inside of your ImageView (NOT AS A CHILD; when I say "inside", I mean spacially inside). If you haven't used the layout editor very much, it might take you a couple of tries to place the TextView inside. Don't just drag it into the ImageView as if it were a child. Drag it and drop it under the ImageView inside of the ConstraintLayout and then use little circles on the sides to attach it to the sides of the ImageView. Don't forget to set the text of the TextView as "".
(I'm not actually sure if this step is necessary because I don't know what the default background of a TextView is, but I do it out of habit.)When you're done placing that TextView inside of the ImageView, go the code of your xml file and use android:background="#00FFFFFF" (or maybe somewhere in your project you have either a transparent background drawable or a transparent #color that you can reference by name).
I'm sure you know the rest. Just give that TextView an Id like newtext and inside of your activity retrieve the view: newText:TextView=findViewById(R.id.newtext). And then set the text: newText.text="new text".
I want to change an ImageView source from my code(not the xml). I have an activity and I want to change the image in it, according to a message I recieve from an other activity. Is there any method that can change the image source from the class code and not the xml code??
For example:
if (message.equals("hello")){
//change the ImageView to hello.png;
}
You can do this by following the below steps:
Save the images with message name(i.e. hello.png, etc).
Now in your if condition use below code to set the image. Change mipmap to drawable if you have saved the images in drawable folder
int imageId=context.getResources().getIdentifier(message,"mipmap",context.getPackageName());
imageView.setImageResource(imageId);
I have been reading some post about changing an image in an ImageView.
But everyone asks for setting an ImageView with a custom image, and I want to set a system image inside it.
I made this:
ledicon.setImageDrawable(getResources().getDrawable(R.drawable.presence_busy));
But Eclipse tells me that presence_busy is not recognized.
Why?
I am using presence_online in the XML file,
android:src="#android:drawable/presence_online"
and all works perfectly.
Try this:
ledicon.setImageDrawable(getResources().getDrawable(android.R.drawable.presence_busy));
(When accessing system resouces you have to look them um in android.R not your own.)
I cannot seem to change the background image of my image button. Heres the code i'm currently trying to use:
ImageButton imgButton = (ImageButton) findViewById(R.id.showSportsButton);
imgButton.setBackgroundResource(R.drawable.tab2_selected);
However this seem to be placing the new image on top of the old image leaving me with 2 images overlapping each other.
Does anyone know why this is??
For solving this question you should implement
imgButton.setImageResource(R.drawable. tab2_selected);
Use this method:
imgButon.setBackground(getActivity().getDrawable(R.drawable.your_icon));
in xml file, <Button> write: android:backgroud="#drawable/your_file"
Make sure you are on same activity. If you are changing background of different activity first create the constructor then use object to change button.
Activity obj= new activity();
obj.imgButton.setBackgroundResource(R.drawable.tab2_selected);
and also check that oncreate() method has void return type if you are using only findviewbyid.
I'm making a grid of cells and each cell is a button that is clickable. I want to add a horizontal line to the button when it is clicked like that:
Could any one help me?
Thanks in advance
The right way to do this would be to create your own 9-patches for the Button. You can create them with any image editor (I usually use Gimp), but make sure you resave them with the draw-9-patch tool in your android-sdk directory because it adds the necessary formating to the 9.png. Check out here for more information regarding 9-patches. Once you've finished creating the two 9-patches for the Button you will need to create an xml file in your drawable folder which will select which image to choose based on the Button's current state, check out here for more info on creating a selector for a button. It's a decent amount of work, but it's worth it to do it the correct way.
private static final StrikethroughSpan STRIKE_THROUGH_SPAN = new StrikethroughSpan();
Spannable spannable = (Spannable) button.getText();
spannable.setSpan(STRIKE_THROUGH_SPAN, 0, button.getText().length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
Have a look at these link for better understanding::
StrikethroughSpan
Spannable.setSpan