Swapnil Sharma Swapnil Sharma -5 years ago 124
Android Question

How to change background color of first item of a listview after setAdapter is used?

How to change background color of first item or any other item of a listview after setAdapter is used? Basically, I want to change inside onCreate and outside of setOnItemClickListener.

Setting List Array Adapter Code:

ArrayAdapter arrayAdapter = new ArrayAdapter(SecondActivity.this,android.R.layout.simple_list_item_1,strings);


Answer Source

Try this

you can do that in getView method at your adapter and if you use ArrayAdapter you can also override getView method as following :

     ArrayAdapter arrayAdapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,strings){
                public View getView(int position, View convertView, ViewGroup parent) {

                    View view;
                    if(convertView==null) {
                        LayoutInflater Inflater = (LayoutInflater) MainActivity.this.getSystemService(MainActivity.this.LAYOUT_INFLATER_SERVICE);
                        view = Inflater.inflate(android.R.layout.simple_list_item_1, null);
//Update Of textView

                    TextView txt1=(TextView)view.findViewById(android.R.id.text1);

    //Rest of your code
                    return view;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download