0

i want to change the colour of selected text in spinner. is there anyway i can change boundaries to white as well . same problem with number picker i want to appear color of numbers white either it is selected or not because all text in my project is white

 package myfyp.pkg.futureadvisor;

    import java.util.ArrayList;
    import java.util.List;
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.NumberPicker;
    import android.widget.Spinner;
    import android.widget.TextView;
    public class Enter_Info_Activity extends Activity {
           TextView t1,t2,t3;
           EditText e1,e2;
           Button b1;
           private Spinner spinner2;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.enterinfor);
            t1=(TextView) findViewById(R.id.textView1);
            t2=(TextView) findViewById(R.id.programe);
            e1=(EditText) findViewById(R.id.editText1);
            t3=(TextView) findViewById(R.id.percentage);
            addItemsOnSpinner2();
            NumberPicker np = (NumberPicker) findViewById(R.id.np);
            np.setMinValue(0);
            //Specify the maximum value/number of NumberPicker
            np.setMaxValue(100);
            //Gets whether the selector wheel wraps when reaching the min/max value.
            np.setWrapSelectorWheel(true);

            //Set a value change listener for NumberPicker


            b1 = (Button) findViewById(R.id.search);
            b1.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent intenet=new Intent(v.getContext(),List_item.class);
                    startActivityForResult(intenet,0);  
                }
            });



        }
         public void addItemsOnSpinner2() {

                spinner2 = (Spinner) findViewById(R.id.spinner2);
                List<String> list = new ArrayList<String>();
                list.add("Bachelor of Architecture");
                list.add("Bachelor of Business Administration");
                list.add("Bachelor of Commerce");
                list.add("Bachelor of Computer Information Systems");
                list.add("Bachelor of Science in Construction Technology");
                list.add("Bachelor of Criminal Justice");
                list.add("Bachelor of Economics");
                list.add("Bachelor of Education");
                list.add("Bachelor of Engineering");
                list.add("Bachelor of Fine Arts");
                list.add("Bachelor of Information Systems");
                list.add("Bachelor of Social Work");
                list.add("Bachelor of Technology");
                list.add("Bachelor of Accountancy");
                list.add("Bachelor of Science in Aerospace Engineering");
                list.add("Bachelor of Science in Architecture");
                list.add("Bachelor of Science in Architectural Engineering");
                list.add("Bachelor of Science in Business Administration");
                list.add("Bachelor of Science in Business and Technology");
                list.add("Bachelor of Science in Chemical Engineering");
                list.add("Bachelor of Science in Chemistry");
                list.add("Bachelor of Science in Civil Engineering");
                list.add("Bachelor of Science in Computer Engineering");
                list.add("Bachelor of Science in Computer Science");
                list.add("Bachelor of Science in Criminal Justice");
                list.add("Bachelor of Science in Electrical Engineering");
                list.add("Bachelor of Science in Engineering Technology");
                list.add("Bachelor of Science in English Literature");
                list.add("Bachelor of Science in Environmental Science");
                list.add("Bachelor of Science in History");
                list.add("Bachelor of Science in Human Resources Management");
                list.add("Bachelor of Science in Industrial Engineering");
                list.add("Bachelor of Science in Information Technology");
                list.add("Bachelor of Science in Information Systems");
                list.add("Bachelor of Science in Information Systems");
                list.add("Bachelor of Science in Integrated Science");
                list.add("Bachelor of Science in Integrated Science");
                list.add("Bachelor of Science in International Relations");
                list.add("Bachelor of Science in Journalism");
                list.add("Bachelor of Science in Manufacturing Engineering");
                list.add("Bachelor of Science in Marketing");
                list.add("Bachelor of Science in Mathematics");
                list.add("Bachelor of Science in Mechanical Engineering");
                list.add("Bachelor of Science in Mining Engineering");
                list.add("Bachelor of Science in Software Engineering");
                list.add("Bachelor of Science in Physics");
                list.add("Bachelor of Science in Politics");
                list.add("Bachelor of Science in Psychology");
                list.add("Bachelor of Science in Real-Time Interactive Simulation");
                list.add("Bachelor of Science in Religion");
                list.add("Bachelor of Science in Risk Management and Insurance");
                list.add("Bachelor of Science in Science Education");
                list.add("Bachelor of Science in Systems Engineering");
                list.add("Bachelor of Science in Veterinary Technology");

                ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_spinner_item, list);
                dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spinner2.setAdapter(dataAdapter);
              }



        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main_activity1, menu);

            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
            if (id == R.id.aboutusid) {
                Intent aboutusintent = new Intent(this,Aboutus_Activity.class); 
                startActivity(aboutusintent);
                return true;
            }
            else if (id == R.id.helpid) {
                Intent helpIntent = new Intent(this,Help_Activity.class); 
                startActivity(helpIntent);
                return true;
            }
            else if (id == R.id.homeid) {
                Intent mainIntent = new Intent(this,MainActivity.class); 
                startActivity(mainIntent);
                return true;}
            else if (id == R.id.exitid) {

                finish();
                System.exit(0);

            }

            else if (id == R.id.exitid) {

                return false;
            }
            return super.onOptionsItemSelected(item);
        }
    }
misbah
  • 39
  • 1
  • 8

1 Answers1

1

You can do this in the layout for your Adapter with a ColorStateList. You can create a ColorStateList using xml in your color resource folder like so:

<?xml version "1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android/com/apk/res/android">
    <item android:state_checked="true" android:color="?attr/colorAccent"/>
    <item android:state_selected="true" android:color="?attr/colorAccent"/>
    <item android:state_pressed="true" android:color="?attr/colorAccent"/>
    <item android:state_activated="true" android:color="?attr/colorAccent"/>
    <item android:color="?android:attr/textColorPrimary"/>
</selector>

Then you can create a layout using the ColorStateList (I use it here for the text color, but you can set any attribute that can use a color resource):

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="wrap_content"
    android:padding="16dp" android:textSize="16sp"
    android:textColor="@color/selector_accent"/>

Lastly, set this layout as your drop down view resource:

dataAdapter.setDropDownViewResource(R.layout.view_selectable);

As for the NumberPicker, it doesn't look like there is a standard way to set the layout, but you should be able to change the color of the text, based on this answer. Then just set the text color using your ColorStateList:

editText.setTextColor(ContextCompat.getColorStateList(this, R.color.selector_accent));
Community
  • 1
  • 1
Bryan
  • 14,756
  • 10
  • 70
  • 125
  • for example you have 30 item in list view then if(this) show 1,4,5,6,30 item elseif(this) show 10 28 17 45 item ............. if yes then how – misbah Jul 22 '16 at 02:51
  • @misbah That looks like an entirely separate question. The question stated above is just how to change the color of the text within a spinner and/or a number picker, which I believe I answered. What you are asking now seems like you want to apply a filter to your list view based on the spinner item clicked? Anyway, if you are asking a new question, post a new question. Link to it here in a new comment, and I will take a look at it. – Bryan Jul 22 '16 at 12:31
  • sorry i am new to this site as well but this is the link – misbah Jul 22 '16 at 19:22
  • http://stackoverflow.com/questions/38516587/how-to-use-selected-values-of-spinner-and-number-picker-for-if-else-statements – misbah Jul 22 '16 at 19:22