Reputation: 1846
I want to reverse the colors (text & background) of my button when it is clicked.
I have the following XML files. When I test it, I can see the text color changes but the background color remains the same. I could not see what I'm missing.
layout.xml
...
<Button
android:id="@+id/start_btn"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="0dp"
android:text="start"
android:background="@drawable/button_background"
android:textColor="@drawable/button_text_color"
android:fontFamily="@string/font_family"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
...
button_text_color.xml
<?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/miops_dark_red" />
<item android:color="@android:color/white" />
</selector>
button_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:state_focused="true">
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@android:color/white" />
<stroke
android:width="1dp"
android:color="@color/miops_dark_red" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
</shape>
</item>
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/miops_dark_red" />
<stroke
android:width="1dp"
android:color="@android:color/white" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
</shape>
</item>
</selector>
Upvotes: 0
Views: 189
Reputation: 305
Try this..
@SuppressLint("ResourceAsColor") private void setBackgroundColor(String color,Button btn) {
GradientDrawable drawable = (GradientDrawable)btn.getBackground();
drawable.setColor(Color.parseColor(color));
drawable.setStroke(4,Color.parseColor(color));
btn.setColorFilter(Color.parseColor("#ffffff"));
}
Upvotes: 0
Reputation: 1846
finally I managed to solve the problem;
I have modified background.xml
as followed;
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/button_background_pressed"
android:state_focused="true"
/>
<item
android:drawable="@drawable/button_background_pressed"
android:state_pressed="true"
/>
<item
android:drawable="@drawable/button_background_released"
/>
</selector>
and added folowing two;
button_background_pressed.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@android:color/white" />
<stroke
android:width="1dp"
android:color="@color/miops_dark_red" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
</shape>
button_background_released.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/miops_dark_red" />
<stroke
android:width="1dp"
android:color="@android:color/white" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
</shape>
Upvotes: 1
Reputation: 1808
change your btn_bak.xml for the code below (or just add the gradient, which i think is the problem), you use only border (stroke). The below i use in my app, and if i understood your question correct you can easily modify it to your needs. If gives the button a yellow background when pressed, and the black is just a frame around the button and if nothing, than white background. Tell me if it works.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true">
<shape>
<gradient
android:startColor="@color/yellow1"
android:endColor="@color/yellow"
android:angle="270" />
<stroke
android:width="3dp"
android:color="@color/black" />
<corners
android:radius="3dp" />
<padding
android:top="20dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:endColor="@color/white"
android:startColor="@color/white"
android:angle="270" />
<padding
android:top="20dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
Upvotes: 0
Reputation: 668
mLogin.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
mLogin.setBackgroundColor(Color.rgb(AESConstants.RED_DOWN,AESConstants.GREEN_DOWN,AESConstants.BLUE_DOWN));
}
else if(event.getAction() == MotionEvent.ACTION_UP) {
mLogin.setBackgroundColor(Color.rgb(AESConstants.RED_UP,AESConstants.GREEN_UP,AESConstants.BLUE_UP));
}
return false;
}
});
This is one approach You can do something like this from code itself instead from the layout files
Upvotes: 0