user3659739 user3659739 - 2 months ago 17
Android Question

Android : programmaticaly setTextColor in TextView in library 22

recently i changed a litle bit of my app and for a reason i don't understand the "setTextColor" method seems that it no longer works.

In my XML, i have a listview and i programmatically add TextViews in this listView.

XML:

<LinearLayout
android:id="@+id/activity_game_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left|top"
android:orientation="vertical"
android:padding="7dp" >
</LinearLayout>


Java:

textView = new TextView(getContext());
textView.setText("some text");
textView.setTextSize(20f);
textView.setGravity(Gravity.CENTER);
textView.setTextColor(Color.BLACK);
textView.setTextAppearance(getContext(), android.R.style.TextAppearance_Medium);
addView(textView);


But for a weird reason this text is white whatever i do.
Someone have an idea for helping me understand why my text is always white ?
Thanks.

Answer

I did try your code and I guess the troubling factor was setTextAppearance. In fact calling setTextColor() after this call fixed the issue. The below code is working perfectly for me:

        TextView textView = new TextView(this);
        textView.setText("some text");
        textView.setTextSize(20f);
        textView.setGravity(Gravity.CENTER);
        // textView.setTextColor(Color.RED);
        textView.setTextAppearance(this, android.R.style.TextAppearance_Medium);
        textView.setTextColor(Color.RED);
        // setContentView(textView);

I don't know the true reason for this issue.

Comments