0

XML

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000"
app:titleTextColor="#ffffff"
>
<TextView
    android:id="@+id/name"
    android:layout_width="275dp"
    android:layout_height="match_parent" />
</android.support.v7.widget.Toolbar>

Activity

public class Chat extends AppCompatActivity {

private TextView name;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_chat);

    name = (TextView) findViewById(R.id.name);

    TextView textView = new TextView(R.layout.activity_chat);
    name.setText((getIntent().getStringExtra("Recievers_Name")));
    name.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.white));

I tried

name.setTextColor(getResources().getColor(R.color.white));

  name.setTextColor(color.WHITE);

  name.setTextColor(Color.parseColor("#FFFFFF"));

But not working... please help.................................................................

5 Answers5

0

Try this it is working..

public class LayoutActivity extends AppCompatActivity {
    TextView textView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);
        textView=findViewById(R.id.textView);
        textView.setTextColor(Color.RED);
    }
}
Tatranskymedved
  • 4,194
  • 3
  • 21
  • 47
0
textView.setTextColor(view.getResources().getColor(R.color.red_1));

This should work for most Api versions

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
0

getResources().getColor() is deprecated. Use this

TextView name = (TextView) findViewById(R.id.textview_name_id);
name.setTextColor(ContextCompat.getColor(context, R.color.black));
Lokesh Pandey
  • 1,739
  • 23
  • 50
0

I think you are missing TextView declaration and initialization in your activity.
Declare it as a variable of your activity, and initialize it after calling setContentView().

public class MainActivity extends Activity {

    private TextView mNameTextView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_launch);
        mNameTextView = (TextView)findViewById(R.id.name);
        // remaining codes
       mNameTextView.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.black));
  }
}
vm345
  • 813
  • 12
  • 28
0

Try this method it's work fine on all Api levels.

public int _getColor(int id) {
    final int version = Build.VERSION.SDK_INT;
    if (version >= 23) {
        return ContextCompat.getColor(this, id);
    } else {
        return getResources().getColor(id);
    }
}