Saturday, 31 January 2015

Apply color and gravity to first item of spinner in XML



I have a spinner defined like this:



<Spinner
android:id="@+id/spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/sbackground"/>


Where sbackgroud.xml is defined like this:



<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://ift.tt/nIICcg" >
<item android:id="@android:id/background">
<shape android:shape="rectangle" >
<solid android:color="@color/background" />
</shape>
</item>
<item>
<bitmap android:src="@drawable/dropdown"
android:gravity="bottom|right"/>
</item>
</layer-list>


The spinner is set, after a user press a button, like this in code:



theSpinner = (Spinner) view.findViewById(R.id.spinner);
theAdapter =
new ArrayAdapter<String>(getActivity(), R.layout.spinnerdbackground,
new ArrayList<String>(Arrays.asList(getResources().getStringArray(R.array.entries))));
theSpinner.setDropDownViewResource(R.layout.textformat);
theSpinner.setAdapter(theAdapter);
theSpinner.setOnItemSelectedListener(this);


After the user presses the button, the spinner is set up and it is appearing has expected but while the spinner is not set up, the appearece is different from what I'm expecting. Even if the spinner is not working before the user presses the button I still want the first option appears like after the user presses the button.


I've added android:entries="@array/entries" to the spinner in the XML. The problem is that I've set:



android:gravity="center_vertical|center_horizontal"
android:textColor="@color/RED"


both in the spinner xml and in a style but not only the color but also the gravity is not set.


Any ideas how to solve this?


No comments:

Post a Comment