- /. , , .
First you need to define the button in xml:
<Button
android:id="@+id/myButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/buttonshape"
android:text="Change Colour but keep shape, please" />
You will see above it buttonshape, therefore in the folder drawable:
(This is a rectangle, gray, with a dark gray border 1dp)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#D3D3D3" />
<corners android:radius="3dp" />
<stroke
android:width="1dp"
android:color="#aeaeae" />
</shape>
Now a new xmlfile in the folder drawable, buttonpressed.xmlfor pressing. The color will be green, everything else will be the same.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
android:shape="rectangle">
<solid android:color="#2AB40E" />
<corners android:radius="3dp" />
<stroke
android:width="1dp"
android:color="#aeaeae" />
</shape>
Now, at the touch of a button in our business:
private void myButton() {
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myButton.setBackgroundResource(R.drawable.buttonpressed);
etc..etc...
source
share