Change text color in android.R.layout.simple_list_item_2
Asked Answered
G

4

10

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.

Georgy answered 30/7, 2013 at 7:29 Comment(1)
For passing a hex color, you would have to use setTextColor(Color.parseColor("#YOURCOLOR")). However, this would not work without a custom adapter.Compel
I
20

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;
        };
    };
Intermigration answered 30/7, 2013 at 7:37 Comment(11)
it is really strange @jaimin . It would be better if you post a new question explain your problemIntermigration
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 @Compel suggested.Endbrain
@Endbrain 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 ?Intermigration
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.Endbrain
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 ? @EndbrainIntermigration
@Endbrain if you replace Color.RED with a color defined in colors.xml you got your separation of concerns again.Operational
@Intermigration 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.Endbrain
@Operational that is a valid point. I still prefer setting these kind of things in Markup, though.Endbrain
@Endbrain I respect your opinion, but still, it doesn't make it a bad practise, just because you think so.Intermigration
@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 ?Intermigration
Working fine today, Tested on Android 5, 6 and 8, Taarget SDK 28, Android Studio 3.1.3 =) Thanks so muchScissel
C
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" />
Compel answered 30/7, 2013 at 7:32 Comment(0)
U
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; 
             }
Upthrust answered 16/9, 2014 at 21:15 Comment(0)
B
-1

You should use setTextColor(Color.any color);

TextView txt = (TextView) view.findViewById(R.id.text1);
txt.setTextColor(Color.yellow);
Baerl answered 30/7, 2013 at 8:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.