Search code examples
androidmethodsandroid-activitylistenerwindow-managers

How to call getWindow() using a method from a listener


I'm using getwindow to set a lot of stuff at the beginning of bunch of my activities within my onCreate. I would like to replace this with a method like the following:

    public static void initializeScreen (Activity This){
    This.requestWindowFeature(Window.FEATURE_NO_TITLE);
    if (global.notBarOnOff == true) {
        This.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        This.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);}
    //Keeps Screen on
    This.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    // Controls Screen Brightness
    Settings.System.putInt(This.getContentResolver(), SCREEN_BRIGHTNESS_MODE, SCREEN_BRIGHTNESS_MODE_MANUAL);
    final WindowManager.LayoutParams settings = This.getWindow().getAttributes();
    settings.screenBrightness = (float) 0.10;   
    This.getWindow().setAttributes(settings);
}

Now I think this will work just fine by calling SCREEN_Controller.initializeScreen(this)

Later I let the user change the brightness by pressing a button.

bLight.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            settings.screenBrightness = (float) 1;
            getWindow().setAttributes(settings);

I know to use my initializeScreen I would have to add the following to my listener

WindowManager.LayoutParams settings = This.getWindow().getAttributes();

So I would like to replace that with a method as well from my SCREEN_Controller class. The problem is I can't use the (Activity This) that I use previously because it wouldn't let me call the Activity in the setOnClickListener with using new. What am I doing wrong?


Solution

  • You can refer to the activity this in the internal class like this ( :-) ):

    YourClassName.this
    

    Two more notes:

    • In Java, there's this (all lower case), not This.
    • Instead of writing this.getWindow().getAttributes();, you could just write getWindow().getAttributes();. As a general rule, if there was no declaration of a variable with the same name in a lower scope, then you can refer to the variable without this.

    Short example:

    public class DontMissTwice extends Activity {
    
        @override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.shaving);
            TextView knife = (TextView)findViewById(R.id.knife);
            knife.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    View.OnClickListener thisListener = this; // refers to View.OnClickListener's object
                    DontMissTwice thisDontMissTwice = DontMissTwice.this // refers to DontMissTwice's object
                }
            });
        }
    }