Modify color Android selected Dropdown item

How can I modify the text color of the selected item on a Spinner in an Android Honeycomb application?

EDIT: I have a Spinner layout which i'm inflating. Is it possible to add an attribute in this spinner to change the text color? I was able to change the background for example using the android:background on the spinner, but I'm not able to do the same with android:textColor.

Answers


I used this code for ListView adaper:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View rowView;
        EntSaleDocumentDetails entSaleDocumentDetails = getItem(position);

        if (convertView == null) {
            LayoutInflater inflator = (LayoutInflater) getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rowView = inflator.inflate(R.layout.myLayout,
                    null);
        } else {
            rowView = (View) convertView;
        }


        TextView myTv= (TextView) rowView
                .findViewById(R.id.tvCode);

        myTv.setTextSize(16);

        //....

        // set selected item
        LinearLayout ActiveItem = (LinearLayout) rowView;
        if (position == selectedItem)
        {
            ActiveItem
                    .setBackgroundResource(R.drawable.background_dark_blue);
        }
        else
        {
            ActiveItem
                    .setBackgroundResource(R.drawable.border02);
        }
        //....
}

public void setSelectedItem(int position) {
        selectedItem = position;
    }

your custom layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="right"
    android:background="@drawable/border02"
    android:gravity="right"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/back_color_selector03"
        android:gravity="right" >

        <TextView
            android:id="@+id/tvName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|right"
            android:gravity="right"
            android:text="TextView" >
        </TextView>

        <TextView
            android:id="@+id/tvCode"
            android:layout_width="100dip"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:text="TextView" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/llProductDetails"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/back_color_selector_blue_simple" >

        <ImageButton
            android:id="@+id/btnProductDetails"
            android:layout_width="40dip"
            android:layout_height="40dip"
            android:src="@drawable/prd_details" />

        <TableLayout
            android:id="@+id/tblProductDetails"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="right" >

            <TableRow
                android:id="@+id/tableRow1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="right" >

                <TextView
                    android:id="@+id/tvQty"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:gravity="right"
                    android:text="TextView"
                    android:textColor="@color/darkText" />

                <TextView
                    android:id="@+id/tvLblQty"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:gravity="right"
                    android:paddingLeft="20dip"
                    android:text="TextView"
                    android:textColor="@color/darkText" />

                <TextView
                    android:id="@+id/tvStockQty"
                    android:layout_width="100dip"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:gravity="right"
                    android:paddingLeft="20dip"
                    android:text="TextView"
                    android:textColor="@color/darkText" />

                <TextView
                    android:id="@+id/tvLblStockQty"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:gravity="right"
                    android:paddingLeft="20dip"
                    android:text="TextView"
                    android:textColor="@color/darkText" />
            </TableRow>

            <TableRow
                android:id="@+id/tableRow2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="right" >

                <TextView
                    android:id="@+id/tvMablagh"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:gravity="right"
                    android:text="TextView"
                    android:textColor="@color/darkText" />

                <TextView
                    android:id="@+id/tvLblMablagh"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:gravity="right"
                    android:paddingLeft="20dip"
                    android:text="TextView"
                    android:textColor="@color/darkText" />

                <TextView
                    android:id="@+id/tvFee"
                    android:layout_width="100dip"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:gravity="right"
                    android:paddingLeft="20dip"
                    android:text="TextView"
                    android:textColor="@color/darkText" />

                <TextView
                    android:id="@+id/tvLblFee"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:gravity="right"
                    android:paddingLeft="20dip"
                    android:text="TextView"
                    android:textColor="@color/darkText" />
            </TableRow>
        </TableLayout>
    </LinearLayout>

</LinearLayout>

and in activity:

@Override
    protected void onListItemClick(ListView parent, View view, int position,
            long id) {
        super.onListItemClick(parent, view, position, id);
        adapter.setSelectedItem(position);
        myListView.invalidateViews();
    }

Need Your Help

WSO2 ESB and REST services security

rest wso2 wso2esb esb

I have a couple of backend REST services which I have to use through the WSO2 ESB. I use basic authorization header for the backend. I've created the service in ESB and it works fine when I call it

jQuery javascript binding and calling issue

javascript jquery

I have a JavaScript function named 'mc_change'. Also, I have a textbox whose change event should call that JavaScript function

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.