Android ImageButton - How to hide the image while showing the background? - android

I've read the API and Googled but perhaps I've missed something: All the visibility options on an ImageButton seem to talk about this view as a whole. So, what should I do if I want to hide the image but keep the background(except for explicitly setting the image to something transparent that is)? I'm doing a Pairs Game and when one clicks on the element it should show the image and if the next click doesn't match the image should be hidden, but the grey background of the button should remain.
Thanks!

So, what should I do if I want to hide the image
Try setting it to #null or create transparent PNG in your drawables and set it.

in XML, remove android:src="something" and in the code remove imgbtn.setImageBitmap(null);

Instead of using ImageButton you can use ImageView with a FrameLayout on top of it. Set the background of FrameLayout as gray color and then show/hide this FrameLayout/Image as per your requirement. Take relative layout for each and make it clickable. On the click event of this layout, do the changes as required.

Related

how to click Background and something should happend?

I want to click the Background of a layout (not clicking anywhere, for example in the top right) and then reproduce a sound, can i put an empty View and put an On Click to that? I think is not possible.
If is possible, there is a better way?
I want to put some invisible "buttons" at the Background because I will put an image Background with shapes, that's why It couldn't be anywhere.
Thanks and sorry for my english.
You can put a button on the right corner and apply an empty or simply transparent image as icon button.
In this way, you can choose a different behavior for each screen position.

creating an "elevated button" effect in android

I want the button to appear as though it has space in between the bottom of it's face and the canvas. Then, when clicked, it will appear as though there is no space between the button and the canvas. I know I can style the button with xml to give it those 2 looks but I don't know how to do the elevated look.
My question is how would one go about doing this?
Easiest approach to this is creating 2 background images, each fitting the state you want, then you can either set the backgrounds according the button's state, or change the backgrounds by setting an onTouchListener to the button.

How can I tint/ dim an Activity

I have a button in my actionbar, when I click that I want to dim the current Activity. Fading the alpha wont work because the background is white and I need it to darken.
Cheers
Probably the simplest way would be to create a View scaled to cover the entire activity end set its background color to something half-transparent. You can also check how the stock Alarm app does it.
Cover the entire layout with Dim Gray transparent image (i.e.., overlay the image with the entire screen). I hope this idea will work for you

Custom picture for button in Android?

I want to implement a star as a button in Android so that when a user wants to favorite their 'current settings' they touch the star, which then turns yellow.
How would you do this? Can you change the default button? Or can you just import the picture and then set like an OnTouchListener?
If someone could also tell me how to import a picture (star), that would be beneficial as well.
Thanks for all the help.
Declare an ImageButton that has the star as it's background. Then you can register a listener for when the button is pressed to either show or remove the star.
Note: removing the star is equivalent to setting the imagebutton's background to null in most cases.
To import the star, put it in your "res/drawables" directory and then reference it in xml or in your Java code.
You can just change your button background when ever user clicks on it you just need to have two image for star one is for selected mode and another is for unselected mode.
So when ever user click you can toggle the selection and sets the desired image as background.
I suggest you to check RatingBar which is a star.
http://developer.android.com/reference/android/widget/RatingBar.html
A RatingBar is an extension of SeekBar and ProgressBar that shows a rating in stars. The user can touch/drag or use arrow keys to set the rating when using the default size RatingBar. The smaller RatingBar style ( ratingBarStyleSmall) and the larger indicator-only style (ratingBarStyleIndicator) do not support user interaction and should only be used as indicators.
When using a RatingBar that supports user interaction, placing widgets to the left or right of the RatingBar is discouraged.
The number of stars set (via setNumStars(int) or in an XML layout) will be shown when the layout width is set to wrap content (if another layout width is set, the results may be unpredictable).
The secondary progress should not be modified by the client as it is used internally as the background for a fractionally filled star.
<RatingBar
android:id="#+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:clickable="true" android:numStars="1"/>

Placing a translucent-like image in Android

How can I place a gray image on top of my whole activity? This gray image must be translucent (meaning the user must still see what is beneath that gray image).
I wanted to created something like the pause menu in most games wherein the background can still be seen and when clicked/tapped, it becomes invisible and continues on with the game. :)
You could open a new activity that has a semi-transparent background. Or open a dialog, and have Android take care of it by itself.
If you want to use the same activity, create an ImageView over the entire activity, load it with a semi-transparent image, and set its visibility to 'gone' until you want it to appear.
You could also use FLAG_DIM_BEHIND instead of an translucent image
http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_DIM_BEHIND

Categories

Resources