1
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/parent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="0px"
        android:layout_height="0px"
        android:focusable="true"
        android:focusableInTouchMode="true" />

    <AutoCompleteTextView
        android:id="@+id/autoComplete"
        android:layout_width="fill_parent"
        android:layout_height="48dp"
        android:background="@drawable/black_rounded_borders" />

</RelativeLayout>

I added the useless LinearLayout just to avoid initial focus on the AutoCompleteTextView when the Activity is launched

drawable/black_rounded_borders.xml

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

    <solid android:color="#FFFFFFFF" />

    <stroke
        android:width="2dp"
        android:color="@color/selector_black_border" />


    <corners
        android:bottomLeftRadius="5dp"
        android:bottomRightRadius="5dp"
        android:topLeftRadius="5dp"
        android:topRightRadius="5dp" />

</shape>

color/selector_black_border.xml

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

    <item android:state_focused="true" android:color="#FF269ff5"/>
    <item android:state_pressed="true" android:color="#FF269ff5"/>
    <item android:state_selected="true" android:color="#FF269ff5"/>
    <item android:state_focused="false" android:color="#FF000000"/>
    <item android:state_pressed="false" android:color="#FF000000"/>
    <item android:state_selected="false" android:color="#FF000000"/>

</selector>

Activity

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    autoComplete = (AutoCompleteTextView) findViewById(R.id.autoComplete);
    Log.d("hasFocus", autoComplete.hasFocus() + "");
    Log.d("isSelected", autoComplete.isSelected() + "");
    Log.d("isPressed", autoComplete.isPressed() + "");
    //All the above are false
}

Though all the states related the blue(#FF269ff5) color are false. I still get the border in blue color. Also if I change the order of states in the color/selector_black_border.xml file, like all false first and then trues, then I get the black border. Its like only the first color works.

What am I missing?

Thank You.

user1537779
  • 2,311
  • 5
  • 28
  • 45

1 Answers1

2

I can not put this into a comment, because it is too big, but I found an explanation of this here, which states:

A ColorStateList cannot be passed as an attribute for <solid> in an XML definition, or really any attribute of a <shape>. This attribute is inflated out of the XML as a Color resource and then passed to the Drawable's setColor() method, which only takes a single ARGB value.

There is only one type of Drawable instance that is designed to contain and present multiple items based on state, and that is StateListDrawable, which is what you get when you inflate a <selector>. All other Drawable instances are meant to simply be members of this collection or drawn standalone.

Note also that an inflated <shape> item is actually a GradientDrawable and not a ShapeDrawable. If you check out the inflate() method of GradientDrawable in the source, you can get all the detail you could ask for on how each attribute is used.

Credits go to @Devunwired.

However, based on that, you can create different shapes (it's a rectangle each time), which has the following properties in each and every one of them. Use the code below in drawable/black_rounded_borders.xml and see if it's working as expected:

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

    <item>
        <selector xmlns:android="http://schemas.android.com/apk/res/android" >
            <item android:state_pressed="true">
                <shape android:shape="rectangle" >
                    <solid android:color="#FFFFFFFF" />

                    <stroke
                        android:width="2dp"
                        android:color="#FF269ff5" />

                    <corners
                        android:bottomLeftRadius="5dp"
                        android:bottomRightRadius="5dp"
                        android:topLeftRadius="5dp"
                        android:topRightRadius="5dp" />
                </shape>
            </item>
            <item android:state_focused="true">
                <shape android:shape="rectangle" >
                    <solid android:color="#FFFFFFFF" />

                    <stroke
                        android:width="2dp"
                        android:color="#FF269ff5" />

                    <corners
                        android:bottomLeftRadius="5dp"
                        android:bottomRightRadius="5dp"
                        android:topLeftRadius="5dp"
                        android:topRightRadius="5dp" />
                </shape>
            </item>
            <item android:state_selected="true">
                <shape android:shape="rectangle" >
                    <solid android:color="#FFFFFFFF" />

                    <stroke
                        android:width="2dp"
                        android:color="#FF269ff5" />

                    <corners
                        android:bottomLeftRadius="5dp"
                        android:bottomRightRadius="5dp"
                        android:topLeftRadius="5dp"
                        android:topRightRadius="5dp" />
                </shape>
            </item>
            <item>
                <shape android:shape="rectangle" >
                    <solid android:color="#FFFFFFFF" />

                    <stroke
                        android:width="1dp"
                        android:color="#FF000000" />

                    <corners
                        android:bottomLeftRadius="5dp"
                        android:bottomRightRadius="5dp"
                        android:topLeftRadius="5dp"
                        android:topRightRadius="5dp" />
                </shape>
            </item>
        </selector>
    </item>

</layer-list>

Sorry for the long post and the fact that i don't know if it's working, but I couldn't find another way of posting this.

Community
  • 1
  • 1
g00dy
  • 6,752
  • 2
  • 30
  • 43