Android Button onPress Color - android

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"));

Related

Style EditText underline color android

I am creating a application that has couple of edittext's that chang color when the edittext has focus or not. When the view has the focus the color should be blue, when normal it should have white color, something like this:
I've tried creating two nine patches and setting the background according to this xml but seems its not working. Can anyone tell me how can I achieve this ?
Xml selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:state_focused="true"
android:drawable="#drawable/spelling_blue" />
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="#drawable/spelling_white" />
<item android:drawable="#drawable/spelling_white" />
</selector>
Thanks
I don't think you need to use android:state_window_focused.
Try 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/spelling_blue" /> <!-- pressed -->
<item
android:state_focused="true"
android:drawable="#drawable/spelling_blue" /> <!-- focused -->
<item
android:drawable="#drawable/spelling_white" /> <!-- default -->
</selector>

Effect for ImageButton in android

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"/>

Android change imageButton when finger is hover on that

i'm create simple application to work with custom buttom if finger is pressed or released and hover on that. in this simple selector project have 4 state for change background image, but i need to change this button when finger pointer is focus or hover on that but that does not work and only press work correctly in my project.
<?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/logo"/>
<item android:state_focused="true"
android:state_pressed="true"
android:drawable="#drawable/gohome" />
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/goran" />
<item android:drawable="#drawable/logo" />
</selector>
In touch UI there is no such thing as "hover". Hover is when your pointer is over a control. Without having a pointer, you cannot reproduce this step.
You can play with "focused", "pressed" and default.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"
android:drawable="#drawable/logo"/>
<item android:state_pressed="true"
android:drawable="#drawable/goran" />
<item android:drawable="#drawable/logo" />
</selector>

Android: How to brighten up the image of a button when clicked?

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.

Android: how to switch button's pressed state programmatically?

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

Categories

Resources