Android Image Button Styling - android

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>

Related

Android Studio - Need help creating button

How can I make a button with this theme? Blue and glowing box:
This can be done by creating a 9-patch image based on that custom button image (and optionally for the selected / pushed button state graphics). The 9-patch format is basically a PNG file with special marker pixels that slice the button graphic into segments so it can dynamically grow and shrink according to the final size of the button view. The Android SDK comes with a tool for creating those 9-patch images.
When this is done you need to create a state list XML resource file in the drawable resource directory. That state list references those 9-patch images for each button state (normal, pressed, etc.). The SDK documentation explains it nicely here: http://developer.android.com/guide/topics/ui/controls/button.html#CustomBackground
Code excerpt from that page:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_pressed"
android:state_pressed="true" />
<item android:drawable="#drawable/button_focused"
android:state_focused="true" />
<item android:drawable="#drawable/button_default" />
</selector>
In the end you can assign that XML drawable to your button as a background image resource like as follows:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some label text"
android:background="#drawable/my_button_state_list" />

How to layout irregular shapes in Android layout

So my graphics artist came to me with a really cool layout for controls on our new app. The problem is getting these images laid out since most views in android are rectangular. See image for what I have to work with.
Any idea how to layout these buttons so they shape around each other if that makes sense?
If the problem is the layout, you can draw the buttons and save it as png. You can layout your app by RelevantLayout . And use the "selector" to change the button image when user press an so on.
selector example: "drawable/selector1.xml"
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/buttonClicked" /> <!-- focused and pressed-->
<item android:state_pressed="true" android:drawable="#drawable/buttonClicked" /> <!-- pressed -->
<item android:drawable="#drawable/button" /> <!-- default -->
</selector>
use it like this:
android:background="#drawable/selector1"
the Views in android are rectangular, that is correct. The only workaround I see here: use multiple "invisible" Buttons (alpha set to 0). You can position them around the screen and assign the same action to some of them. Of course you'll need to implement the OnClickListener and use switch-case.

ImageButton Android round Image without square border

When I create a round ImageButton, there is also a transparent square border around the object. It shows wenn I click on the ImageButton in the Graphical Layout of the xml file.
How can I remove this border?
You can set custom background for the button,
You can specify two images(rounded) for the two states(pressed and not pressed), so that the only two round shaped images wold cover your image, default rectangular would go. I hope It helps you.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/skyblueBackground" android:state_pressed="true" />
<item android:drawable="#color/transparentBackground" />
</selector>
I tried with colors, you can use images. You just want to save it in your drowable folder and specify it for the background of button

Holo theme and custom background for my button

I have some problems to add the blue color over the button when the user press it. It works if there is no drawable in background for this button but in my case, i have to add a custom background and i want the blue color when the user clicks on the button.
Here is my code
<Button
android:id="#+id/create_profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/info_account"
android:layout_centerHorizontal="true"
android:background="#drawable/btn_create_profile" />
Blue color is not something that platform draws for you. Standard buttons have a selector drawable as their background, which involves a set of images for a view. So for button for example it is a standard button image, pressed button image (with blue overlay drawn above), disabled (half transparent), etc.
Button knows it's current state and displays appropriate image.
So what you want to do is to draw a pressed button yourself and create a selector drawable like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/your_pressed_button/>
<item android:drawable="#drawable/your_normal_button/>
</selector>
I believe it's worth reading about Drawable Resources. You can also find examples of button states generated here.
You should make custom drawable :
For this you have to simply create a xml file in your drawable folder and write :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="false"
android:drawable="#drawable/ic_back" />
<item android:state_pressed="true"
android:drawable="#drawable/ic_back_pressed" />
<item android:state_focused="true"
android:drawable="#drawable/ic_back_pressed" />
</selector>
and now set this drawable in background of your button.
Here, in normal state background id ic_back
and pressed and focus state background is ic_back_pressed
For creating solid drawable shapes (for example, if you want solid color backround as drawable you can go here.. )

Set opacity to ImageButton?

I want to set opacity to my ImageButton, so when it is unselected, I can see the background a bit, and when I press on it - it becomes normal(no transparency).
If the background you are using is itself an image, then you can't simply "set" the transparency, it's coming from the png image that is the resource for the background. I'd recommend creating 3 9-patch png images for the different stages of the button using transparency as necessary for whichever stage you like. There's a description of how to use a different graphic and xml config file for your own background images in the docs on ImageButton
http://developer.android.com/reference/android/widget/ImageButton.html
If you used a solid color for the background, transparency can be achieved using a color code that has AARRGGBB as elements.
android:background="#55FF0000" would be a partially transparent red background.
Use Selector (http://developer.android.com/reference/android/content/res/ColorStateList.html)
The layout code would look sth like that :
android:background="#drawable/my_selector"
and the selector code would be my_selector.xml with following content :
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="#drawable/button_without_opactity" />
<item android:state_selected="true" android:drawable="#drawable/button_without_opactity" />
<item android:drawable="#drawable/button_with_opacity" />
</selector>
button_without_opacity & button_with_opacity should be 9-patches

Categories

Resources