android selector

I use a Gallery to display Contacts's info,on the item view,there has two button which i use selector as their background,now here is the problem,i touch the area out of this two button,this two button will change the background seem like they are all pressed. here is the gallery item

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="#505050"
  >
  <ImageView 
    android:id="@+id/phone_contact_image"
    android:layout_height="160dip"
    android:layout_width="160dip"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="25dip"
    android:src="@drawable/contact_phone_image_1"
    />
  <TextView  
    android:id="@+id/phone_contact_name"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    android:textSize="25dip"
    android:layout_below="@id/phone_contact_image"
    android:layout_marginTop="10dip"
    android:gravity="center_horizontal"
    android:textColor="#FFFFFF"
    />
  <TextView
    android:id="@+id/phone_contact_numbler"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textSize="25dip"
    android:gravity="center_horizontal"
    android:layout_below="@id/phone_contact_name"
    android:text="@string/contact_phone_numbler"
    android:textColor="#FFFFFF"
    />
  <Button 
    android:id="@+id/phone_contact_edit_btn"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:text="@string/editContact_title_edit"
    android:textSize="28dip"
    android:background="@drawable/btn_button_bg"
    android:layout_marginLeft="30dip"
    android:layout_marginRight="30dip"
    android:layout_marginTop="10dip"
    android:textColor="#FFFFFF"
    android:layout_below="@id/phone_contact_numbler"
    />
  <Button 
    android:id="@+id/phone_contact_call_btn"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:text="@string/title_call_via"
    android:textSize="28dip"
    android:background="@drawable/btn_confirm_bg"
    android:layout_marginTop="20dip"
    android:layout_marginLeft="30dip"
    android:layout_marginRight="30dip"
    android:textColor="#FFFFFF"
    android:layout_below="@id/phone_contact_edit_btn"
    />
</RelativeLayout>

here is the selector

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:drawable="@drawable/btn_button_focus" />
    <item android:state_pressed="true" android:drawable="@drawable/btn_button_pressed" />
    <item android:drawable="@drawable/btn_button_normal" />
</selector>

Answers


you are right. But you didnt mentioned when the state is not pressed or not focused and not selected then what you will do?

Try this: make a styles.xml file in you res.values folder and add it.

 <style name="styleNormalButton">
        <item name="android:background">@drawable/backgroundbutton</item>
    </style>

consider the @drawable/backgroundbutton.xml is your selector file and add the below code in that.

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:drawable="@drawable/btn_button_focus" />
    <item android:state_pressed="true" android:drawable="@drawable/btn_button_pressed"/>

    <item android:drawable="@drawable/btn_default_pressed_holo_dark"
      android:state_enabled="false" />
    <item android:drawable="@drawable/btn_button_normal" />
</selector>

and finally into your layou file. set the background of the button to style/styleNormalButton

hope this will help you


Need Your Help

Deserializing a mixed list of objects from JSON

c# .net json .net-4.0 deserialization

I'm using the DataContractJsonSerializer to deserialize objects from an external service. In most cases, this has worked great for me. However, there is one case where I need to deserialize JSON ...

Java: getClass() of bounded type

java type-erasure type-parameter

I noticed something while I was derping around with generics. In the example below, doStuff1 compiles but doStuff2 doesn't:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.