Question

I want to change the color of a button when it's pressed or focused, or in fact, any state. However, I want to do it not only through XML, but also without using another drawable. Previous questions that address this, like Highlight a button when it pressed without using two drawable? and How to Change color of Button in Android when Clicked? do it either programmatically without drawables, or with drawables through XML. Is there a property I can set in a button.xml file or something that can change the background color of the view when highlighted or clicked, without consulting Java?

Here is the code to my activity_main.xml. If it's not completely obvious already, I'm using a static image for the game board to a phone tic-tac-toe game.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ImageView
    android:id="@+id/gameBoard"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:contentDescription="@string/gameboard"
    android:src="@drawable/gameboard" />

<ImageButton
    android:id="@+id/squareOne"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginBottom="288dp"
    android:layout_marginRight="219dp"
    android:layout_marginTop="71dp"
    android:background="@android:color/transparent"
    android:contentDescription="@string/top_left"
    android:src="@drawable/blank_button" />

<ImageButton
    android:id="@+id/squareTwo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/squareOne"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/squareOne"
    android:layout_marginLeft="110dp"
    android:layout_marginRight="108dp"
    android:background="@android:color/transparent"
    android:contentDescription="@string/top_middle"
    android:src="@drawable/blank_button" />

<ImageButton
    android:id="@+id/squareThree"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/squareTwo"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/squareTwo"
    android:minWidth="100dp"
    android:background="@android:color/transparent"
    android:contentDescription="@string/top_right"
    android:src="@drawable/blank_button" />

<ImageButton
    android:id="@+id/squareFour"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignRight="@+id/squareOne"
    android:layout_below="@+id/squareTwo"
    android:layout_marginBottom="179dp"
    android:layout_marginTop="9dp"
    android:background="@android:color/transparent"
    android:contentDescription="@string/middle_left"
    android:src="@drawable/blank_button" />

<ImageButton
    android:id="@+id/squareFive"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/squareFour"
    android:layout_alignLeft="@+id/squareTwo"
    android:layout_alignRight="@+id/squareTwo"
    android:layout_alignTop="@+id/squareFour"
    android:background="@android:color/transparent"
    android:contentDescription="@string/middle"
    android:src="@drawable/blank_button" />

<ImageButton
    android:id="@+id/squareSix"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/squareThree"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/squareFive"
    android:layout_marginBottom="178dp"
    android:background="@android:color/transparent"
    android:contentDescription="@string/middle_right"
    android:src="@drawable/blank_button" />

<ImageButton
    android:id="@+id/squareSeven"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignRight="@+id/squareFour"
    android:layout_below="@+id/squareFive"
    android:layout_marginBottom="70dp"
    android:layout_marginTop="10dp"
    android:background="@android:color/transparent"
    android:contentDescription="@string/bottom_left"
    android:src="@drawable/blank_button" />

<ImageButton
    android:id="@+id/squareEight"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/squareSeven"
    android:layout_alignLeft="@+id/squareFive"
    android:layout_alignRight="@+id/squareFive"
    android:layout_alignTop="@+id/squareSeven"
    android:background="@android:color/transparent"
    android:contentDescription="@string/bottom"
    android:src="@drawable/blank_button" />

<ImageButton
    android:id="@+id/squareNine"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/squareEight"
    android:layout_alignLeft="@+id/squareSix"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/squareEight"
    android:background="@android:color/transparent"
    android:contentDescription="@string/bottom_right"
    android:src="@drawable/blank_button" />

</RelativeLayout>

I use a blank_button.png file and stretch it over each square on the gameBoard ImageView. I know I know from all you haters that I should be using relative layouts instead, but I'm only looking for this app to run on a particular kind of device.

Was it helpful?

Solution

By xml,you can search by google.I will show you one way to solve the problem. A forum which mentioned this problem

Of course,if you don't use xml,you must change this to java code.It's simple,but this means it won't show complex effects.

Supplement:Override the onStateChange method,you should override your widget.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top