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.
Related
I have some buttons with transparent background in my app. Now, I want to customize them to maintain that transparent background, but when they are pressed, the backgroud should become green.
I know that are lots of topics about custom buttons here on SO, I have readed several of them, also lots of tutorials from google. And although it may seem an easy task, I'm not getting it to work.
This is an example code of my buttons:
<Button
android:id="#+id/accept_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="#drawable/button_state"
android:text="#string/btnaccept"
android:textStyle="bold" />
And this is the selector xml file buton_state.xml where I've defined the background color change for diferent button states:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Button focused and pressed-->
<item android:state_pressed="true"
android:state_focused="true" >
<shape>
<solid
android:color="#color/LightGreen" />
</shape>
</item>
<!-- Button Default-->
<item android:state_pressed="false"
android:state_focused="false" >
<shape>
<solid
android:color="#android:color/transparent" />
</shape>
</item>
</selector>
This file is in res/drawable folder.
In my app, the button gets correctly a default transparent background, but this background color doesn't change to green when focused nor pressed.
try this may be help you,
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#color/LightGreen" /> <!--pressed -->
<item android:drawable="#android:color/transparent" /> <!-- Normal -->
</selector>
Use Selector
<?xml version="1.0" encoding="utf-8"?>
<item android:drawable="#drawable/numpad_button_bg_selected" android:state_selected="true"></item>
<item android:drawable="#drawable/numpad_button_bg_pressed" android:state_pressed="true"></item>
<item android:drawable="#drawable/numpad_button_bg_normal"></item>
and then
<Button
android:id="#+id/button1"
android:background="#drawable/Selector File Name"
android:layout_width="200dp"
android:layout_height="126dp"
android:text="Hello" />
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 want to reduce the height of selected tab indicator, I have used 9 patch image for it. Can you please let me know if you have any idea for the same ?
Thanks.
I used this code:
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="#drawable/tab_unselected_focused_example" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="#drawable/tab_selected_focused_example" />
<!-- Pressed -->
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="true" android:drawable="#drawable/tab_unselected_pressed_example" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="true" android:drawable="#drawable/tab_selected_pressed_example" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="true" android:drawable="#drawable/tab_unselected_pressed_example" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="true" android:drawable="#drawable/tab_selected_pressed_example" />
and 9 patch images put in drawable, now it works perfectly.
I use a different way of achieving custom design on tabs , create two xml files like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
android:layout_weight="1"
android:background="#drawable/tab_selector"
>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Friends"
android:textColor="#drawable/tab_text_selector"
/>
When you set the indicator while adding a tab , use setIndicator(View view) pass this view after inflating it . This way you can achieve any custom design for tabs
Create selector files for changing background or text color on the basis of state.
You need to create custom layout file for highlighted and non-highlighted tabs
Try tabIndicatorHeight attribute. e.g. app:tabIndicatorHeight="1dp". There's also a nice write-up of it here: http://panavtec.me/playing-with-the-new-support-tablayout.
My purpose is to change the color of a button when click and my codes are
<?xml version="1.0" encoding="utf-8"?>
selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->
<item android:drawable="#drawable/btn_askering_active" android:state_selected="true" />
<!-- When not selected, use white-->
item android:drawable="#drawable/btn_askering" />
</selector>
It works but if i make a small change like below :
xmlns:android="http://schemas.android.com/apk/res/android">
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When not selected, use white-->
item android:drawable="#drawable/btn_askering" />
<!-- When selected, use grey -->
item android:drawable="#drawable/btn_askering_active" android:state_selected="true" />
</selector>
It does not work anymore....
I need some help...Any comments are welcomed here.Thanks
I think you need to mention stats in selector like pressed or focused and change image accordingly.
Here i have attached sample selector file,have a look and try accordingly.
<?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/estimator_hover_new" />
<item android:state_focused="true" android:state_pressed="true"
android:drawable="#drawable/estimator_hover_new" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="#drawable/estimator_hover_new" />
<item android:drawable="#drawable/estimator_new" />
</selector>
All The Best....
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>