I have a button with 2 distinct pictures for pressed and released state. How do I switch the state programmatically?
View.setPressed(boolean)
this can be done by using custom drawable backgound inside our ImageButton
buttonSelector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="#drawable/button_pressed" android:state_selected="true"/>
<item android:drawable="#drawable/button_pressed" android:state_focused="true" />
<item android:drawable="#drawable/button_pressed" android:state_pressed="true" />
<item android:drawable="#drawable/button_disabled" android:state_enabled="false" />
<item android:drawable="#drawable/button_default" />
put this #drawable/buttonSelector.xml code inside the background of your Image Button xml code
<ImageButton
android:id="#+id/demoBtn"
android:background="#drawable/buttonSelector"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="#drawable/btndefaultImage"/>
In your Mainactivity you can set button state as pressed or in default state by using below line
demoBtn.setSelected(true);//used to maintain button as selected/pressed
demoBtn.setSelected(false);//used to maintain button as unseleced/default
Related
I know this has been asked a nos of times but i am not able to change the button background color when pressed. Here are the codes. When the page is launched the button bg color is black but when i press it, it changes to green - probably it might be at code level which i have no access so i want to use the xml file configuration.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#android:color/black"/>
<item android:state_enabled="true"
android:state_pressed="true" android:drawable="#android:color/black" />
<item android:state_enabled="true"
android:state_focused="true" android:drawable="#android:color/black" />
<item android:state_enabled="true"
android:state_selected="true" android:drawable="#android:color/black" />
</selector>
and
<Button
android:id="#+id/payBtn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/button_background_selector"
android:onClick="onPayPressed"
android:text="#string/pf_pay_btn_hint"
android:textColor="#android:color/white"
android:textColorLink="#android:color/white"
android:textSize="#dimen/pf_20_txt_size" />
Button button = findViewById(R.id.loginbtn)
button.setBackgroundColor(Color.parseColor("#555555"));
I want to create effects for ImageButton. For example, it will change color when clicked...How I do it? I want to do this in .xml file. Can you help me! Thank you!
I tried to create the state.xml file as follwing:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="#drawable/btn_0" />
<item
android:state_focused="true"
android:state_enabled="true"
android:drawable="#drawable/btn_ac" />
</selector>
However, I can't set background for ImageButton. The error like this:
All you have to do it to add the "android:background" attribut to your ImageButton and set a drawable.
Your layout
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/my_btn"
android:background="#drawable/btn_drawable"/>
btn_drawable.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/blue"
/>
<item android:state_focused="true"
android:drawable="#drawable/white"
/>
<item android:drawable="#drawable/green" />
</selector>
In that code above you set a different drawable when your ImageButton is pressed (state_pressed), focused (state_focus) or when is normal (not pressed and not focused).
Here you can find with more detail.
Create a drawable like below and name it as btn_drawable.
<?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/btn_disable" />
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="#drawable/btn_click" />
</selector>
This is for an example,Like this you can add the <item/> according to your needs and state of the image button.
Then set the drawable as image button background.
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/btn_drawable"/>
I want to create a Button with text and an image, where both the text and image change when the Button is in the pressed state. All of the other questions about Buttons and images addressed changing the background in the pressed state, but none commented on changing the image drawable in the foreground.
You need a state selector for both the text color of the button and the image used as the drawable.
Here's how you do it:
layout/my_layout.xml:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/bottom_action_delete"
android:drawableLeft="#drawable/test_button_drawable"
android:textColor="#color/link_text_red"
android:text="Test Button"
android:onClick="buttonDelete" />
drawable/test_button_drawable.xml: (bottom_action_delete_image and bottom_action_delete_image_pressed are PNGs in drawable-hdpi/)
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android"
android:dither="true" >
<item
android:state_window_focused="false"
android:drawable="#drawable/bottom_action_delete_image" />
<item
android:state_pressed="true"
android:drawable="#drawable/bottom_action_delete_image_pressed" />
<item
android:state_focused="true"
android:drawable="#drawable/bottom_action_delete_image_pressed" />
<item
android:drawable="#drawable/bottom_action_delete_image" />
</selector>
color/link_text_red.xml: (link_text_focused_red_v2, link_text_pressed_red_v2, and link_text_red_v2 are defined in values/colors.xml)
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:color="#color/link_text_red_v2"
android:state_window_focused="false" />
<item
android:color="#color/link_text_focused_red_v2"
android:state_focused="true"/>
<item
android:color="#color/link_text_pressed_red_v2"
android:state_pressed="true"/>
<item
android:color="#color/link_text_red_v2" />
</selector>
If you need something even more complex, you can use the attribute
android:duplicateParentState="true" in child layout elements of the Button, and its pressed state will be passed down the hierarchy.
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 have made my button transparent so I would like to have the button text color change when the button is pressed. Is it possible to do this using just xml files?
Yes, you can do it like that:
layout/main_layout.xml:
.....
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bonjour !"
android:textColor="#color/button_text_color"
/>
.....
color/button_text_color.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#c0c0c0" android:state_pressed="true"/>
<item android:color="#ffffff"/>
</selector>
See the section called State List in this bit of documentation...Drawable Resources.
You can define two different Button xml files one for the transparent 'default' state and another with the button as Red for your 'pressed' state. You then define a selector which switches the drawable resources in the different states.
EDIT: As per devunwired's comment the Color State List resource is probably more suitable for just changing colours rather than the drawable itself.
I like the solution proposed by Konstantin Burov in the other issue: Android customized button; changing text color
You can actually manage more states than just pressed and normal. But it should solve the problem.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Focused and not pressed -->
<item android:state_focused="true"
android:state_pressed="false"
android:color="#ffffff" />
<!-- Focused and pressed -->
<item android:state_focused="true"
android:state_pressed="true"
android:color="#000000" />
<!-- Unfocused and pressed -->
<item android:state_focused="false"
android:state_pressed="true"
android:color="#000000" />
<!-- Default color -->
<item android:color="#ffffff" />
</selector>
Then you can use that selector drawable in your button changing the text color attribute like below. Note that the selector in the example below is named "button_text_color"
android:textColor="#drawable/button_text_color"
Using the same drawable approach you can also solve the background color of the button. Just remember that in the selector instead of using the "android:color" attribute you need to use the "android:drawable" attribute like below.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Focused and not pressed -->
<item android:state_focused="true"
android:state_pressed="false"
android:drawable="#ffffff" />
<!-- Focused and pressed -->
<item android:state_focused="true"
android:state_pressed="true"
android:drawable="#000000" />
<!-- Unfocused and pressed -->
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="#000000" />
<!-- Default color -->
<item android:drawable="#ffffff" />
</selector>
And then in the button itself do, note that this time the selector name is "button_background"
android:background="#drawable/button_background"
You have to do it in your code. Try this:
mBtn = ((Button) findViewById( R.id.button1 ));
mBtn.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View v) {
mBtn.setTextColor( Color.RED );
}
});
Declare:
private Button mBtn;
You must set #drawable xml resource in textColor attributte
Here is example: Android customized button; changing text color
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="false"
android:color="#FFFFFF" />
<item
android:state_pressed="true"
android:color="#000000" />
</selector>