Change text color in a Listview (Android)

Asked

Viewed 418 times

0

Hey, guys, I’m having a little bit of a problem. I’m trying to change the color of a text inside a standard Listview android, however, it does not change. I tried changing the color in Layout, changed by code and nothing. Check my trial codes:

Modification via code:

  @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View view = inflater.inflate(R.layout.fragment_avisos, container, false);

            TxtSemAviso = view.findViewById(R.id.TxtSemAviso);

            final ArrayList<String> avisos = preencherDados();

            final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>
                    (DashBoardActivity.context, android.R.layout.simple_list_item_1, avisos){
                @SuppressLint("ResourceAsColor")
                @Override
                public View getView (int position, View convertView, ViewGroup parent) {
                    View view = super.getView(position, convertView, parent);
                    // Como o simple_list_item_1 retorna um TextView, esse cast pode ser feito sem problemas
                    ((TextView) view).setTextColor(R.color.colorverde2);
                    return view;
                }
            };

Modification via XML:

 <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceListItemSmall"
        android:gravity="center_vertical"
        android:paddingStart="?android:attr/listPreferredItemPaddingStart"
        android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
        android:minHeight="?android:attr/listPreferredItemHeightSmall"
        android:textColor="@color/colorverde2"/>
  • You want to change the color of just one listview item or all?

1 answer

1


Do the following. Create in res/layout an Xml named after: simple_list

cole:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceListItemSmall"
        android:gravity="center_vertical"
        android:paddingStart="?android:attr/listPreferredItemPaddingStart"
        android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
        android:minHeight="?android:attr/listPreferredItemHeightSmall"
        android:textColor="@color/colorverde2"/>

In his adapter instead of calling:

android.R.layout.simple_list_item_1

calling:

R.layout.simple_list
  • 1

    That works too! Thanks for the help. I got the result I wanted by changing my ((Textview) view). setTextColor(R.color.colorverde2); for ((Textview) view). setTextColor(COLOR.GREEN);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.