I'm developing lock screen. And I need my lock screen activity be over all apps.
So, I have this:
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View oView = layoutInflater.inflate(R.layout.lock_screen, null); // lock_screen is .xml file
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) getSystemService(WINDOW_SERVICE);
wm.addView(oView, params);
It works, but not at all. Everything is frozen, the navigation baris working, but touch screen is not. How can I make only my activity be over everything and can't quit it?
This is because you've only overlaid a view, not the activity.
Related
Good afternoon. I have a service in which the object is created and WindowManager View - is assigned parameter background (mView.setBackgroundColor(color). The fact is that now I have View drawn only over the main desktop screen and status bar, and navigation bar is not affected. How do I do to View draws and on top of the navigation bar (buttons) in the Android? At the moment:
#Override
public void onCreate() {
super.onCreate();
mView = new LinearLayout(this);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
0 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT);
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(mView, params);
}
Result:
enter image description here
That is all I have to paint in red, relatively speaking, including a navigation bar. Thank you.
int value_below_screen=100;
params.y=-value_below_screen;
this will offset the screen below by "value_below_screen"
I want to open a PopUpWindow in another PopUpWindow. I have an ImageButtons in my MainActivity. When I click on it a PopUpWindow appears. I use it as a kind of submenu in my app. In my first PopupWindow is another ImageButton. If I click on it a second PopupWindow should appear and overlay the first one.
Opening the first PopupWindow works just fine. When I click on the button in it to open the second one, the app crashes. How can I make the second PopupWindow work?
Thanks for your help.
I tried it likes this:
final ImageButton btnOpenPopup = (ImageButton) findViewById(R.id.button_name);
btnOpenPopup.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater layoutInflater
= (LayoutInflater) getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup_fertig, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
popupWindow.showAtLocation(btnOpenPopup, Gravity.TOP | Gravity.RIGHT, 0, 0);
Button btn_2 = (Button) popupView.findViewById(R.id.button_2);
btn_2.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
LayoutInflater layoutInflater_2
= (LayoutInflater) getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView_2 = layoutInflater.inflate(R.layout.popup_2, null);
final PopupWindow popupWindow_2 = new PopupWindow(
popupView_2,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
popupWindow_2.showAtLocation(btn_2, Gravity.TOP | Gravity.RIGHT, 0, 0);
}
}
});
}
I'm also trying to do what you are doing but was not successful however I did figure out a workaround for what you want to do.
Inside your popupView xml layout, you would have to create a framelayout as the parent layout and then put both your popupView and popupView 2 layout within the parent layout. You would then switch on and off the visibility for each of the two layouts when a button is pressed in the popupView.
It actually works quite nicely, the popupView resizes itself appropriately according to the content it holds with smooth animations.
As a bit of background on this - I created a layout for a user interface that sat ontop of a GLSurfaceview. As I progressed I wanted to add adverts from admob on top of the GLSurfaceview. After some reading it seemed like the only way to do this was to create a new Layout view then add each dynamically created view to this. Then set the new Layout view as the content view. This means that all the views in my UI are dynamically created in java code.
My question is:
Is there any way you can use a view from an xml layout that is not set as the content view and somehow add it to the content view just like you would when dynamically creating a view?
I did try this but got null pointer exceptions from which ever view I tried to findViewById.
Any sort of write up explaining this would be helpful
EDIT added code
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Create an ad.
adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId(AD_UNIT_ID);
RelativeLayout layout = new RelativeLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("******************************").build();
adView.loadAd(adRequest);
RelativeLayout.LayoutParams adParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
adParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
final TextView Score = new TextView(this);
Score.setText(" 0");
RelativeLayout.LayoutParams scoreParams = new
RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Score.setLayoutParams(scoreParams);
Typeface tf = Typeface.createFromAsset(getAssets(),"Fonts/BOYCOTT_.ttf");
Score.setTextSize(getResources().getDimension(R.dimen.textsize));
Score.setTypeface(tf);
Score.setTextColor(0xFFFFFFFF);
// Check if the system supports OpenGL ES 2.0.
final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
final boolean supportsEs2 = configurationInfo.reqGlEsVersion >= 0x20000;
if (supportsEs2)
{
// Request an OpenGL ES 2.0 compatible context.
mGLSurfaceView = new RBGLSurfaceView(this); new GLSurfaceView(this);
mGLSurfaceView.setEGLContextClientVersion(2);
final DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
mGoogleApiClient = getApiClient();
mGLSurfaceView.setRenderer(new RBRenderer(this, Score, mGoogleApiClient),displayMetrics);
layout.addView(mGLSurfaceView);
layout.addView(Score, scoreParams);
layout.addView(adView, adParams);
//Set main renderer
setContentView(layout);
}
else
{
// This is where you could create an OpenGL ES 1.x compatible
// renderer if you wanted to support both ES 1 and ES 2.
return;
}
}
Ok so the above code is creating an adview, textview and surface view and adding them to the layout which is then getting set as the contentview - works great.
But is it possible to add the text view from an xml layout even if this xml layout is not set as the contentview?
Doing this doesnt work
final TextView test= (TextView) findViewById(R.id.test);
// set properties here etc
layout.adView(test);
because 'test' is null because the layout in which the view 'test' belongs to is not the set as the content view.
is it possible to add the text view from an xml layout even if this xml layout is not set as the contentview?
Yes, it's totally possible! Try this:
LayoutInflater inflater = (LayoutInflater) this.getSystemService(this.LAYOUT_INFLATER_SERVICE);
View textView = inflater.inflate(R.layout.text_view, null);
layout.addView(textView);
Hope this helps!
So I'm working on an Android app (in Eclipse) and I've hit a wall. In my app I have a drawer that slides out with a list of options. I would like the user to be able to click one of the options and to bring up a floating window with a form in it. I'm trying to do this using the onClick attribute on the buttons rather than using a onClickListener. Is this possible without having to use a onClickListener or am I trying to avoid the inevitable? The button's onClick attribute in my layout has a value of "newWindow".
My MainActivity class
public void newWindow(View v){
Intent intent = new Intent(){
LayoutInflater layoutInflater
= (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
};
startActivity(intent);
}
I see my popupView variable is unused but I'm not sure where to place or if I'm even headed in the right direction. Thanks in advance for the help!
I can't for the life of me figure out why you are trying to start an Activity with an Intent here. Your code shold be:
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(popupView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
popupWindow.showAtLocation(...); // or showAsDropdown(...)
My applications draws an overlay which has a GridView as it's child. All the clicks are being captured by the overlay veiw's onTouch event and nothing is passed to the GridView's onItemClick event. The code which creates it is:
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
TaskBarView = inflater.inflate(R.layout.view_overlay, null);
TaskBarViewParams = new WindowManager.LayoutParams(TaskBarInactiveWidth, TaskBarHeight,
LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
TaskBarViewParams.gravity = Gravity.LEFT | Gravity.TOP;
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
windowManager.addView(TaskBarView, TaskBarViewParams);
Note: This problem only comes into picture on API levels 16 and up. The GridView captures it's onItemClick and the overlay captures it's onTouch properly as it should below API level 16.
Am I missing a flag or something?