I want to inflate a layout to view and then add it to WindowManager but before I want to make background of layout to be the same as wallpaper. I've tried this code but sadly WindowManager didn't show my new background.Please, help me!!!
My code:
final WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
final Drawable wallpaperDrawable = wallpaperManager.getDrawable();
RelativeLayout rl = (RelativeLayout) findViewById(R.id.main);
rl.setBackgroundDrawable(wallpaperDrawable);
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS |
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE |
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
WindowManager wm = (WindowManager) getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE);
LayoutInflater inflater = (LayoutInflater) this.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup mTopView = (ViewGroup) inflater.inflate(R.layout.activity_main, null, true);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setAttributes(params);
wm.addView(mTopView, params);
Related
i'm making a feature in my app that display an overlay window with text on the screen, when it appears then i try to touch that window it actually touches behind that window
i need to make that window touchable , which params should i use ?
here is my code
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
LayoutInflater screenNotificationInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
layOverTopviewNotification = screenNotificationInflater.inflate(R.layout.layout_notification, null);
TextView layOverText = (TextView) layOverTopviewNotification.findViewById(R.id.notification_layout);
layOverText.setText(athkarString[completed]);
params = new WindowManager.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
params.x = 0;
params.y = 0;
params.gravity = Gravity.TOP;
windowManager.addView(layOverTopviewNotification, params);
I finally found an answer.
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
inflater = LayoutInflater.from(PlayAthkarService.this);
textView = new TextView(PlayAthkarService.this);
textView.setBackgroundResource(R.drawable.floating_window);
textView.setTextSize(20);
textView.setText(athkarString[completed]);
textView.setTextColor(getResources().getColor(R.color.colorPrimaryDark));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
params = new WindowManager.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
, WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.OPAQUE);
}else {
params = new WindowManager.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
, WindowManager.LayoutParams.TYPE_PRIORITY_PHONE,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.OPAQUE);
}
params.gravity = Gravity.TOP | Gravity.RIGHT ;
params.x = 0;
params.y = 50;
windowManager.addView(textView, params);
Maybe you can try to set textview a touchListener and make textview full screen
In service for displaying custom view over all windows I need to set onClick listener.
#Override public void onCreate() {
super.onCreate();
mContext = this;
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout parent = (LinearLayout) inflater.inflate(R.layout.result_layout,
null);
// Here i need to add onClick listener
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 100;
windowManager.addView(parent, params);
}
I tried to create button lister from inflater but without luck. What is the right approach to set click event listener for selected button by button ID?
Many thanks for any advice.
Resolved using the following code which is works in the service well:
#Override public void onCreate() {
super.onCreate();
mContext = this;
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mParentLayout = (LinearLayout) inflater.inflate(R.layout.result_layout,
null);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 100;
windowManager.addView(mParentLayout, params);
Button b = (Button) mParentLayout.findViewById(R.id.closeButton);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
windowManager.removeView(mParentLayout);
}
});
}
Inspired from:
Could not get Touch event for TYPE_SYSTEM_OVERLAY
Android can play tricks on what you think might work, but doesn't.
First, What I typically do is from onCreate() I place a call to:
InitializePage(); where I then put all my initialize code. It's just my style.
Then in my InitializePage() function I initialize the button, then enter the OnClickListener. I use Android Studio with Intellisense so it writes the code for me....
mSendButton = (TextView)findViewById(R.id.send_button1);
mSendButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{.....
});
It should be that easy. Good luck
How can I show TextView and EditView over the lock screen?
I've tried this and I also add TextView in the layout but nothing has happened
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lock);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
}
then I tried this (in Service) and it didn't work too
#Override
public void onCreate() {
WindowManager.LayoutParams mLayoutParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_FULLSCREEN
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| PixelFormat.TRANSLUCENT);
TextView textView = new TextView(this);
textView.setId('1');
textView.setText("This is a demo");
textView.setTextSize(15);
textView.setGravity(Gravity.CENTER);
WindowManager mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
mWindowManager.addView(textView, mLayoutParams);
}
Help me please
you need to do it like this:
wm = (WindowManager) getSystemService(WINDOW_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
80, 80, WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED|
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.TRANSLUCENT
);
params.gravity = Gravity.CENTER;
params.setTitle("text params");
wm.addView(YourTextView, params);
I used
((WindowManager) context.getSystemService(Service.WINDOW_SERVICE)).removeView(view);
but it didn't work..view is still showing on the screen.
so how to remove WindowManager view from screen?
i Created it like this
public void create()
{
windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
layoutParams = new WindowManager.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT |
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSPARENT);
int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,10, context.getResources().getDisplayMetrics());
layoutParams.x = 265;
layoutParams.y = height;
layoutParams.format = PixelFormat.TRANSLUCENT;
LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.ad_dialog, null);
Button bttnOk = (Button) view.findViewById(R.id.bttn_ok);
Button bttnCancel =(Button) view.findViewById(R.id.bttn_cancel);
bttnOk.setOnClickListener(this);
bttnCancel.setOnClickListener(this);
windowManager.addView(view, layoutParams);
}
windowManager.removeViewImmediate(view);
Make sure you added that view before or it will crash your app
I use Windows Manager to show huge listview. Here is code, that I use:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LayoutInflater ltInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View view = ltInflater.inflate(R.layout.my_layout, null, false);
ListView lv = (ListView) view.findViewById(R.id.ext_fill_list);
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.RIGHT | Gravity.TOP;
mWindowManager.addView(view, params);
ArrayList<String>mItems = new ArrayList<String>(Arrays.asList(ITEMS));
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, mItems);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("ACC", "item clicked");
}
});
}
All works fine, but OnItemClickListener not work. How to solve this problem?
I found solution. Change LayoutParams like this:
mLoginPasswdDialog = inflater.inflate(R.layout.dialog_external_fill_by_copy, null);
WindowManager.LayoutParams lpd_params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT, 10, 10,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FORMAT_CHANGED | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.RGBA_8888);
lpd_params.gravity = Gravity.CENTER_HORIZONTAL | Gravity.RIGHT;
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
mWindowManager.addView(mLoginPasswdDialog, lpd_params);