user3132352
user3132352

Reputation: 403

Android how to create popup window

I need to create a popup window with buttons and a button that will close the popup. I found some tutorials but I couldn't find out how to do the implementation.

What I want to do: Click an action button and the popup shows and when I click the the close button the popup window must close.

There were an onCreate method in the tuorials and I didn't understand how is it called.

Can somebody give an example of an popup implementation or a link to a good tutorial? Thank you!

Upvotes: 6

Views: 59962

Answers (2)

Dhina k
Dhina k

Reputation: 1489

private void callPopup() {

    LayoutInflater layoutInflater = (LayoutInflater)getBaseContext()
        .getSystemService(LAYOUT_INFLATER_SERVICE);

    View popupView = layoutInflater.inflate(R.layout.popup, null);

    popupWindow=new PopupWindow(popupView,
        LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT,        
        true);

    popupWindow.setTouchable(true);
    popupWindow.setFocusable(true);

    popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);
    Name = (EditText) popupView.findViewById(R.id.edtimageName);

    ((Button) popupView.findViewById(R.id.saveBtn))
        .setOnClickListener(new OnClickListener() {

        @TargetApi(Build.VERSION_CODES.GINGERBREAD)
        public void onClick(View arg0) {
            Toast.makeText(getApplicationContext(),
                Name.getText().toString(),Toast.LENGTH_LONG).show();

            popupWindow.dismiss();
        }
    });

    ((Button) popupView.findViewById(R.id.cancelbtutton))
        .setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            popupWindow.dismiss();
        }
    });
}

Upvotes: 6

Zeeshan Khan
Zeeshan Khan

Reputation: 563

private void showPopup(){
    Button btn_closepopup=(Button)layout.findViewById(R.id.btn_closePoppup);
    pwindo=new PopupWindow(layout,480,500,true);
    pwindo.showAtLocation(layout, Gravity.CENTER, 0, 40);
    chartContainer1.addView(mChart);
    btn_closepopup.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            pwindo.dismiss();
        }
    });
}

Upvotes: 7

Related Questions