Android Cusom Button Stretch Background - android

i am working custom button in android and i am set custom style and apply from back ground of button and my problem is image not stretch button background and i want to stretch background based on button size here' my custom button xml code:
custom_button.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/disable" />
<item android:state_enabled="true"
android:state_pressed="true" android:drawable="#drawable/pressed" />
<item android:state_enabled="true"
android:state_focused="true" android:drawable="#drawable/focused" />
<item android:state_enabled="true"
android:drawable="#drawable/normal" />
</selector>
here's my layout xml code refer button style xml code:
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="28dp"
android:text="Button"
android:background="#drawable/custom_button" />
here's my output below screen shot:
anyone can help me how to fix this issue with greatly appreciated!
Thanks in Advance!

use 9-patch images for selector images. this will help Android to stretch your images as you described in your 9-patch boundaries
Draw 9-patch
Draw 9-patch Tutorial
Another Android 9 Patch Image Tutorial

Use the draw9patch application found in android-sdk/tools. Check instructions how it works and backgrounds should be able to scale properly.

Related

Button raised effect not working when background image attached

I am creating a simple button as follows :
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/edSearch"
android:id="#+id/btnSearch"
android:layout_marginTop="30dp"
android:drawableTop="#drawable/ic_action_search"
android:textSize="16sp"
android:text="Search"
android:background="#color/md_green_500"
android:textColor="#fff"
/>
Now, when i try add any image in background like :android:background="#drawable/img123" then button raise effect does not work. is there any way i can achieve raise effect with background image ?
You can create a selector drawable for this. You need to provide 3 different images for this so that the button can toggle those images as per the state.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="false"
android:state_pressed="false"
android:drawable="#drawable/image_normal" />
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/image_pressed" />
<item android:state_focused="true"
android:state_pressed="false"
android:drawable="#drawable/image_focused" />
</selector>
Also inspite of that you can use ImageButton which will keep your image in center of button and the effects will be default as per operating system.

android imageview android:state_selected="true" not working

I am using Image selector for changing my tab-widget image icons , when I selected any one of image view, image of that image view should change. For that i have used following code, but it is working good , but problem is android:state_pressed="true" is working but android:state_selected="true" not working in any image view , i am getting stuck from this issue , can anyone help me, answer will be appreciable.Thanks in advance.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_contacts_vippie_selected" android:state_selected="true"/>
<item android:drawable="#drawable/ic_contacts_vippie"/>
Here is my Imageview:
<ImageView
android:id="#+id/hiiMoblieContact"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/flexi_contact_selector" />
Instead of image view use toggle button or check-box or radio button. For example I am using check box.
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/hiiMoblieContact"
android:button="#drawable/flexi_contact_selector"/>
flexi_contact_selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_contacts_vippie_selected" android:state_checked="true" />
<item android:drawable="#drawable/ic_contacts_vippie" />
</selector>
Hope this will help you..
Imageview doesn't save states. But don't worry, there is a workaround. Either use toggleButtons and set background or use CheckableLinearlayout as parent of imageview. Xml of imageview should be like this-
<package_name.CheckableLinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/hiiMoblieContact"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:clickable="false"
android:duplicateParentState="true"
android:src="#drawable/flexi_contact_selector"/>
</package_name.CheckableLinearLayout>
The changes are highlighted in bold. Now you can handle the checkablelinearlayout's set checkedmethod. If for example you set the checkacle linearlayout's set checked true, the image will automatically change. You will have to make this CheckableLinearLayout class in your project and its link is - http://developer.android.com/intl/es/samples/CustomChoiceList/src/com.example.android.customchoicelist/CheckableLinearLayout.html

Changing Button UI in Android

I am trying to customize a Button UI in Android.
I tried the following things:
btn.setBackgroundColor
btn.setBackgroundResource
btn.setBackgroundColor
But all of these are increasing the size of the Button, and because of that the Buttons near by can not be segregated (??).
Please suggest something.
If you want to apply a Hover effect then you have to do this in your XML where button layout is like
Button
android:id="#+id/xyz"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="15dp"
android:background="#drawable/general_btn_hover_effect"
android:onClick="somefunction"
android:text="#string/search_number"
android:textColor="#ffffff"
android:textSize="20sp"
/>
Notice android:background="#drawable/general_btn_hover_effect" there and then in #drawable folder make a general_btn_hover_effect.xml and write this into it
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/blank_normal_bg" android:state_focused="true" android:state_pressed="false"/>
<item android:drawable="#drawable/blank_hover_bg" android:state_focused="true" android:state_pressed="true"/>
<item android:drawable="#drawable/blank_hover_bg" android:state_focused="false" android:state_pressed="true"/>
<item android:drawable="#drawable/blank_normal_bg"/>
</selector>
Changing background color can not change size of button. Maybe you changed size in xml layout. Check again

Setting background on ImageButton doesn't work

I have an ImageButton and I wanted to be able to see a color when I clicked on it. I added a background, and used the same listselector I use for all my listviews, but it doesn't show anything when I click the ImageButton.
Here is the ImageButton xml
<ImageButton
android:id="#+id/button_biomes"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/biome"
android:background="#drawable/listselector"/>
And this is the listselector drawable:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_enabled="true"
android:state_pressed="true" android:drawable="#color/actionbar" />
<item android:state_enabled="true"
android:state_focused="true" android:drawable="#color/actionbar" />
<item
android:drawable="#color/android:transparent" />
</selector>
If anyone could help me It'd be greatly appreciated. Thanks!
It is possible that your image takes up entire space and doesn't show the background. Here, image and background are two different things, so do not expect the src image to be transparent.

Android - Issue with custom Spinner background

I'm trying to give a custom background to my spinner. I've downloaded the HTC sense spinner design (Cf. following pictures) with the 9 patch format.
I've created a new XML file with the following code inside:
<?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="false"
android:drawable="#drawable/spinner_pressed" />
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="#drawable/spinner_pressed" />
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/spinner_pressed" />
<item
android:drawable="#drawable/spinner_normal" />
</selector>
And I've added a spinner to my layout with this code:
<Spinner
android:id="#+id/my_spinner"
android:layout_width="180dp"
android:layout_height="50dp"
android:layout_marginLeft="20dp"
android:background="#drawable/custom_spinner" />
But whereas being correctly displayed, I got the following format:
Did I make something wrong?
I hope you'll can help me.
Regards.
V.
The stretchable areas defined on your 9-patch seem wrong, and that is why your 9-patch are stretched like this. Have a look at Nine-patch docs and notice that the left-border defines what area should be stretched. I think you should also define a stretchable area below the "arrow" if you want the "arrow" to be centered.
If you do not know how to edit 9-patch you should use draw9patch it is included in the AndroidSDK/tools.

Categories

Resources