banner



How To Create Popup Window In Android Studio

How to make a simple Android popup window

This is a fuller example. It is a supplemental answer that deals with creating a popup window in general and not necessarily the specific details of the OP's problem. (The OP asks for a cancel button, but this is not necessary because the user can click anywhere on the screen to cancel it.) It will look like the following image.

enter image description here

Make a layout for the popup window

Add a layout file to res/layout that defines what the popup window will look like.

popup_window.xml

          <?xml version="1.0" encoding="utf-8"?> <RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:background="#62def8">      <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_centerInParent="true"         android:layout_margin="30dp"         android:textSize="22sp"         android:text="This is a popup window."/>  </RelativeLayout>                  

Inflate and show the popup window

Here is the code for the main activity of our example. Whenever the button is clicked, the popup window is inflated and shown over the activity. Touching anywhere on the screen dismisses the popup window.

MainActivity.java

          public class MainActivity extends AppCompatActivity {      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);     }      public void onButtonShowPopupWindowClick(View view) {          // inflate the layout of the popup window         LayoutInflater inflater = (LayoutInflater)                 getSystemService(LAYOUT_INFLATER_SERVICE);         View popupView = inflater.inflate(R.layout.popup_window, null);          // create the popup window         int width = LinearLayout.LayoutParams.WRAP_CONTENT;         int height = LinearLayout.LayoutParams.WRAP_CONTENT;         boolean focusable = true; // lets taps outside the popup also dismiss it         final PopupWindow popupWindow = new PopupWindow(popupView, width, height, focusable);          // show the popup window         // which view you pass in doesn't matter, it is only used for the window tolken         popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);          // dismiss the popup window when touched         popupView.setOnTouchListener(new View.OnTouchListener() {             @Override             public boolean onTouch(View v, MotionEvent event) {                 popupWindow.dismiss();                 return true;             }         });     } }                  

That's it. You're finished.

Going on

Check out how gravity values effect PopupWindow.

PopupWindow bottom gravity with offsets

You can also add a shadow.

PopupWindow with shadow

Further study

These were also helpful in learning how to make a popup window:

  • PopupWindow documentation
  • How To Create Pop Up Window In Android (YouTube video)
  • Android Popup Window Example

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.

are you done with the layout inflating? maybe you can try this!!

          View myPoppyView = pw.getContentView(); Button myBelovedButton = (Button)myPoppyView.findViewById(R.id.my_beloved_button); //do something with my beloved button? :p                  

How To Create Popup Window In Android Studio

Source: https://newbedev.com/how-to-create-a-popup-window-popupwindow-in-android

Posted by: fostersagoonger.blogspot.com

0 Response to "How To Create Popup Window In Android Studio"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel