-1

I found this. So, far no luck. I used his code, but I'm not able to get past this error..I'm not familiar with inflaters. So could someone explain this code?

final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
            WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
            PixelFormat.TRANSLUCENT);

    WindowManager wm = (WindowManager) getApplicationContext()
            .getSystemService(Context.WINDOW_SERVICE);
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    ViewGroup mTopView = (ViewGroup) getApplicationContext()
            .getLayoutInflater().inflate(R.layout.activity_invisible, null);
    getWindow().setAttributes(params);
    wm.addView(mTopView, params);

This part of the code:

ViewGroup mTopView = (ViewGroup) getApplicationContext()
            .getLayoutInflater().inflate(R.layout.activity_invisible, null);

Gives me this error The method getLayoutInflater() is undefined for the type Context

Can someone explain what I'm doing wrong?

Community
  • 1
  • 1
Karthik Balakrishnan
  • 4,353
  • 6
  • 37
  • 69

2 Answers2

1

getLayoutInflater() is a method in Activity, not Context.

You must be using an instance of an Activity to call it, or else it will not work.

However, you can probably use the LayoutInflater inflater you create on the line before the one with the error. Something like:

ViewGroup mTopView = (ViewGroup) inflater.inflate(R.layout.activity_invisible, null);
Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
  • The thing is the link that I've posted in my question uses application context. Or so, I think. Could you check that please? Because his code is `ViewGroup mTopView = (ViewGroup) App.inflater.inflate(R.layout.main, null);` What exactly could `App` be? – Karthik Balakrishnan Jan 15 '13 at 16:46
  • App is likely he name of his Application class. `inflater` is most likely a static LayoutInflater object that he initializes in the onCreate() of his Application class. – Raghav Sood Jan 15 '13 at 16:48
0

If you are trying to execute this code inside onCreate(), instead of

ViewGroup mTopView = (ViewGroup) getApplicationContext().getLayoutInflater().inflate(R.layout.activity_invisible, null);

try

ViewGroup mTopView = (ViewGroup) getLayoutInflater().inflate(R.layout.activity_invisible, null);

Your activity has the getLayoutInflater() method, so there's no need to obtain context explicitly.

Andrii Chernenko
  • 9,873
  • 7
  • 71
  • 89