android code

PopupWindow 예제

paulaner80 2016. 8. 23. 14:26
반응형


사실 MainActivity.java 의 setMyPopupWindow()메소드만 알면 된다.

모든건 거기다 넣어 두었다. 


-. 팝업윈도우의 이동을 위해 touchLinstener를 달았다.

-. 아웃사이드에 터치했을 때 팝업윈도우를 닫아 주기 위해 setBackgroundDrawable()과 setOutsideTouchable(true)를 해 주었다.



layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context="example.mypopupwindownexample.MainActivity">


    <Button

        android:id="@+id/show_popupbutton"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Hello World!" />

</RelativeLayout>



layout/my_popup.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical" android:layout_width="500px"

    android:layout_height="500px"

    android:background="#989898"

    >

    <TextView

        android:id="@+id/textview"

        android:layout_width="match_parent"

        android:layout_height="100px"

        android:text="textview"

        />

    <Button

        android:id="@+id/button"

        android:layout_width="match_parent"

        android:layout_height="100px"

        android:text="button"

        />

</LinearLayout>




MainActivity.java

public class MainActivity extends AppCompatActivity {


    private static final String TAG = "paulaner";


    Button show_popupbutton;

    PopupWindow mPopupWindow;


    int mCurrentX = 20;

    int mCurrentY = 50;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        show_popupbutton = (Button)findViewById(R.id.show_popupbutton);

        show_popupbutton.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                setMyPopupWindow();

            }

        });

    }


    private void setMyPopupWindow(){


        LayoutInflater inflater = LayoutInflater.from(MainActivity.this);


        View popupLayout = inflater.inflate(R.layout.my_popup, null);

        Button buttonInPopup = (Button)popupLayout.findViewById(R.id.button);

        buttonInPopup.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                mPopupWindow.dismiss();


            }

        });


        popupLayout.setOnTouchListener(new View.OnTouchListener() {

            private float mDx;

            private float mDy;


            @Override

            public boolean onTouch(View view, MotionEvent motionEvent) {

                switch (motionEvent.getAction()) {

                    case MotionEvent.ACTION_DOWN:

                        mDx = mCurrentX - motionEvent.getRawX();

                        mDy = mCurrentY - motionEvent.getRawY();

                        break;

                    case MotionEvent.ACTION_MOVE:

                        mCurrentX = (int) (motionEvent.getRawX() + mDx);

                        mCurrentY = (int) (motionEvent.getRawY() + mDy);

                        mPopupWindow.update(mCurrentX, mCurrentY, -1, -1);

                        break;

                }

                return true;

            }

        });



        mCurrentX = getWindow().getDecorView().getWidth()/2;

        mCurrentY = getWindow().getDecorView().getHeight()/2;


        //웬지 가운데가 아님

        Log.i(TAG, "onClick: mCurrentX : "+mCurrentX+", mCurrentY:"+mCurrentY);

        mPopupWindow = new PopupWindow(popupLayout, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);

        mPopupWindow.showAtLocation(popupLayout, Gravity.NO_GRAVITY, mCurrentX,mCurrentY);

        mPopupWindow.setBackgroundDrawable(new ColorDrawable(getResources().getColor(android.R.color.transparent)));

        mPopupWindow.setOutsideTouchable(true);

        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {

            @Override

            public void onDismiss() {

                Log.i(TAG, "onDismiss: ");

            }

        });

    }

}






'android code' 카테고리의 다른 글

android 에서 lombok 설정하기  (0) 2018.02.05
android canvas 지우개  (0) 2016.08.25
canvas에서 selector 적용하기  (0) 2016.08.10
안드로이드 파일 복사 퍼미션  (0) 2016.07.31
서버에 이미지 업로드 하는 예제  (0) 2016.07.29