Use PopupWindow

Description

The following code shows how to Use PopupWindow.

Example

res\layout\activity_main.xml


<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Show PopupWindow"
        android:onClick="onShowWindowClick" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:text="Show PopupWindow"
        android:onClick="onShowWindowClick" />
</FrameLayout>

res\layout\popup.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="This is a PopupWindow" />
    <EditText
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Close" />
</LinearLayout>

menu main.xml file


<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <group
        android:id="@+id/group"
        android:checkableBehavior="all">
        <item
            android:id="@+id/menu_option_first"
            android:title="First Option"/>
        <item
            android:id="@+id/menu_option_second"
            android:title="Second Option"/>
    </group>
    <item
        android:id="@+id/menu_save"
        android:title="Save"/>
</menu>

MainActivity.java


package com.java2s.myapplication4.app;
//  w  w w  .  j ava  2 s  .  c om
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;

public class MainActivity extends Activity implements View.OnTouchListener {

    PopupWindow mOverlay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        View popupContent = getLayoutInflater().inflate(R.layout.popup, null);

        mOverlay = new PopupWindow();
        mOverlay.setWindowLayoutMode(WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT);
        mOverlay.setWidth(350);
        mOverlay.setHeight(250);
        mOverlay.setContentView(popupContent);
        mOverlay.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_launcher));

    //    mOverlay.setAnimationStyle(R.style.PopupAnimation);
        mOverlay.setTouchInterceptor(this);
        //mOverlay.setOutsideTouchable(true);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mOverlay.dismiss();
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return false;
    }

    public void onShowWindowClick(View v) {
        if (mOverlay.isShowing()) {
            mOverlay.dismiss();
        } else {
            mOverlay.showAsDropDown(v);
        }
    }
}
Use PopupWindow




















Home »
  Android »
    Android UI »




UI Basics
Action Bar
Animation
Button
Canvas
CheckBox
Clock Date Picker
Dialog
EditText
Event
Fragment
Gesture
GridView
ImageView
Layout
ListView
Map
Menu
Model
OpenGL
ProgressBar
RadioButton
Spinner
Tab
TextView
Thread
Toast
Video
View
WebView