2

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); 

    listView.setAdapter(arrayAdapter);
Asmaa Rashad
  • 593
  • 5
  • 28
Swapnil Sharma
  • 351
  • 2
  • 4
  • 13
  • Possible duplicate of [ListView item background via custom selector](http://stackoverflow.com/questions/2562051/listview-item-background-via-custom-selector) – Swapnil Sharma Dec 18 '16 at 21:37

1 Answers1

0

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){
                @Override
                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);
                    }
                    else
                    {
                        view=convertView;
                    }
                    if(position==YOUR_TARGET_POSITION_TO_CHANGE_ITS_BACKGROUND)
                    {
                        view.setBackgroundColor(0x2196f3);
                    }
//Update Of textView

                    TextView txt1=(TextView)view.findViewById(android.R.id.text1);
                text1.setText(strings[position]);

    //Rest of your code
                    return view;
                }
            };
    listView.setAdapter(arrayAdapter);
Asmaa Rashad
  • 593
  • 5
  • 28
  • Also, there's no listRowLayout. I'm using ListView. – Swapnil Sharma Jun 16 '16 at 19:50
  • ArrayAdapter arrayAdapter = new ArrayAdapter(SecondActivity.this,android.R.layout.simple_list_item_1,strings); listView.setAdapter(arrayAdapter); – Swapnil Sharma Jun 16 '16 at 19:51
  • I want to change the color of specific items after setAdapter is used based on some conditions. But I'm unable to select an item like i can do using parent.getChildAt(position).setBackgroundColor(Color.parseColor("#2196f3")); which is used inside setOnItemClickListener – Swapnil Sharma Jun 16 '16 at 19:57
  • I have edited my answer and added your code to the question accept editing and try the new update and let me know if it works or not – Asmaa Rashad Jun 16 '16 at 20:07
  • Thank you! It helped but I don't know why text becomes invisible. Also, I changed 'new strings' to 'strings' – Swapnil Sharma Jun 16 '16 at 20:21
  • check new update again it's the update of text view . if it works you can approve the answer – Asmaa Rashad Jun 16 '16 at 20:32
  • I didn't understand why did you add TextView. There's no such textview. The text of every item becomes invisible but the number of items is still same as the length of strings. – Swapnil Sharma Jun 16 '16 at 20:35
  • `android.R.id.text1` is the textview of `android.R.layout.simple_list_item_1` and we have overridden `getView` and we just changed the background color on it but textview isn't changed – Asmaa Rashad Jun 16 '16 at 20:41
  • what do you mean of that : `The text of every item becomes invisible but the number of items is still same as the length of strings` – Asmaa Rashad Jun 16 '16 at 20:42
  • Oh thanks! It worked! I'm new in android development so I pretty much used a lot of your time. I owe you a big one. – Swapnil Sharma Jun 16 '16 at 20:45