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
  • 151,199
  • 17
  • 111
  • 216

3 Answers3

9

Use

getActivity().getApplicationContext()

instead of

this
user3673952
  • 597
  • 7
  • 26
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
  • 2,837
  • 6
  • 38
  • 49
  • 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
  • 9,899
  • 7
  • 60
  • 69
Vood ouf
  • 19
  • 4