Reputation: 2698
I'm developing an Android Application. I want to be able to add one view by code, that is drawn in top of all the activities on the application.
I've tried to add it to the window manager:
LayoutInflater inflater = activity.getLayoutInflater();
layout = inflater.inflate(R.layout.toast_layout, null);
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.gravity = Gravity.BOTTOM;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.type = WindowManager.LayoutParams.TYPE_TOAST;
final WindowManager mWindowManager = (WindowManager);
activity.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
mWindowManager.addView(layout, params);
However, adding it like this I face two problems:
1.The layout is still displayed when I exit my app.
2.The layout does not respont to Click events.
Is there another solution to achieve this?
Thanks.
Upvotes: 24
Views: 23804
Reputation: 11131
change LayoutParams
type from TYPE_TOAST
to TYPE_APPLICATION
, and delete previous flags I have suggested,
and create one BaseActivity
for all activities in your app, in that Activity in onResume()
add this View to WindowManager
and in onPause()
, remove that View like,
windowManager.removeView(view);
Upvotes: 8
Reputation: 2698
In order to manage the show/view of the view I've used the solution proposed by Gopal. I've attached onStop and onResume events so that the view is hidden when exiting application.
For the click events, I've realized that the type toast does not respond to click events. Therefore I've changed the type
params.type = WindowManager.LayoutParams.TYPE_TOAST;
to
params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
I've to add the following permision too:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
Upvotes: 5
Reputation: 28484
1) Create BaseActivity which extends Activity.
2) Now your all activity should extends BaseActivity instead Activity
3) Override setContentView() method.
4) create blank vertical linearLayout in this method.
5) Add your topView in this layout
6) And then add inflated view in this linearlayout
7) And finally call super.setContentView(passLinearLayoutHere)
How to Implement this ?
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
public class BaseActivity extends Activity {
@Override
public void setContentView(int resId) {
LinearLayout screenRootView = new LinearLayout(this);
screenRootView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
screenRootView.setOrientation(LinearLayout.VERTICAL);
// Create your top view here
View topView = new View(this); // Replace this topview with your view
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View screenView = inflater.inflate(resId, null);
topView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//You will get onclick here of your topview in whatever screen it is clicked
}
});
screenRootView.addView(topView);
screenRootView.addView(screenView);
super.setContentView(screenRootView);
}
}
Upvotes: 9