Making Android image larger when pressed, and normal when released - android

I have an app, that has an ImageViev, which can be clicked to perform an action. I want it to be a little bit bigger when it's pressed, and go back to normal when it's released. The problem is, I have 10 different images that appear in the ImageViev, depending on the situation. I've read a lot of threads about that, and none of the solutions seems to solve my problem. I need to:
Make an image bigger when the button is pressed, or replace it with a bigger version.
Make the image shrink when the button is released, or go back to normal version of the image.
When I change the image in the ImageView, it will still change its size when pressed.
I've tried this so far:
Creating a drawable item, that 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/b1big" />
<item
android:state_pressed="false"
android:drawable="#drawable/b1"/>
</selector>
but it won't work, because Android automatically scales the image to the size of the ImageView, so no matter if b1big is bigger than b1.
Changing the width and height of the drawable item, but it doesn't have these parameters.
Is creating 10 normal images like image1, image2... image10, then creating 10 big images like image1big... image10big and then creating 10 drawable objects like:
<item
android:state_pressed="true"
android:drawable="#drawable/image1big" />
<item
android:state_pressed="false"
android:drawable="#drawable/image1"/>
and using them as src for my ImageView? How do I prevent them from scaling then?

Update you selector's content to
<?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/b1big" />
<item
android:drawable="#drawable/b1"/>
</selector>

Related

Image Button change size onClick

On Android Studio I made an image perform like a button. Though what I can't find anywhere is how do I make it so when I click on the image aka the button it becomes a bit smaller and when you release it comes back to original size. Ik that onClick you can perform functions and activities and what not but how do I change the actual button image so as I'm holding down the click button it gets smaller and when i release it comes back to its original size.
You can use selectors to achieve that. For example define this in your drawable folder:
selector_state_xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_bg_selected" android:state_selected="true"></item>
<item android:drawable="#drawable/button_bg_pressed" android:state_pressed="true"></item>
<item android:drawable="#drawable/button_bg_normal"></item>
</selector>
And use this as your View drawable:
<Button
android:id="#+id/your_button"
android:background="#drawable/selector_state_xml"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
An alternative could be using ScaleAnimation

Toggle button images are of small size but toggle button takes more space

I want to add two images at the different states of toggle button.
The Images are changing as i have used the selector but the size of toggle button is very big as compared to image.
Actually ,my images are of smaller size and the toggle button is taking much space and it disturbs my layout.
Please give solution.
You have to make a file like this in your drawable folder and use it as background of that
view in android view
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/list_item_bg_normal" android:state_activated="false" />
<item android:drawable="#drawable/list_item_bg_pressed" android:state_pressed="true" />
<item android:drawable="#drawable/list_item_bg_pressed" android:state_activated="true" />
</selector>

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.

Android ActionBar styling issues

I want to have custom background for the menu items displayed in the ActionBar. I found out that I can do that by adding the following item to my app theme style definition:
<item name="android:actionBarItemBackground">#drawable/actionbar_button</item>
Where actionbar_button.xml contains:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/action_btn_on" android:state_pressed="true" />
<item android:drawable="#drawable/action_btn_off" />
</selector>
However, this causes the background image to also render behind the custom app logo displayed on the left of the ActionBar. After several attempts at figuring out a selector that would include only the buttons, I came up with this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/action_btn_on" android:state_pressed="true" android:state_enabled="true" />
<item android:drawable="#drawable/action_btn_off" android:state_pressed="false" android:state_enabled="true"/>
<item android:drawable="#android:color/transparent"/>
</selector>
Which sort of worked, and after the app loads fully the background behind the logo is transparent as I want it. But the problem is that while it's still loading it briefly shows the logo with the button background behind it and more annoyingly, it scales and crops the logo to fit inside the fill area of the 9-patch graphic that is the button background. When finished loading, the background is switched out, but the scaling and cropping remains, which looks quite ugly. How can I fix it?
Have not found a working solution to the general problem of setting image-based backgrounds to ActionBar buttons, but the scaling/cropping does not happen if I use entered XML based backgrounds, such as created with a layer-list of shapes. For some cases in may not be viable, but in my case it was entirely possible, and maybe even better than using a bitmap based resource.

Android Image Button Styling

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>

Categories

Resources