how to create a popup window 28popupwindow 29 in android

Solutions on MaxInterview for how to create a popup window 28popupwindow 29 in android by the best coders in the world

showing results for - "how to create a popup window 28popupwindow 29 in android"
Abril
16 May 2019
1public class MainActivity extends AppCompatActivity {
2
3    @Override
4    protected void onCreate(Bundle savedInstanceState) {
5        super.onCreate(savedInstanceState);
6        setContentView(R.layout.activity_main);
7    }
8
9    public void onButtonShowPopupWindowClick(View view) {
10
11        // inflate the layout of the popup window
12        LayoutInflater inflater = (LayoutInflater)
13                getSystemService(LAYOUT_INFLATER_SERVICE);
14        View popupView = inflater.inflate(R.layout.popup_window, null);
15
16        // create the popup window
17        int width = LinearLayout.LayoutParams.WRAP_CONTENT;
18        int height = LinearLayout.LayoutParams.WRAP_CONTENT;
19        boolean focusable = true; // lets taps outside the popup also dismiss it
20        final PopupWindow popupWindow = new PopupWindow(popupView, width, height, focusable);
21
22        // show the popup window
23        // which view you pass in doesn't matter, it is only used for the window tolken
24        popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
25
26        // dismiss the popup window when touched
27        popupView.setOnTouchListener(new View.OnTouchListener() {
28            @Override
29            public boolean onTouch(View v, MotionEvent event) {
30                popupWindow.dismiss();
31                return true;
32            }
33        });
34    }
35}
36