«   2024/12   »
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
Tags
more
Archives
Today
Total
관리 메뉴

올해는 머신러닝이다.

Android PopupWindow 관련 팁 본문

Android/Tip&Tech

Android PopupWindow 관련 팁

행복한 수지아빠 2011. 11. 23. 10:24

출처 : http://blog.naver.com/PostView.nhn?blogId=ezmo01&logNo=110093886802

정말 이정도 되는예제 아니면 올리지 말았으면 좋겠다. 쓰레기같은 소스들때문에 허비한 시간이 아깝다.


First, create an XML layout, name it popup_layout.xml and in the res/layout/ folder:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/popup_menu_root"
    android:background="#FFFFFF"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <Button android:id="@+id/popup_menu_button1"
        android:text="close"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <Button android:id="@+id/popup_menu_button2"
        android:text="ok2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <Button android:id="@+id/popup_menu_button3"
        android:text="ok3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

Here I have created a linear layout and put three buttons inside. So that the popup is visible I have made the background white.

Now to the code. You probably already have a class where you need to create a popup. You can declare it wherever you need. I needed to show the popup window after user clicks a button, so I made it class-level and declared in the beginning:

private PopupWindow pw;

Next in the appropriate onClickListener I create the PopupWindow and show it:

// get the instance of the LayoutInflater
LayoutInflater inflater = (LayoutInflater) PopupWindowClass.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// inflate our view from the corresponding XML file
View layout = inflater.inflate(R.layout.popup_menu, (ViewGroup)findViewById(R.id.popup_menu_root));
// create a 100px width and 200px height popup window
pw = new PopupWindow(layout, 100, 200, true);
// set actions to buttons we have in our popup
Button button1 = (Button)layout.findViewById(R.id.popup_menu_button1);
button1.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View vv) {
        // close the popup
        pw.dismiss();
    }
});
Button button2 = (Button)layout.findViewById(R.id.popup_menu_button2);
button2.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View vv) {
        Toast.makeText(PopupWindowClass.this, "Hello", Toast.LENGTH_LONG).show();
    }
});
Button button3 = (Button)layout.findViewById(R.id.popup_menu_button3);
button3.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View vv) {
        finish();
    }
});
// finally show the popup in the center of the window
pw.showAtLocation(layout, Gravity.CENTER, 0, 0);