I would like to adding some effects like color change to my android button when user focus/click the button
My android button layout is here :
<Button
android:id="#+id/btnUpload"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#color/btn_bg"
android:text="தகவல் அனுப்புக"
android:textColor="#color/white"
android:padding="20dp"
android:margin="20dp"
android:layout_alignParentBottom="true" />
You should check out the documentation regarding color state lists.
Create an XML file in your drawable folder with the name of btn_background and place the following code in the file:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#ffff0000"/> <!-- pressed -->
<item android:state_focused="true"
android:color="#ff0000ff"/> <!-- focused -->
<item android:color="#ff000000"/> <!-- default -->
</selector>
After you have created this file, change the background attribute of your Button to point to the new background:
android:background="#color/btn_background"
Good luck and happy coding!
Related
I have an image for my button.
In order to make use of it I have to have 1 additional image for each state:
1. disabled
2. selected
3. pressed
etc..
in iOS all those additional states are handled automatically and deferred from original image provided.
Is it possible to accomplish this for Android?
Is it possible to accomplish this for Android?
No, It is NOT. You need to have all states's images with you to define the selector
You can define button_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selected state -->
<item android:drawable="#drawable/button_bg_selected" android:state_selected="true"></item>
<!-- Pressed state -->
<item android:drawable="#drawable/button_bg_pressed" android:state_pressed="true"></item>
<!-- Disabled state -->
<item android:drawable="#drawable/button_bg_disabled" android:state_enabled="false"></item>
<!-- Normal state -->
<item android:drawable="#drawable/button_bg_normal"></item>
</selector>
Then simply assign this selector as a background of a button
<Button
android:id="#+id/button1"
android:background="#drawable/button_selector"
android:layout_width="200dp"
android:layout_height="126dp"
android:text="Hello" />
Refer : Button Selector
Hope it will help you ツ
For selected and pressed you can use an xml file with root for button's background but for disabled I guess you need to handle it in java code. I'm not sure about disabled state.
You will have to use selector for android.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/pressed"
android:state_pressed="true" />
<item android:drawable="#drawable/focused"
android:state_focused="true" />
<item android:drawable="#drawable/normal" />
</selector>
android:background="#drawable/selector_button" />
just have a look of this link.
As far as I know you need to handle this states in a new resource file called cursor: e.g:
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/edittext_selector" android:layout_height="fill_parent"
android:layout_width="fill_parent">
<!-- Image display in background in select state -->
<item
android:state_pressed="true"
android:drawable="#drawable/edittextback1">
</item>
<!-- Image display in background in select state -->
<item
android:state_enabled="true"
android:state_focused="true"
android:drawable="#drawable/edittextback2">
</item>
<!-- Default state -->
<!--<item android:state_enabled="true"-->
<!--android:drawable="#drawable/your_ninepath_image">-->
<!--</item>-->
</selector>
This answer refers to how you can handle button states in Android
You can create a button layout file separately in put that in res > layout folder
For Instance:
if the layout file name is btn.xml
<?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/btn_pressed" />
<item android:drawable="#drawable/btn_normal"/>
</selector>
You can set BackgroundResource of a Button
yourButton.setBackgroundResource(R.layout.btn);
EDIT
In addition you can refer to this link which is more closer to answer your question
Android Studio 0.3.7
Hello,
I have created 2 buttons png and patched using draw9patch.
The buttons will indicate whether the buttons is pressed or unpressed.
I have the following buttons_colours.xml in my values directory
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/rd_btn_press"/> <!-- pressed -->
<item android:state_focused="true"
android:state_pressed="false"
android:drawable="#drawable/rd_btn"/> <!-- unpressed -->
<!-- Default -->
<item android:drawable="#drawable/rd_btn"/>
</selector>
In my layout for the button I have this:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:id="#+id/button"
android:layout_gravity="center_horizontal"
android:background="#drawable/rd_btn"/>
Problem 1: I get an element selector must be declared in my buttons_colours.xml
Problem 2: Not sure if this is related to problem 1, but the button never changes to my rd_btn_press state when I press it.
Many thanks for any suggestions,
you have set wrong background for the button. Replace #drawable/rd_btn with `buttons_colours :
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:id="#+id/button"
android:layout_gravity="center_horizontal"
android:background="#drawable/buttons_colours"/>
also, I don't know why you have two states in your items in the selector. Here an example of working selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/rd_btn_press"
android:state_pressed="true" />
<item android:drawable="#drawable/rd_btn"
android:state_focused="true" />
<item android:drawable="#drawable/rd_btn" />
</selector>
I am trying to create a button that has a default image, but when it is pressed it switches to a new image. I have used this tutorial along with this question but it will not work.
This is my info_button_selector.xml:
<?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/ic_menu_info_details2" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/ic_menu_info_details2" /> <!-- focused -->
<item android:drawable="#drawable/ic_menu_info_details" /> <!-- default -->
</selector>
This is the code with my ImageButton:
<ImageButton
android:id="#+id/apModeInfoButton"
android:layout_width="0dip"
android:background="#null"
android:layout_height="match_parent"
android:layout_weight="1.15"
android:clickable="true"
android:src="#drawable/info_button_selector" />
The button stays at the initial state looking like ic_menu_info_details the entire time.
I changed my info_button_selector.xml file to 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/ic_menu_info_details2" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/ic_menu_info_details2" /> <!-- focused -->
<item android:drawable="#drawable/ic_menu_info_details2" /> <!-- default -->
</selector>
The ic_menu_info_details2 image was the displayed the entire time, which is expected. This shows that the button IS using the XML file as its drawable resource, but why is it that the button does not change its image when pressed?
EDIT
Using Joss's answer below, the image still does not change, and is now stretched in a strange way. Note: Both images are exactly the same except the second image is scaled to be 60% the size. This might mean that it is working but I cannot tell as the view is stretched.
<ImageButton
android:id="#+id/apModeInfoButton"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1.15"
android:clickable="true"
android:background="#drawable/info_button_selector" />
I used both of these versions of the XML file as well as different combinations of the images, all of the combinations resulted in the same sized image never changing.
<?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/ic_menu_info_details" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/ic_menu_info_details" /> <!-- focused -->
<item android:drawable="#drawable/ic_menu_info_details" /> <!-- default -->
</selector>
<?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/ic_menu_info_details2" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/ic_menu_info_details2" /> <!-- focused -->
<item android:drawable="#drawable/ic_menu_info_details2" /> <!-- default -->
</selector>
Set that drawable to the ImageButton background rather than the src.
<ImageButton
android:id="#+id/apModeInfoButton"
android:layout_width="0dip"
android:background="#drawable/info_button_selector"
android:layout_height="match_parent"
android:layout_weight="1.15"
android:clickable="true" />
Edit to follow up the distortion problem:
Well what you can do is to have the layers separate.
Because the background is stretchable, set this to the BACKGROUND and separate the image which shouldn't stretch and set this to the SRC.
<ImageButton
android:id="#+id/apModeInfoButton"
android:layout_width="0dip"
android:src="#drawable/non_scaleable_image"
android:background="#drawable/gradient_bg"
android:layout_height="match_parent"
android:layout_weight="1.15"
android:clickable="true" />
This way, you could have a background as a gradient and have it stretch without loosing quality, and the SRC image would not resize and distort.
That is really what an ImageButton should be used for.
Alternatively, do what I suggested in the first answer, but use a Button instead. Let me know if this helps.
You should try using a completed different image for details2 and see if the image is in fact changing, but not being re-sized. I wouldn't expect a stateful button to re-size, maybe use two different images and set visibility.
I'm trying to apply custom image to checkbox in android, for this I create an check_custom.xml file in which I define custom image for different states of check box like:
<item android:state_checked="true"
android:drawable="#drawable/btn_check_on" /> <!-- checked -->
<item android:state_checked="false"
android:drawable="#drawable/btn_check_off" /> <!-- unchecked -->
<item android:state_focused="true"
android:drawable="#drawable/btn_check_onfocus" /> <!--on focus-->
<item android:drawable="#drawable/btn_check_off" /> <!-- default -->
Three different images on three states on checked,on focus and on unchecked,
and then I assign this xml file to background attribute of check boxes,but I'm not getting required result, this technique apply the custom image as well as default image both.
Salman, set android:button instead of android:background. See also Custom checkbox image android
I didn´t like the appearance of the checkbox default image when the background layout was dark, so I changed it using a xml selector in order to have a white background in the button.
The selector is needed in android:button="#drawable/selector_check"
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:text="Show password"
android:textColor="#color/white"
android:button="#drawable/selector_check"
>
And the following is the content of "drawable/selector_check.xml":
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="#android:drawable/checkbox_on_background" /> <!-- checked -->
<item android:state_checked="false"
android:drawable="#android:drawable/checkbox_off_background" /> <!-- unchecked-->
</selector>
And this is the result:
Copy the btn_check.xml from android-sdk/platforms/android-#/data/res/drawable to your project's drawable folder and change the 'on' and 'off' image states to your custom images.
Then your xml will just need android:button="#drawable/btn_check"
<CheckBox
android:button="#drawable/btn_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true" />
If you want to use different default android icons, you can use android:button="#android:drawable/..."
<item android:drawable="#drawable/ON" android:state_checked="false"/>
<item android:drawable="#drawable/OFF" android:state_checked="true"/>
<item android:drawable="#drawable/ON"/>
I want to assign an image to one of the buttons in my activity?
How can i achieve that??
And for that where shall I place image.jpeg or any other image file?
Use ImageButton.
Put your image in resources, and use it 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/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/button_focused" /> <!-- focused -->
<item android:drawable="#drawable/button_normal" /> <!-- default -->
</selector>
its pretty old question but seems like you didn't get best solution on this post because still haven't accepted any answer.
So if we need a "button with Image" then we have two options
either we can go for Image Button as 'Orsol' mentioned in his answer
or simply you can Add Normal Button and then set Image to Background using Properties
code will be like this
<Button
android:background="#drawable/square_1_up"
android:layout_height="62dp"
android:layout_width="65dp"
android:id="#+id/button1">
</Button>
for this we need to put button image in Drawable folder named "square_1_up.png"
output Button will be like
Save these file under res/drawable/button.xml
<?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/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/button_focused" /> <!-- focused -->
<item android:drawable="#drawable/button_normal" /> <!-- default -->
</selector>
Suppose your main layout file main.xml which is under res/layout/main.xml
<LinearLayout>
<ImageButton
android:src="#drawable/button.xml"
android:layout_width="fill_parent"
android:layout_heght="wrap_content"
android:id="#+id/button1"
/>
</LinearLayout>