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"/>
Related
This might be one of those forehead-slapping questions, but what I want to do, is make my buttons show orange (xFFFCB447) when they are pressed, just like the Android dialogs do. (picklist for example)
There doesn't seem to be a specific "pressed" TRectangle in the Default Style, and my adding different TRectangles (coloured appropriately doesn't seem to do it either), so my only alternative seems to be to use the Bitmap Style designer to Export the style.png; hope I figure out which part of the image is used for the button pressed before tomorrow afternoon, and Update the image again.
Is there not an easier/quicker way to do it?
Option A) Drop a TRectangle on your button. Set it's Align to Contents. Set it's fill color to Orange. Set it's Opacity to 0.50. Set it's HitTest to False. Set it's Visible to False. In the TButton.OnMouseDown set the TRectangle Visible to True. In the TButton.OnMouseUp set the TRectangle Visible to False. You will also need to set the OnMouseMove of the control that TButton is on to set TRectangle Visible to False as well so if you mouse down and then move away it will be hidden as well.
Option B) Drop a TFillRGBEffect on the TButton. Set it to Orange. Do the same as above to enable and disable the effect on mouse down and up. However, be aware that TFillRGBEffect may be slower than the TRectangle way.
Option C) Just use a TRectangle instead of a TButton. Put a TText or TLabel inside it for the text and align to Contents. Change it's Fill color in OnMouseDown and OnMouseUp.
I have a button in which I set its background a to 9 patch drawable and I set the text to some value. All works well. Now I want to add an image with the text (so text and image inside the button).
How can I do that?
Thanks
You can use styles in your button layout xml.
style="#style/settingTextView"
This will turn your button to the buttons you see in the settings APP. Then you can add left and right images to your button. Using -
android:drawableRight="#drawable/some_image_right"
android:drawableLeft="#drawable/some_image_left" android:text="#string/button_text"
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.
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.
I am partial to dual use buttons in iOS, like in the image below:
How can I create these types of buttons in Android? In other words, the entire button must be clickable, the arrow must be right-aligned and the text of the button must be left aligned.
I tried playing with a regular Button widget, but no luck. Do I have to resort to a Table layout (but then the clickability would be lost).
You will need customized shape background for the Button
You can find customizing shape here.
Set that background to the Button from xml, and for showing arrow, you will need to add arrow.png to your project drawables.
you will add attribute to button like this:
<Button android:backround="#drawable/cell_background"
android:drawableRight="#drawable/arrow" ... />