Popup in Android

In android you can control the view in any away.
A way is with inflater

1
2
3
4
5
6
// Inflate the popup_layout.xml
            LinearLayout viewGroup = (LinearLayout) context
                            .findViewById(R.id.popupLinearLayout);
            LayoutInflater layoutInflater = (LayoutInflater) context
                            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View layout = layoutInflater.inflate(R.layout.activity_popup, viewGroup);

You can download this code: Download code

First step is animation. One for enter and other for exit.

res/anim/enter.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="utf-8"?>
    android:interpolator="@android:anim/decelerate_interpolator" >
 
    <scale
        android:duration="1000"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.0"
        android:toYScale="1.0" />
 
    <alpha
        android:duration="2000"
        android:fromAlpha="0.0"
        android:startOffset="500"
        android:toAlpha="1.0" />
</set>
res/anim/exit.xml
1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="utf-8"?>
 
    <alpha
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="1000"
        android:fillAfter="true"
        android:fromAlpha="1.0"
        android:toAlpha="0" />
 
</set>

Second step: These animations are item in PopupWindowAnimation style.

res/values/styles.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
<resources>
 
    <style name="AppBaseTheme" parent="android:Theme.Light"></style>
 
    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme"></style>
 
    <style name="PopupWindowAnimation" parent="android:Animation">
        <item name="android:windowEnterAnimation">@anim/enter</item>
        <item name="android:windowExitAnimation">@anim/exit</item>
    </style>
 
</resources>

you create the layout with button which show the popup.

res/layout/activity_the_developer_world_is_yours.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >
 
    <Button
        android:id="@+id/open"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show popup" />
 
</LinearLayout>

Now you create the layout of popup.

res/layout/popup_layout.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/popupLinearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPopUp"
    android:gravity="center"
    android:orientation="vertical" >
 
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorPopUpMarcoWindow"
        android:gravity="center"
        android:orientation="vertical" >
 
        <Button
            android:id="@+id/close"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_marginRight="5dp"
            android:background="@color/colorPopUpMarcoWindow"
            android:text="@string/closebutton" />
 
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:background="@color/colorPopUpWindow"
            android:gravity="center"
            android:orientation="vertical" >
 
            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Popup"
                android:textColor="@color/colorPopUpMarcoWindow" />
 
            <ImageButton
                android:id="@+id/imageButton1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/octopus_cat" />
 
            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:text="This is a simple popup"
                android:textColor="@color/colorPopUpMarcoWindow" />
        </LinearLayout>
    </LinearLayout>
 
</LinearLayout>
res/values/strings.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="utf-8"?>
<resources>
 
    <string name="app_name">PopUpAndroid</string>
    <string name="title_activity_the_developer_world_is_yours">TheDeveloperWorldIsYours</string>
    <string name="action_settings">Settings</string>
    <string name="closebutton">Close [x]</string>
 
    <color name="colorPopUp">#BB000000</color>
    <color name="colorPopUpWindow">#000000</color>
    <color name="colorPopUpMarcoWindow">#FFFFFF</color>
 
</resources>

In conclusion, you create the main class. In this class you create a inflater for show popup.

src/TheDeveloperWorldIsYoursActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package com.thedeveloperworldisyours.popup;
 
import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
 
public class TheDeveloperWorldIsYours extends Activity {
    private Point p;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_the_developer_world_is_yours);
        Button popUpButton = (Button) findViewById(R.id.open);
        popUpButton.setOnClickListener(new OnClickListener() {
 
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if (p !=null)
                showPopup(TheDeveloperWorldIsYours.this, p);
            }
        });
 
    }
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
            int[] location = new int[2];
            Button button = (Button) findViewById(R.id.open);
 
            button.getLocationOnScreen(location);
            // Initialize the Point with x, and y positions
            p = new Point();
            p.x = location[0];
            p.y = location[1];
 
    }
 // The method that displays the popup.
 
    private void showPopup(final Activity context, Point p) {
 
            Rect rectgle= new Rect();
            Window window= getWindow();
            window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
            int StatusBarHeight= rectgle.top;
            int contentViewTop=
                window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
            int TitleBarHeight= contentViewTop - StatusBarHeight;
            Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
            int popupWidth = display.getWidth();
            int popupHeight = (display.getHeight()-StatusBarHeight);
 
            // Inflate the popup_layout.xml
            LinearLayout viewGroup = (LinearLayout) context
                            .findViewById(R.id.popupLinearLayout);
            LayoutInflater layoutInflater = (LayoutInflater) context
                            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View layout = layoutInflater.inflate(R.layout.activity_popup, viewGroup);
 
            // Creating the PopupWindow
            final PopupWindow popup = new PopupWindow(context);
            popup.setContentView(layout);
            popup.setWidth(popupWidth);
            popup.setHeight(popupHeight);
            popup.setFocusable(true);
            popup.setAnimationStyle(R.style.PopupWindowAnimation);
 
            // Some offset to align the popup a bit to the right, and a bit down,
            // relative to button's position.
 
            int OFFSET_X = 0;
            int OFFSET_Y = 0;
            // Clear the default translucent background
            popup.setBackgroundDrawable(new BitmapDrawable());
            // Displaying the popup at the specified location, + offsets.
            popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y
                            + OFFSET_Y);
 
            // Getting a reference to Close button, and close the popup when
            // clicked.
            Button close = (Button) layout.findViewById(R.id.close);
            close.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                            popup.dismiss();
                    }
            });
 
    }
 
}

You can download this code: Download code

2 thoughts on “Popup in Android

  1. We’ve learn a number of good things right here. Undoubtedly price tag social bookmarking pertaining to revisiting. I’m wondering the best way a whole lot attempt you put to create this kind of amazing helpful internet site.

Leave a Reply

Your email address will not be published. Required fields are marked *