简体   繁体   中英

Change color textview in adapter spinner android

  reports = (Spinner)findViewById(R.id.spinner_report);
  reportType = getIntent().getStringExtra("report");

   private void setTypeReport(){
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
            R.array.type_report, R.layout.item_spinner_p);

    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    reports.setAdapter(adapter);

    int pos= adapter.getPosition(reportType);
    type_report= getResources().getStringArray(R.array.type_report);

    String valueAtIndex = type_report[pos];
    for(int i = pos; i > 0; i--){
        type_report[i] = type_report[i-1];
    }
    type_report[0] = valueAtIndex;
    //now set this array to second Spinner
    ArrayAdapter spinnerBArrayAdapter = new ArrayAdapter(this,
            android.R.layout.simple_spinner_dropdown_item,
            type_report);
    reports.setAdapter(spinnerBArrayAdapter);
    newreport = reports.getSelectedItem().toString();
}
               main.xml
               <?xml version="1.0" encoding="utf-8"?>
               <RelativeLayout
               xmlns:android="http://schemas.android.com/apk/res/android"
               android:layout_width="match_parent"
               android:background="#eee"
               android:descendantFocusability="beforeDescendants"
               android:focusableInTouchMode="true"
               android:layout_height="match_parent">

                 <Spinner
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/spinner_report"
                    android:background="@drawable/round_box"
                    android:visibility="gone"
                    android:layout_marginBottom="5dp">
                </Spinner>
               </RelattiveLayout>


     item_spinner_p.xml 

     <TextView xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@android:id/text1"
       android:layout_width="match_parent"
        android:layout_height="wrap_content"
       android:textColor="@color/black"
       android:textSize="17sp"
       android:paddingTop="10dp"
       android:paddingBottom="10dp"
       android:paddingLeft="7dp"
       android:paddingRight="7dp"/>



      string.xml
<string-array name="type_report">
    <item>Male</item>
    <item>Female</item>

</string-array>

Default color is black in item_spinner_p.xml ... either male or female , it will show black color.. Problem is , I want to do when reportType = Male (from previous activity), it will change color to Red and if reportType = Female (from previous activity) , it change color to Blue.

I think to use way set color in string.xml .. set color Red for Male .. but i dont know correct way to set it ..

You have to create custom spinner adapter. Look at this page for how to do that: http://mrbool.com/how-to-customize-spinner-in-android/28286

But if you want simple solution you can create two separate layouts for male and females like this :

For males: item_males

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@android:id/text1"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:textColor="@android:color/red"
   android:textSize="17sp"
   android:paddingTop="10dp"
   android:paddingBottom="10dp"
   android:paddingLeft="7dp"
   android:paddingRight="7dp"/>

And for females: item_females

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@android:id/text1"
   android:layout_width="match_parent"
    android:layout_height="wrap_content"
   android:textColor="@android:color/blue"
   android:textSize="17sp"
   android:paddingTop="10dp"
   android:paddingBottom="10dp"
   android:paddingLeft="7dp"
   android:paddingRight="7dp"/>

With text colors set to red and blue for males and females respectively. Then apply a check while setting the adapter like this :

ArrayAdapter<CharSequence> adapter;
if(reportType.equals("Males")){
adapter = ArrayAdapter.createFromResource(this,R.array.type_report, R.layout.item_males);}
else{
adapter = ArrayAdapter.createFromResource(this,R.array.type_report, R.layout.item_females);}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM