First create an Android Project in the Eclipse IDE.
Then open the XML file and drag and drop one button to the Graphical Layout of the XML file.
Now create one XML file in the drawable folder and use two images for seeing the changes on a button press.
Default Button Image.
On Button Pressed Image.
Paste both the images into the drawable folder.
Open the design.xml file and write the following code in the XML file:
- <?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/buttonclicked" />
- <item android:drawable="@drawable/buttondefault" />
- </selector>
Here buttonclicked and buttondefault are the image's name.
Now open the activity_main.xml file from the layout folder and here's the file code:
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context="com.example.demo.MainActivity" >
- <Button
- android:id="@+id/button1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_alignParentRight="true"
- android:layout_alignParentTop="true"
- android:layout_marginTop="97dp"
- android:text="Button"
- android:background="@drawable/design"/>
- </RelativeLayout>
android:background=”
@drawable/design” is used to select the design.xml file in the background of the button.
Then run the Android application.
Before button is pressed
On pressing the button
So you can use this XML file is for any object in Android and is also useful for anything in design.xml such as images and colors.
Thank you.