Easy Tutorial
❮ Android Tutorial Webview Cache Verilog2 Gate ❯

2.6.1 Basic Usage of PopupWindow (Floating Box)

Category Android Basic Tutorial

Introduction:

>

This section introduces the last UI control for displaying information—PopupWindow (Floating Box). If you want to see what it looks like, you can open your phone's QQ, long-press an item in the list, and a small black dialog box will pop up. This is a PopupWindow. Unlike AlertDialog, its position can be arbitrary.

AlertDialog is non-blocking, while PopupWindow is blocking! The official introduction to PopupWindow is:

A popup window that can be used to display an arbitrary view. The popup window is

a floating container that appears on top of the current activity.

Roughly意思是: A popup window control that can display any View and floats above the current activity.

Let's learn about this control below.

Official documentation: PopupWindow


1. Interpretation of Related Methods


1) Several Commonly Used Constructors

>

We can see in the documentation that there are nine constructors for PopupWindow, but here are only a few commonly used ones in actual development:

-public PopupWindow (Context context)

-public PopupWindow(View contentView, int width, int height)

-public PopupWindow(View contentView)

-public PopupWindow(View contentView, int width, int height, boolean focusable)

No need to explain the parameters, contentView is the View displayed by PopupWindow, and focusable determines whether it shows focus.


2) Some Commonly Used Methods

>

Here are a few commonly used methods, others can be found in the documentation:

-setContentView (View contentView): Set the View displayed by PopupWindow

-getContentView (): Get the View displayed by PopupWindow

-showAsDropDown(View anchor): Position relative to a control (directly below to the left), no offset

-showAsDropDown(View anchor, int xoff, int yoff): Position relative to a control, with offset

-showAtLocation(View parent, int gravity, int x, int y): Position relative to the parent control (e.g., center Gravity.CENTER, bottom Gravity.BOTTOM), with or without offset. PS: The parent parameter can be any view in the activity!

-setWidth/setHeight: Set the width and height, which can also be specified in the constructor. In addition to specific values, you can also use WRAP_CONTENT or MATCH_PARENT. The width and height attributes of popupWindow correspond directly to the first layer of View.

-setFocusable(true): Set focus. After PopupWindow pops up, all touch and physical key events are handled by PopupWindows. Any other event response must occur after PopupWindow disappears (except for system-level events like home). For example, when such a PopupWindow appears, pressing the back key first makes the PopupWindow disappear, and the second press exits the activity. To be precise, to exit the activity, you must first make the PopupWindow disappear, because not all back key presses will make the PopupWindow disappear; it must be set with a background.

-setAnimationStyle(int): Set animation effects


2. Code Example

Running Effect Diagram:

btn_hehe.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Toast.makeText(MainActivity.this, "You clicked hehe~", Toast.LENGTH_SHORT).show();
        popWindow.dismiss();
    }
});

3. Sample Code Download

PopWindowDemo.zip


Summary:

>

Due to time constraints, I didn't come up with a better example, so I wrote a simple demo that should meet basic needs. Additionally, if you want to delve deeper into PopupWindow, you can refer to the following references: Android PopupWindow Usage and Analysis Android PopupWindow Detailed Explanation

-1.0 Android Basic Introduction Tutorial

-1.0.1 2015 Latest Android Basic Introduction Tutorial Table of Contents

-1.1 Background and System Architecture Analysis

-1.2 Development Environment Setup

-1.2.1 Developing Android Apps with Eclipse + ADT + SDK

-1.2.2 Developing Android Apps with Android Studio

-1.3 Solving SDK Update Issues

-1.4 Genymotion Emulator Installation

-1.5.1 Git Tutorial for Basic Local Repository Operations

-1.5.2 Using GitHub to Set Up a Remote Repository with Git

-1.6 How to Use 9-Patch Images

-1.7 Interface Prototype Design

-1.8 Project Source Analysis (Various Files, Resource Access)

-1.9 Signing and Packaging Android Applications

-1.11 Decompiling APK to Retrieve Code and Resources

-2.1 Concepts of View and ViewGroup

-2.2.1 LinearLayout (Linear Layout)

-2.2.2 RelativeLayout (Relative Layout)

-2.2.3 TableLayout (Table Layout)

-2.2.4 FrameLayout (Frame Layout)

-2.2.5 GridLayout (Grid Layout)

-2.2.6 AbsoluteLayout (Absolute Layout)

-2.3.1 Detailed Explanation of TextView (Text Box)

-2.3.2 Detailed Explanation of EditText (Input Box)

-2.3.3 Button and ImageButton

-2.3.4 ImageView (Image View)

-2.3.5 RadioButton (Radio Button) & Checkbox (Checkbox)

-2.3.6 ToggleButton and Switch

-2.3.7 ProgressBar (Progress Bar)

-2.3.8 SeekBar (Drag Bar)

-2.3.9 RatingBar (Star Rating Bar)

-2.4.1 ScrollView (Scroll Bar)

-2.4.2 Date & Time Components (Part 1)

-2.4.3 Date & Time Components (Part 2)

-2.4.4 Adapter Basics

-2.4.5 Simple Usage of ListView

-2.4.6 Optimization of BaseAdapter

-2.4.7 Focus Issues with ListView

-2.4.8 Solving Checkbox Misalignment Issues in ListView

-2.4.9 Data Update Issues with ListView

-2.5.0 Building a Reusable Custom BaseAdapter

-2.5.1 Implementing Multi-Layout ListView Items

-2.5.2 Basic Usage of GridView (Grid View)

-2.5.3 Basic Usage of Spinner (List Option Box)

-2.5.4 Basic Usage of AutoCompleteTextView (Auto-Complete Text Box)

-2.5.5 Basic Usage of ExpandableListView (Collapsible List)

-2.5.6 Basic Usage of ViewFlipper (Flip View)

-2.5.7 Basic Usage of Toast

-2.5.8 Detailed Explanation of Notification (Status Bar Notification)

-2.5.9 Detailed Explanation of AlertDialog (Dialog Box)

-2.6.0 Basic Usage of Other Common Dialogs

-2.6.2 Menu (Menu)

-2.6.3 Simple Usage of ViewPager

-2.6.4 Simple Usage of DrawerLayout (Official Sliding Menu)

-3.1.1 Event Handling Mechanism Based on Listeners

-3.2 Event Handling Mechanism Based on Callbacks

-3.3 Analysis of Handler Message Passing Mechanism

-3.4 TouchListener vs OnTouchEvent + Multi-Touch

-3.5 Listening for Content Changes in EditText

-3.6 Responding to System Settings Events (Configuration Class)

-3.7 AsyncTask Asynchronous Task

-3.8 Gestures (Gestures)

-4.1.1 Beginning with Activity

-4.1.2 Intermediate with Activity

-4.1.3 Advanced with Activity

-4.2.1 Introduction to Service

-4.2.2 Intermediate with Service

-4.2.3 Advanced with Service -4.3.1 BroadcastReceiver Getting Started

-4.3.2 BroadcastReceiver Deep Dive

-4.4.1 ContentProvider Introduction

-4.4.2 ContentProvider Further Exploration — Document Provider

-4.5.1 Basic Usage of Intent

-4.5.2 Passing Complex Data with Intent

-5.1 Fragment Basic Overview

-5.2.1 Fragment Example Walkthrough — Bottom Navigation Bar Implementation (Method 1)

-5.2.2 Fragment Example Walkthrough — Bottom Navigation Bar Implementation (Method 2)

-5.2.3 Fragment Example Walkthrough — Bottom Navigation Bar Implementation (Method 3)

-5.2.4 Fragment Example Walkthrough — Bottom Navigation Bar + ViewPager Swipe to Switch Pages

-5.2.5 Fragment Example Walkthrough — Simple Implementation of News (Shopping) App List Fragment

-6.1 Data Storage and Access — File Storage and Reading/Writing

-6.2 Data Storage and Access — SharedPreferences for Saving User Preferences

-6.3.1 Data Storage and Access — Introduction to SQLite Database

-6.3.2 Data Storage and Access — Further Exploration of SQLite Database

-7.1.1 Android Network Programming Essentials and HTTP Protocol Study

-7.1.2 Android HTTP Request Headers and Response Headers Study

-7.1.3 Android HTTP Request Method: HttpURLConnection

-7.1.4 Android HTTP Request Method: HttpClient

-7.2.1 Android XML Data Parsing

-7.2.2 Android JSON Data Parsing

-7.3.1 Android File Upload

-7.3.2 Android File Download (1)

-7.3.3 Android File Download (2)

-7.4 Android Calling WebService

-7.5.1 WebView (Web View) Basic Usage

-7.5.2 WebView and JavaScript Interaction Basics

-7.5.3 Important Considerations for WebView in Android 4.4 and Later

-7.5.4 WebView File Download

-7.5.5 WebView Cache Issues

-7.5.6 WebView Handling Webpage Error Code Information

-7.6.1 Socket Network Basics Preparation

-7.6.2 TCP Protocol Based Socket Communication (1)

-7.6.3 TCP Protocol Based Socket Communication (2)

-7.6.4 UDP Protocol Based Socket Communication

-8.1.1 Summary of 13 Drawable Types in Android Part 1

-8.1.2 Summary of 13 Drawable Types in Android Part 2

-8.1.3 Summary of 13 Drawable Types in Android Part 3

-8.2.1 Bitmap (Bitmap) Comprehensive Analysis Part 1

-8.2.2 OOM Issues Caused by Bitmap

-8.3.1 Detailed Explanation of Three Drawing Tool Classes

-8.3.2 Drawing Class Practical Examples

-8.3.3 Paint API — MaskFilter (Mask)

-8.3.4 Paint API — Xfermode and PorterDuff Detailed Explanation (Part 1)

-8.3.5 Paint API — Xfermode and PorterDuff Detailed Explanation (Part 2)

-8.3.6 Paint API — Xfermode and PorterDuff Detailed Explanation (Part 3)

-8.3.7 Paint API — Xfermode and PorterDuff Detailed Explanation (Part 4)

-8.3.8 Paint API — Xfermode and PorterDuff Detailed Explanation (Part 5)

-8.3.9 Paint API — ColorFilter (Color Filter) (1/3)

-8.3.10 Paint API — ColorFilter (Color Filter) (2/3)

-8.3.11 Paint API — ColorFilter (Color Filter) (3/3)

-8.3.12 Paint API — PathEffect (Path Effect)

-8.3.13 Paint API — Shader (Image Rendering)

-8.3.14 Paint Enum/Constant Values and ShadowLayer Shadow Effect

-8.3.15 Paint API — Typeface (Font Style)

-8.3.16 Canvas API Detailed Explanation (Part 1)

-8.3.17 Canvas API Detailed Explanation (Part 2) Clipping Methods Collection

-8.3.18 Canvas API Detailed Explanation (Part 3) Matrix and drawBitmapMesh

-8.4.1 Android Animation Collection — Frame Animation

-8.4.2 Android Animation Collection — Tween Animation

-8.4.3 Android Animation Collection — Property Animation — Introduction

-8.4.4 Android Animation Collection — Property Animation — Further Exploration

-9.1 Using SoundPool to Play Sound Effects (Duang~)

-9.2 MediaPlayer for Audio and Video Playback

-9.3 Using Camera to Take Photos

-9.4 Using MediaRecord to Record Audio

-10.1 TelephonyManager (Telephony Manager)

-10.2 SmsManager (SMS Manager)

-10.3 AudioManager (Audio Manager)

-10.4 Vibrator (Vibrator)

-10.5 AlarmManager (Alarm Service)

-10.6 PowerManager (Power Service)

-10.7 WindowManager (Window Management Service)

Follow on WeChat

❮ Android Tutorial Webview Cache Verilog2 Gate ❯