0

i have a button , so i want to change style when button is clicked . i have used button

style "oval" and background image. so when in click it action performing perfectly but

not highlighted .

  <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:background="#82B210"
    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=".SettingActivity" >

        <Button
            android:id="@+id/button2"
            style="@drawable/bg"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_alignBaseline="@+id/button1"
            android:layout_alignBottom="@+id/button1"
            android:layout_alignLeft="@+id/confess_logout_btn"
            android:background="@drawable/confessfeeds" />   

        <Button
            android:id="@+id/confess_logout_btn"
            style="@drawable/bg"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_alignBaseline="@+id/confess_settings_btn"
            android:layout_alignBottom="@+id/confess_settings_btn"
            android:layout_marginLeft="70dp"
            android:layout_toRightOf="@+id/confess_settings_btn"
            android:background="@drawable/logout" />    

      </RelativeLayout>

Thanks in advance

Hariharan
  • 24,741
  • 6
  • 50
  • 54

3 Answers3

0

To achieve different looks for various states of your button, you'll need to do more than just set the background image. You're going to need to define the various states in a drawable-xml file. Here's a tutorial on how to do this:

http://undertowsam.wordpress.com/2012/04/23/design-custom-background-and-button-for-android-using-xml/

SBerg413
  • 14,515
  • 6
  • 62
  • 88
0

Add this file in your drawable folder,

btn_selector.xml

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

    <item android:drawable="@drawable/btn_clicked_image" android:state_pressed="true"/>
    <item android:drawable="@drawable/btn_clicked_image" android:state_focused="true"/>
    <item android:drawable="@drawable/btn_normal_image"/>

</selector>

In your button add this

 android:background="@drawable/btn_selector"
Aerrow
  • 12,086
  • 10
  • 56
  • 90
  • your code working perfectly , but not need no change image only shadow or hover colour should be changed . – Pratap Manish Bhadoria Oct 03 '13 at 12:20
  • @PratapManishBhadoria: replace btn_clicked_image withyour hover/shadow color/image,now you replace the default button image with "@android:color/transparent" (instead of this "@drawable/btn_normal_image") – Aerrow Oct 03 '13 at 12:44
0
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@android:color/white" android:state_pressed="true"/>
    <item android:drawable="@color/blue"/>

</selector>

You have to write a selector like this and place it into /res/drawable . Then set this as background of your button. Say, the name of above file is button_background.xml , then you have to do like this. Problem solved.

<Button
............
............
android:background="@drawable/button_background" />
ayon
  • 2,180
  • 2
  • 17
  • 32