9

I'm using a simple adapter to display my code. Unfortunately, I need to change the top textView color.

This is a snippet of my code:

// Keys used in Hashmap
String[] from = { "txt1", "txt2" };
// Ids of views in listview_layout
int[] ids = { android.R.id.text1, android.R.id.text2 };
SimpleAdapter adapter = new SimpleAdapter(this, aList,
android.R.layout.simple_list_item_2, from, ids);
setListAdapter(adapter);

I tried to make my own simple_list_item_2, but it wouldn't allow me to change the color of a textView in xml for some reason. Any ideas on how to do this?

My last thought is:

findViewById(android.R.id.text1).setTextColor(#000) but I don't know where to put it, and my hex code doesn't work.

EGHDK
  • 17,818
  • 45
  • 129
  • 204
  • For passing a hex color, you would have to use `setTextColor(Color.parseColor("#YOURCOLOR"))`. However, this would not work without a custom adapter. – FD_ Jul 30 '13 at 07:35

4 Answers4

19

you have to override getView from SimpleAdapter. For instance:

SimpleAdapter adapter = new SimpleAdapter(this, aList,
            android.R.layout.simple_list_item_2, from, ids) {

        public View getView(int position, View convertView, ViewGroup parent) {
            View view = super.getView(position, convertView, parent);
            TextView text1 = (TextView) view.findViewById(android.R.id.text1);
            text1.setTextColor(Color.RED);
            return view;
        };
    };
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • it is really strange @jaimin . It would be better if you post a new question explain your problem – Blackbelt Sep 23 '14 at 08:12
  • 1
    I don't think this is a good solution. You should not mix application logic with styling. It would be better to define a custom layout as @FD_ suggested. – Jeremy May 25 '16 at 10:22
  • 1
    @Jeremy there is nothing wrong in it. If it would have been wrong, there would not have been the method `setTextColor`. *you should not mix application logic with styling.*, that's your opinion. Would you mind explaining why it is wrong ? – Blackbelt May 25 '16 at 10:35
  • I didn't say the solution is wrong, just that it is a bad one. Of course you CAN set styling in code. And of course it is my opinion that you should not mix application logic and layout, hence the downvote. Mixing code and layout results in an application that is hard to maintain and port to other platforms or device sizes. What if the TO changes the theme with the new theme having a red background? There is a good reason why the world uses CSS. – Jeremy May 25 '16 at 13:05
  • I was complaining about the downvote. I don't care about the downvote. *Mixing code and layout results in an application that is hard to maintain and port to other platforms or device sizes.* What kind of platform are you talking about? In which way does it affect *device sizes* ? @Jeremy – Blackbelt May 25 '16 at 13:15
  • 2
    @Jeremy if you replace Color.RED with a color defined in colors.xml you got your separation of concerns again. – WarrenFaith May 25 '16 at 13:20
  • @Blackbelt The question asked is a beginner's question. Answers like yours encourage starters to mix code and layout, which, IMHO, is a bad idea. The statement you quoted does not count that much for Android Applications, but for programming in general. – Jeremy May 25 '16 at 13:34
  • @WarrenFaith that is a valid point. I still prefer setting these kind of things in Markup, though. – Jeremy May 25 '16 at 13:35
  • @Jeremy I respect your opinion, but still, it doesn't make it a *bad practise*, just because you think so. – Blackbelt May 25 '16 at 13:39
  • @Jeremy, how would you do it, if, for instance, you are required to use as text color, or some other attribute, provided by an external webservice ? – Blackbelt May 25 '16 at 13:43
  • 1
    Working fine today, Tested on Android 5, 6 and 8, Taarget SDK 28, Android Studio 3.1.3 =) Thanks so much – Marcus J.Kennedy Jun 28 '18 at 13:47
1

Create a custom xml Layout for your ListView items and set the text color of the TextView using the textColor attribute:

<TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textColor="#ff0000" />
FD_
  • 12,947
  • 4
  • 35
  • 62
0

If you use a Spinner dropdown text color will not change. To change we must also add the above method the method getDropDownView.

public View getDropDownView (int position, View convertView, ViewGroup parent) {
                 View view = super.getDropDownView (position, convertView, parent); 
                 TextView text = (TextView) view.findViewById (android.R.id.text1); 
                 text.setTextColor (Color.BLACK); 
                 return view; 
             }
ferdiado
  • 369
  • 3
  • 8
-1

You should use setTextColor(Color.any color);

TextView txt = (TextView) view.findViewById(R.id.text1);
txt.setTextColor(Color.yellow);
Knut Herrmann
  • 30,880
  • 4
  • 31
  • 67