Reputation: 7793
I try to simulate selector xml(first button) by java (second button).
Let's say i have this onCreate()
java code in MainActivity.java
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button secondButton = (Button) findViewById(R.id.buttonJavaSelector);
int[][] hole_states = new int[][] {
new int[]{android.R.attr.state_pressed},
new int[]{}
};
int[] hole_colors = new int[] {
ContextCompat.getColor(this, android.R.color.white),
ContextCompat.getColor(this, android.R.color.holo_green_light)
};
ColorStateList holeStateList = new ColorStateList(hole_states, hole_colors);
//secondButton.setBackgroundColor(this.getResources().getColor(android.R.color.holo_red_light));
secondButton.setBackgroundTintList(holeStateList);
secondButton.setHighlightColor(Color.RED);
}
Then activity_main.xml
:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.blogspot.diannaoxiaobai.tmptesting.MainActivity">
<LinearLayout
android:id="@+id/layout_sign_in"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/yes_part"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/buttonXmlSelector"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/button_ripple_light"
android:text="Cancel (Xml Selector)"
android:textColor="@android:color/holo_blue_light" />
</LinearLayout>
<LinearLayout
android:id="@+id/no_part"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/buttonJavaSelector"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sign In (Java Selector)"
android:textColor="@android:color/holo_blue_light" />
</LinearLayout>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
And then drawable/button_ripple_light.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="@android:color/white" />
<item android:drawable="@android:color/holo_green_light" />
</selector>
The output screenshot:
If i pressed both buttons, both able to become white color:
But i don't want the padding around the 2nd button. I searched many answers in S/O, they always suggest change background color to remove (Someone said it's shadow, not padding). So i uncomment this line in MainActivity.java
above (Note that i set red color here):
secondButton.setBackgroundColor(this.getResources().getColor(android.R.color.holo_red_light));
Now the padding no longer exist, but nothing happen(i.e. become white) when i press second button:
setBackgroundColor() to remove padding doesn't works correctly. What should i do to remove second button padding in this case ?
Upvotes: 0
Views: 438
Reputation: 3455
Try
StateListDrawable holeStateList = new StateListDrawable();
holeStateList.addState(new int[]{android.R.attr.state_pressed}, new ColorDrawable(ContextCompat.getColor(this,
android.R.color.white)));
holeStateList.addState(new int[]{}, new ColorDrawable(ContextCompat.getColor(this,
android.R.color.holo_green_light)));
secondButton.setBackground(holeStateList);
Upvotes: 1