I'm developing an app for ICS. I like the way in which buttons in action bar feedbacks to user's touch (blue glow around), is there some simple way to add the same feedback for ImageButton?
Something like this, but with ImageButton:
Yes, go into your SDK.
Navigate to Platforms->android-15->data->res
You are now in the res folder for the system. If you hunt in the drawable folder you should be able to find the xml selector that represents the default system button. That selector should make reference to some images that are stored in the other drawable folders. Pick a resolution and go into its folder and find all of the required images.
Once you've got all the required resources you can include them in your own project. Then use your own selector to show the alternate image when it is pressed.
Im not certain the effect you are after but I think if you set btn_default_holo_pressed.9.png as the background of an image view with an image in the src it should appear with the blue bar around the outside of it. With the selector you can make this happen during the press action.
If you are just trying to add feedback
Use the drawable as the ImageButton background.
<ImageButton
android:id="#+id/btn_show_filter_dialog"
android:layout_width="24dp"
android:layout_height="24dp"
android:background="#drawable/ic_filter_state"/>
Create the drawable file for your Image button:
ic_filter_state.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="false"
android:drawable="#drawable/ic_filter_disable" />
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="#drawable/ic_filter_click" />
<item
android:state_focused="true"
android:state_enabled="true"
android:drawable="#drawable/ic_filter_roll" />
<item
android:state_enabled="true"
android:drawable="#drawable/ic_filter_solid" />
</selector>
For feedback, you will only really need state_pressed="true" and state_enabled="true" drawables
Here is an example with a vector drawable, but you can add your own drawable.
Change the fillColor column for each state:
ic_filter_solid.xml:
<vector android:alpha="1" android:height="24dp"
android:viewportHeight="512" android:viewportWidth="512"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#ffffff" android:pathData="M487.976,0H24.028C2.71,0 -8.047,25.866 7.058,40.971L192,225.941V432c0,7.831 3.821,15.17 10.237,19.662l80,55.98C298.02,518.69 320,507.493 320,487.98V225.941l184.947,-184.97C520.021,25.896 509.338,0 487.976,0z"/>
</vector>
I think this might be what you're looking for:
Image in Canvas with touch events
Imagebuttons and imageviews are pretty much similar to each other.
Related
I am creating a game for Android where the player has the choice to pick some dice from the board. Is there a way to add a small visual effect that can inform the player which dice has he choose? Each ImageView have a listener already.
The Pic of the dice.
You will need the separate images for pressed and normal states.
For example
pressed_state image => pressed_dice_img.jpg
normal_state image => normal_dice_img.jpg
then you will have to make a selector file say dice_image_view_selector.xml in res/drawable folder like this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="#drawable/pressed_dice_image" />
<item android:state_pressed="true" android:drawable="#drawable/pressed_dice_image" />
<item android:drawable="drawable/normal_dice_image" />
</selector>
then apply to your each image view like this
android:background="dice_image_view_selector.xml"
(Now I have come across related questions on StackOverflow but unfortunately none of the solutions worked for me which is why I had to ask this separately)
I am a Novice to Android. The problem: I need to have an image that acts as a button. Now I understand that this can be achieved by either using an image on a standard button or by using something called as "ImageButton" (which I guess is highly recommended although I have no idea why).
Requirements: I need detailed guidance for going about this problem. Specifically, I thought the "putting-image-on-standard-button" was easier until I faced two major issues: I was unable to set the image in the center (thanks drawable-top,bottom,left6,right) and once I changed the background color to match that of the Activity screen's back-color, the button effect disappeared. Simply put, I need a moderately easy way of having an image act as a button or a button with an image which has all three effects: focussed, pressed and default. I used ImageButton but then I did not know how to make custom shapes or 9patch images to give it all the desired effects, I am pretty satisfied with the default button that android provided. All I simply need is something like a background hover over the image or something of that sort which indicates the user that the image is being pressed and the event has been generated!
Can someone please help me out with this? I need the UI to look decent and therefore, need the corresponding effects on my image/button. Thanks in Advance :)
This is my icon-image:
I wish to have some sort of hover effect around this that indicates that the image has been pressed just like any normal button.
Use ImageButton and StateList Drawable. You need selector for different button's states. You can assign different drawable for different state to imitate the onFocus or onPressed effect on normal button.
This is selector.xml in drawable folder under res:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#color/cyan"/> <!-- pressed state -->
<item android:state_focused="true"
android:drawable="#color/cyan"/> <!-- focused state -->
<item android:drawable="#android:color/transparent"/> <!-- default state -->
</selector>
And this is color.xml in values folder under res:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="cyan">#33B5E5</color>
</resources>
Set the ImageButton's src to your image and set the background to selector.xml.
This is the final result:
There is a good tutorial here: Android ImageButton Selector Example
If someone also still has an issue with this.
I've created the selector but referred the drawable to two different image files, and used the XML in the imagebutton as a source. It worked like a charm.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/btn_add_pressed"
android:state_pressed="true" />
<item android:drawable="#drawable/btn_add"
android:state_focused="true" />
<item android:drawable="#drawable/btn_add" />
</selector>
And the image button looks like this:
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/add_button_selector"
android:background="#null"/>
create xml view
<ImageView
android:id="#+id/imageview1"
android:background="#drawable/selector_xml_name"
android:layout_width="200dp"
android:layout_height="126dp"/>
create inside draw able folder selector_xml_name.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/numpad_button_bg_selected"android:state_selected="true"></item>
<item android:drawable="#drawable/numpad_button_bg_pressed" android:state_pressed="true"></item>
<item android:drawable="#drawable/numpad_button_bg_normal"></item>
create inside draw able folder numpad_button_bg_selected.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" android:padding="90dp">
<solid android:color="#color/selected"/>
<padding />
<stroke android:color="#000" android:width="1dp"/>
<corners android:bottomRightRadius="15dp" android:bottomLeftRadius="15dp" android:topLeftRadius="15dp" android:topRightRadius="15dp"/>
How do I style an image button so that it displays as a PNG icon but on click/touch, the PNG shape gets a soft glowing edge?
You probably want to look at 9-patch
Basically you create a transparent png with the glow effect baked into the image, and it'll scale the edges while keeping the corners intact and letting you place the content within a predefined area of the image.
To map your images to a button, you need a selector, which is a xml file and goes into your projects drawables. A sample grabbed from another answer looks like this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/red_button_pressed" />
<item android:state_focused="true" android:drawable="#drawable/red_button_focus" />
<item android:drawable="#drawable/red_button_rest" />
</selector>
Is it any way to light the borders of a button whilte pressing on it (you can see the effect when you place an ImageView and click on it). Needed to say, that i have a background on the buttone.
You need to have two images and assign each of them to one button state. You should make a state list drawable: just create an xml file "button.xml" and place it in your res/drawable folder. The xml would look like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/buttonwithglow" />
<item android:state_pressed="false"
android:drawable="#drawable/buttonwithoutglow" />
</selector>
Use a StateListDrawable.
I think, e.g. Curved buttons, or a circle button. If I can how?
this is very simple
Select any image as a background of your button. either of circle or curved or any image you want.
For Click Effect see state list diagram on google . its like stting a xml as a background which say what image to choose for pressed , focussed and normal state
It's a bad ideia to round buttons by using a rounded background image. I say it by (bad) experience... when in different resolutions it will appear pixilated.
You should use a drawable, with a shape rounded created by you!
Something like (selector to have effects on press, on selected..):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/greyer_bubble"
android:state_pressed="true" />
<!--When selected, use this -->
<item android:drawable="#drawable/greyer_bubble"
android:state_selected="true" />
<!--When not selected, use that -->
<item android:drawable="#drawable/green_bubble" />
</selector>
Example of one of the rounded buttons defined above.
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/green" />
<corners android:radius="12dp" />
</shape>
You can set image as background for button in layout design.