I have setup background image for a button as below.
// declarations globally declared...
ImageButton sampleButton;
int sampleFirstImage = 0;
int sampleSecondImage = R.drawable.imageSecond;
......................................
......................................
sampleFirstImage = R.drawable.imageFirst;
sampleButton.setImageResource(sampleImage);
In certain cases, i want to replace this image with the another second image if the first image is set. As i need to check the condition as first image set or not for button, i need to know what image is already being set for the button. So i want to GET, image set for the button? Is there any API which gets image id set for a control?
Thanks. Appreciate if you know and can explain it to me!
If you are setting the image yourself then you could consider adding a tag (setTag) whenever you set an image source.
When you get the tag later you will know which image was last set.
Related
I'm building the Activity to take information on the users pet and make a sqlite database entry with it. I want to be able to take a picture and store the src as one of the rows in the entry.
I currently have an ImageButton displaying a default picture from drawable, but how do I extract the src from the ImageButton?
I figure if I can do that then it'll be easy to make the button clickable so the user can set their own (then when they hit the save button, it'll grab the src of the new pic instead, otherwise if they don't click it the default pic will get saved)
Please Check the Below answer may be helps you.
Bitmap bitmap = ((BitmapDrawable)imagebutton.getDrawable()).getBitmap();
I am trying to create a button from using an image, and add the event handling for that.
However, I get some errors when adding the highlighted code (please refer to the second image when clicking on this link Code )
Image 1: the errors
Image 2: the java code
Image 3: xml code
Thank you in advance.
Let me know if you would like further info
Button is not set correctly in buttonId variable, do this intead:
imageButton = (ImageButton) findViewById(R.id.imageButton1)
and also please post your code here
just do this
first remove line
//int buttonid line
and replace
imageButton=(ImageButton)findViewById(buttonid);
with
imageButton=(ImageButton)findViewById(R.id.imageButton1);
you don't need to set drawable image when you already set it in you xml
There's an ImageView in my app with a drawable that can be changed by the user and a black TextView over it. If the user changes this image to one with a black background, he won't be able to see the text.
How can I identify if the image resource is R.drawable.imageA so I can set the text a different color?
I thought about setting a tag for the ImageView, but then I would have to set it for each option, when there's only two options I care about.
I tried:
if (imageview.getDrawable.equals(R.drawable.imageA)) {
textView.setText(Color.WHITE);
}
Also tried with getResource, but neither worked.
Aside from WebnetMobile.com's answer, you could just have a variable related to which image is currently displayed.
IE. When it starts up:
boolean isImageA = true;
And then change that to false when the event to change the background is triggered. All you have to do then is check whether it's true or false.
You can't. You need to store it yourself on side and use for your comparisons, or extend ImageView and embed that functionality inside (i.e. by adding getDrawableId())
Are you going to provide the user with a Dialog box / selection mechanism where the user gets to pick the image ?
If the answer to the above is Yes, then this is the place where you should be able to capture this decision within your application, and then modify the color of the TextView .
If the answer to 1 is no, then how does the user change the Image within your application ?
In your case, you may also be able to use reflection to achieve your goal. For example, take a look at this thread -> Android, getting resource ID from string?
Added later:
For a truly generalized solution, you may want to inspect the ImageView itself, to determine the optimal color to display. This has been demonstrated in this thread:
Automatically change text color to assure readability
I have one screen with some image buttons and image view. when user click on these buttons or image view i need to show selected effect not replace any image instead of these. only selected dark shade effect on button or imageview same as iphone. How it's possible ?
i have not any selected image source , i checked these code ...
http://developer.android.com/reference/android/widget/ImageButton.html
But here we set another image on pressed event.
Anyone know any property of android to directly set select dark shadow
effect without image ?
Please anyone suggest how its possible ?
Thanks
you can use setAlpha method of imageView class to do this.
imgView.setAlpha(90);
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
Thats the link for adding images to show different state of buttons. You create a xml file for this and set this file as the background for the button.
how could i get an image and a text from the Internet and made a button for my android application dynamically?And draw them in th position i want.
i.e by Java code
thinks
Download the image and the text, then:
button.setText(downloadedText);
button.setBackgroundDrawable(drawable);
You'll have to create a Drawable from the image, which you can probably do using Drawable#createFromStream.
Drawing the button in the position you want is no different from a normal button. Just declare it in your XML (possible set to Visibility.GONE at first), get a reference to it using View.findViewById and then do the above operations on it.