Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the Window containing a View in Android

I can't find a way to obtain a reference the Window containing an arbitrary View v. I found getWindowToken, but I can't figure out how to use it? Does anyone know how?

Also, does anyone know why it returns an IBinder rather than a Window?

like image 714
Casebash Avatar asked May 07 '10 01:05

Casebash


People also ask

What is Android view view?

What is Android View? A View is a simple building block of a user interface. It is a small rectangular box that can be TextView, EditText, or even a button. It occupies the area on the screen in a rectangular area and is responsible for drawing and event handling.

What is a window in Android?

A window is basically like you think of a window on the desktop. It has a single Surface in which the contents of the window is rendered. An application interacts with the Window Manager to create windows; the Window Manager creates a Surface for each window and gives it to the application for drawing.

What is DecorView?

The DecorView is the view that actually holds the window's background drawable. Calling getWindow(). setBackgroundDrawable() from your Activity changes the background of the window by changing the DecorView's background drawable.

What is SetContentView?

SetContentView is used to fill the window with the UI provided from layout file incase of setContentView(R. layout. somae_file). Here layoutfile is inflated to view and added to the Activity context(Window).


1 Answers

Well... since all views have a reference of the activity that created them (Context), you can use that Context to get a reference of the window. Let me show you this example I wrote some minutes ago:

// main activity import android.app.Activity; import android.os.Bundle; public class GetWindow extends Activity {     /** Called when the activity is first created. */     @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         MyView view = new MyView(this);         view.changeSomethingInWindow(); // keep an eye on this method         setContentView(view);     } } 

Then, inside your view you can do this:

// your view :D import android.app.Activity; import android.content.Context; import android.view.View; import android.view.Window; import android.view.WindowManager;  public class MyView extends View{     public MyView(Context context) {         super(context);     }      @Nullable     private Activity getActivity() {         if (context == null) return null;         if (context instanceof Activity) return (Activity) context;         if (context instanceof ContextWrapper) return getActivity(((ContextWrapper)context).getBaseContext());         return null;     }       public void changeSomethingInWindow(){         // get a reference of the activity         Activity parent = getActivity();         if(parent == null) return;         // using the activity, get Window reference         Window window = parent.getWindow();         // using the reference of the window, do whatever you want :D         window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,             WindowManager.LayoutParams.FLAG_FULLSCREEN);     } } 

In this case, I change the mode the Window is displayed to Fullscreen. Hope this help you. Tell me if you get in trouble with this.

like image 142
Cristian Avatar answered Sep 23 '22 07:09

Cristian