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>
Related
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.
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!
I my app i have many buttons,what i want is that when user selects particular button,its background should change so that user knows which button is selected.So for this i have used selectors.But when ever i run my app the event on button click works fine but the button image doesnt change
Selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/people_1" android:state_selected="true"></item>
<item android:drawable="#drawable/people_1" android:state_active="true"></item>
<item android:drawable="#drawable/places_1" android:state_focused="true"></item>
<item android:drawable="#drawable/hotel_icon"></item>
</selector>
Xml where i used the selector
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/hotel"
android:layout_weight="1"
android:id="#+id/home" />
I solved it,the only thing missing was
<item android:drawable="#drawable/selected" android:state_pressed="true" />
I know when a button(without background) is clicked, it changes its color and then flow goes to its onClickListner. But when i set an Image to the Button, it is not the same. I want may Image in Button to brighten up a little so that it will look nice.
I know it has something to do with styling but dont know what style to use,
below is my xml code for the button.
<Button
android:id="#+id/supplier"
style="?android:attr/buttonStyleSmall"
android:layout_width="160dp"
android:layout_height="60dp"
android:layout_alignLeft="#id/two_player"
android:layout_alignTop="#id/two_player"
android:layout_marginTop="75dp"
android:background="#drawable/supplier" />
Create one buttonselector in drawable folder then set it button background
btnselector.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_bg_clicked" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/btn_bg" /> <!-- focused -->
<item android:drawable="#drawable/btn_bg" /> <!-- default -->
</selector>
Now set this to button background as like
<Button
android:id="#+id/supplier"
style="?android:attr/buttonStyleSmall"
android:layout_width="160dp"
android:layout_height="60dp"
android:layout_alignLeft="#id/two_player"
android:layout_alignTop="#id/two_player"
android:layout_marginTop="75dp"
android:background="#drawable/btnselector" />
i think you got it ...
You have to use a Selector to achieve that.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Active tab -->
<item
android:state_selected="true"
android:state_focused="false"
android:state_pressed="false"
android:drawable="#drawable/tab_bg_selected" />
<!-- Inactive tab -->
<item
android:state_selected="false"
android:state_focused="false"
android:state_pressed="false"
android:drawable="#drawable/tab_bg_unselected" />
<!-- Pressed tab -->
<item
android:state_pressed="true"
android:drawable="#drawable/tab_bg_pressed" />
<!-- Selected tab (using d-pad) -->
<item
android:state_focused="true"
android:state_selected="true"
android:state_pressed="false"
android:drawable="#android:color/transparent" />
</selector>
Supposing the above file is called bg_selector, you set it as the background of the object you want to brighten, and in your case the 'Pressed tab' is what you're looking for. In my 'tab_bg_pressed' you would define another drawable where you would set the background as a brighten effect or whatever you want.
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>