Change button background to clicked - android

When you press on a button in Android it changes color. I want it to stay like that. I mean i want something like this:
Button btn = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
btn.setBackGroundColor(R.drawable.clicked /*clicked style*/ );
}
});
example:
This is a pressed on button. I want it to stay like that after i stop pressing it. Is there a easy way like that:
android.R.drawable.btn_default //this is the default button style, i want the pressed one :D

In your java code write button.setPressed(true);.It will set button in pressed mode. You can change the parameter to true and false when you want to set it pressed and unpressed...

Try using selector:
Define selector.xml in drawable folder
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_pressed_yellow"
android:state_pressed="true" />
<item android:drawable="#drawable/button_normal_green" />
</selector>
and set background of the button to android:background="#drawable/selector" then in the code on the click event of the button set btn.setpressed(true);

So you want it to start non-pressed and then stay pressed after being clicked one?
Try using an OnTouchListener instead adding btn.setPressed(true);:
public boolean onTouch(View v, MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
btn.setPressed(true);
}
return true;
}

Take a look of the StateSelector images
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
You can set diferent resources for the diferent states of your button/view. This example set different colors:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/branded_content_pressed_color" android:state_enabled="true" android:state_pressed="true"/>
<item android:drawable="#color/branded_content_pressed_color" android:state_enabled="true" android:state_focused="true"/>
<item android:drawable="#color/branded_content_background_color"/>
</selector>
Then you can set the selector normally
button.setBackgroundResource(R.drawable.selector);

You can try this way either put image as dark when press or color
drawable/selector.xml
<?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/buttondark"/>
<item android:state_pressed="true" android:drawable="#drawable/buttondark" />
<item android:drawable="#drawable/button" />
</selector>
inside on-create method write
btn.setBackgroundResource(R.drawable.selector);

Related

Button states not working as expected when pressed

I have a "Like" button that the user can click to "like" something (similar to Facebook).
I need to make it so that after the user has liked something, the text color of the button changes to red.
Here's is my code now:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:color="#color/red" />
<item
android:color="#color/normal" />
</selector>
The button:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Like"
android:textColor="#drawable/like_button" />
The problem is that the text color doesn't stay red when I lift my finger, it only changes to red when I hold my finger over the button.
What should I change?
According to your code you are specifically using:
android:state_pressed="true"
This basically means it is only red when pressed hence the results you are getting
Source: https://developer.android.com/guide/topics/resources/color-list-resource.html
You need to include in your Activity (Java)
Button likeButton = (Button) findViewById(R.id.like_button);
likeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(likeButton.isSelected())
likeButton.setSelected(false);
else
likeButton.setSelected(true);
}
});
You need to include in your Layout (XML)
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/red" android:state_selected="true"/>
<item android:color="#color/red" android:state_pressed="true"/>
<item android:color="#color/normal" android:state_pressed="false"/>
<item android:color="#color/normal"/>
</selector>
<Button
android:id="#+id/like_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/like"
android:layout_gravity="center"
android:text="#string/like" />
Cheers.
In order to "save" the "state" of a "like", you have to update the data model / database behind the button with some boolean indicator that says "yes, this is now liked/unliked".
Your XML selector only says "change color when this is pressed, otherwise revert", it has no logic to say "this is now liked".
You are just stating one state for button that is state pressed.That's why,It is getting red only when you pressed on it.If you want to make the text in Red after pressing the button,Then you should add selector in drawable something like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color=""#color/red" android:state_selected="true"/>
<item android:color=""#color/red" android:state_pressed="true"/>
<item android:color="#color/normal" android:state_pressed="false"/>
<item android:color="#color/normal"/>
</selector>
In your activity,put this code:
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(button.isSelected())
button.setSelected(false);
else
button.setSelected(true);
}
});
This will select the button and will change the text color to red and if the button is already selected,It will change it to normal.If you just want to keep button selected on click,You can simply add this line in OnClickListener.
button.setSelected(true);
The state_pressed is a mechanism to let you know whether the button is really pressed or not. This is similar to the case of giving a little sound when you really click a key in the virtual keyboard.
Since I do not know the whole story of your situation, I guess that maybe a MVC pattern is suitable for your case.
For example, in the back end, there is a data storing liked= true or false.
In the view, there are two buttons : likeButton and unlikeButton. When liked==false, likeButton is visible and unlikeButton is invisible. When liked==true, likebutton is invisible and unlikeButton is visible.
The OnClick listener for the likeButton and unlikeButton is to set the data liked= true or false.
Both likeButton and unlikeButton can have state_pressed to change the button color to red to let user know that the button is already pressed and being pressed. But, anyway, once the button is released after pressed, the onClick listener should start doing the jobs and finally the already pressed button should become invisible.
Hope that this example can clarify.

toggle Textview image with each click

I have a TextView whose accompanying image I want to toggle with each click/pressed action. So if it's img_1 and the user clicks, it should switch to img_2; if the user clicks the textview again, then it should turn to img_1; and so on. I don't know how to apply the following to my end:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ico_red" android:state_pressed="true"/>
<item android:drawable="#drawable/ico_blue"/>
</selector>
right now it would only change very briefly to red on pressed and then immediately return to blue so that blue is the usual color.
The easy way will be using selector responsive for state_selected and manually toggling the selection state in the onClick listener for your textView:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ico_red" android:state_selected="true"/>
<item android:drawable="#drawable/ico_blue"/>
</selector>
And in your java code:
yourTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.setSelected(!v.isSelected());
}
});

Android Selectors Text Color

I've been over way too many StackOverflow questions and still can't get it working.
I dynamically (programmatically) create a number of buttons on my activity. I need to use selectors to change the background and text color depending on if the button is pressed or not.
I've got the background to change from black to white when its pressed, but I can't seem to get the text color to change (or set it for that matter ) - it just defaults to black.
Here's what I've got.
Java file:
monthButtons[i].setBackgroundResource(R.drawable.button_background);
monthButtons[i].setTextColor(R.color.text_color);
button_background.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#color/white"/>
<item android:drawable="#color/black"/>
</selector>
text_color.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:textColor="#color/black" />
<item
android:textColor="#color/white" />
</selector>
Can someone point me to a method to change the text color when the button is pressed? Please bear in mind I don't have anything defined in a layout file for these buttons.
Thanks
Why don't you just use one background xml? Like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#color/white"
android:textColor="#color/black" />
<item android:drawable="#color/black"
android:textColor="#color/white"/>
</selector>//Untested
And set it to be background for the button. If it doesn't work you can always do it programmatically. Hope this helps.
How about this:
button.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN )
{
button.setBackground("#000");
edittext.setTextColor("#dedede");
return true;
}
else
{
button.setBackground("#dedede");
edittext.setTextColor("#000");
return true;
}
return false;
})); // Untested. Sorry i'm away from pc :(
You can use Color State List Resource to change the text color dynamically:
https://developer.android.com/guide/topics/resources/color-list-resource.html

Button on click visual state change

Is there any way to animate a button in Android so that when you click it it changes the background of the button to a pressed image?
I'm only using the background property to show the image on the form button.
Use this XML: save it in drawable folder and set as the background drawable.
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:drawable="#drawable/category_listing_bg_img" />
<item android:state_pressed="true" android:drawable="#drawable/category_listing_bg_img_pressed" />
</selector>
add an xml file on your res/drawable folder name it button_selector.xml put also two drawable one for the pressed state and onother for unpressed or normal state. Finally add this two your xml file button selector and everything should work!! don't forget to set the #drawable/bytton_selector.xml as the background of your button on your main.xml file.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/btn_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/btn_unpressed"/>
</selector>
Yes there is. Implement onTouchListener. use the MotionEvent variable (lets say event) in onTouch method write this:
if (event.getAction() == MotionEvent.ACTION_DOWN){
/*Code*/
}
if (event.getAction() == MotionEvent.ACTION_UP){
/*Code*/
}
what you should do is create a selector (what Krishnakant Dalal was talking about).
it handels how the UI element looks like at every single state it can be (presses, disabled, normal etc.)
for more about selectors read here: http://android-journey.blogspot.com/2009/12/android-selectors.html

Android...Change Button Colour when Clicked, but I have many buttons?

I want to change the button's colour when I clicked.
But when I clicked the another second button, the first button must be coloured the with the last colour.
I mean that, when I clicked whic button, it must be coloured with blue and the other buttons must be non-colour. here is the code;
if(view == button1)
{
button1.setBackgroundColor(Color.BLUE);
}
else if(view == button2){
button2.setBackgroundColor(Color.BLUE);
}
else if(view == button3){
button3.setBackgroundColor(Color.BLUE);
}
else if(view == button4){
button4.setBackgroundColor(Color.BLUE);
}
//init all buttons background : GRAY
public void initButtons(){
button1.setBackGroundColor(Color.GRAY);
button2.setBackGroundColor(Color.GRAY);
button3.setBackGroundColor(Color.GRAY);
button4.setBackGroundColor(Color.GRAY);
}
and in the implementation of the OnClick : do this :
#Override
public void onClick(View v ) {
initButtons();
((Button)v).setBackGroundColor(Color.BLUE);
}
Hope it helps :)
you can use drawable selector to define the button states in xml then by default the clicked button will be changed to the color you want only when it is in click state.
Create a new xml file in your drawable folder for example blue_btn.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/yourColor"
android:state_pressed="true" android:state_enabled="true" />
<item android:drawable="#color/yourOtherColor" android:state_enabled="true" />
</selector>
then use R.drawable.blue_btn as a background for your buttons
refer to: http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
Keep a reference to the previously modified Button. When you have a new click, set that previously referenced Button back to un-colored, set the current button to Blue then set that reference to the current button.
When you need to change the background color of the button when it is pressed, then u have to follow these ,
//create an xml file, like layout_a.xml file in drawable
<?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" /> <!--btn pressed -->
<item android:drawable="#drawable/btn_normal" /> <!-- Normal condition -->
</selector>
//Now this file should be in a drawable folder and use this single line code in button code to get all the properties of this xml file .
<Button
android:id="#+id/street_btn"
android:layout_width="wrap_content"
android:background="#drawable/layout_a" > <!-- your required code -->
</Button>
change this line:
button2.setBackgroundColor(Color.BLUE);
and try this:
button2.setBackgroundColor(Color.parseColor("#5AC8E2"));

Categories

Resources