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.
This example demonstrates How to Dynamically Add Views into View. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
An Android app contains one or more activities. An Android activity is a screen, similar to windows in a desktop application. Inside an activity you can have GUI components. The GUI components are instances of View or ViewGroup subclasses.
View is Display System of Android where you define layout to put subclasses of View in it eg. Buttons, Images etc. But Activity is Screen System of Android where you put display as well as user-interaction,(or whatever which can be contained in full-screen Window.)
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With