How to create a popup window (PopupWindow) in Android
Here, I am giving you a demo example. See this and customize it according to your need.
public class ShowPopUp extends Activity {
PopupWindow popUp;
boolean click = true;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
popUp = new PopupWindow(this);
LinearLayout layout = new LinearLayout(this);
LinearLayout mainLayout = new LinearLayout(this);
TextView tv = new TextView(this);
Button but = new Button(this);
but.setText("Click Me");
but.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (click) {
popUp.showAtLocation(layout, Gravity.BOTTOM, 10, 10);
popUp.update(50, 50, 300, 80);
click = false;
} else {
popUp.dismiss();
click = true;
}
}
});
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.VERTICAL);
tv.setText("Hi this is a sample text for popup window");
layout.addView(tv, params);
popUp.setContentView(layout);
// popUp.showAtLocation(layout, Gravity.BOTTOM, 10, 10);
mainLayout.addView(but, params);
setContentView(mainLayout);
}
}
Hope this will solve your issue.
Create a popup window with a next button when a user gives the right answer to my question in the quiz
The issue is in the following line
Button btn = findViewById(R.id.button3);
As you are inflating a popupwindow and inside that R.id.button3
is there. So parent of R.id.button3
is popupwindow. The view initialization should look like below
View popupView = inflater.inflate(R.layout.popup_window, null);
Button btn = popupView.findViewById(R.id.button3);
How to create auto popup window in android studio
public void openDialog(View view){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage("Are you sure,You wanted to make decision");
alertDialogBuilder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(MainActivity.this,"You clicked yes button",Toast.LENGTH_LONG).show();
}
});
alertDialogBuilder.setNegativeButton("No",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
Just call this method openDialog(); after your setContentView(R.layout.activity_main);
Example
setContentView(R.layout.activity_main);
openDialog();
How to create PopupWindow on startup in android?
this is how you may show a popup window.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical"
android:id="@+id/layoutTemp">
</LinearLayout>
popup_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="Test Pop-Up" />
</LinearLayout>
main.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new Handler().postDelayed(new Runnable() {
public void run() {
showPopup();
}
}, 100);
}
public void showPopup(){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
PopupWindow pw = new PopupWindow(inflater.inflate(R.layout.popup_layout, null, false), 100, 100, true);
pw.showAtLocation(findViewById(R.id.layoutTemp), Gravity.CENTER, 0, 0);
}
The idea is that your popup will be displayed once your activity is loaded, otherwise it will produce exception Unable to add window -- token null is not valid; is your activity running?. Unless you show popup on a button click, that is the reason I'm showing it after a delay of 100 milliseconds (which is almost unnoticeable).
Problems creating a Popup Window in Android Activity
To avoid BadTokenException, you need to defer showing the popup until after all the lifecycle methods are called (-> activity window is displayed):
findViewById(R.id.main_page_layout).post(new Runnable() {
public void run() {
pw.showAtLocation(findViewById(R.id.main_page_layout), Gravity.CENTER, 0, 0);
}
});
Related Topics
How to Retrieve the Android Sdk Version
Extending Application to Share Variables Globally
Bitmap Recycle with Largeheap Enabled
Updating Progress Dialog in Activity from Asynctask
Android - Out of Memory Exception When Creating Bitmap
Marking Sms Messages as Read/Unread or Deleting Messages Not Working in Kitkat
How to Install Android Sdk Build Tools on the Command Line
How to Make Sticky Headers in Recyclerview? (Without External Lib)
Background Listview Becomes Black When Scrolling
Scale Image to Fill Imageview Width and Keep Aspect Ratio
How to Create a Http Server in Android
Android Failed to Load Js Bundle
Android: How to Create a Dialog Without a Title
Recyclerview - How to Smooth Scroll to Top of Item on a Certain Position
Android Fragment Lifecycle Over Orientation Changes