2

I use this:

actionBar.setBackgroundColor(ContextCompat.getColor(this, R.color.action_bar));

To use colors from the color.xml .

It works great, but in some Codes it says:

Wrong 1st argument type. Found: 'org.telegram.ui.ActionBar.BaseFragment', required: 'android.content.Context'

But I import android.content.Context:

import android.content.Context;

I tried to use instead of 'this':

actionBar.setBackgroundColor(ContextCompat.getColor(context, R.color.action_bar));

But than Android Studio say :

Cannot resolve symbol 'context'
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245

3 Answers3

8

Use

getActivity().getApplicationContext()

instead of

this
user3673952
  • 698
  • 10
  • 30
2

Fragment is not a subtype of Context.

When inside of a fragment use this:

ContextCompat.getColor(getContext(), R.color.action_bar)

When inside an activity you can use:

ContextCompat.getColor(this, R.color.action_bar)
m02ph3u5
  • 3,022
  • 7
  • 38
  • 51
  • Same like above. Android Studio say: Cannot resolve symbol: 'getContext()' – Yannick - theSpy_007 Mar 05 '16 at 16:37
  • What's `org.telegram.ui.ActionBar.BaseFragment`? If it's a subclass of androids `Fragment` and you haven't messed with `getContext()` or `getActivity()` either should work. Also see http://stackoverflow.com/questions/8215308/using-context-in-a-fragment – m02ph3u5 Mar 06 '16 at 14:59
1

Use :

actionBar.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), 

R.color.action_bar));
Serjik
  • 10,543
  • 8
  • 61
  • 70
Vood ouf
  • 19
  • 4