Find the Window containing a View in Android
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.