-1

I want to click outside of the popupwindow to close that. After a research I tried many cases but none worked for me, is there any help I can get from you?

This is my code:

package com.javacodegeeks.android.fragmentstest;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.annotation.SuppressLint;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.DialogInterface;


@SuppressLint("InflateParams")
public class MainActivity extends Activity implements OnClickListener  {

    ImageView mButton1;
    Context contex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      setContentView(R.layout.activity_main);

      mButton1 = (ImageView) findViewById(R.id.button1);
      mButton1.setOnClickListener(new OnClickListener() {


          public void onClick(View arg0) {
            mButton1.setEnabled(false);
            LayoutInflater layoutInflater 
             = (LayoutInflater)getBaseContext()
              .getSystemService(LAYOUT_INFLATER_SERVICE);  
            View popupView = layoutInflater.inflate(R.layout.popupform, null);  
                     final PopupWindow popupWindow = new PopupWindow(
                       popupView, 
                       LayoutParams.WRAP_CONTENT,  
                             LayoutParams.WRAP_CONTENT); 

                      Button btnbutton1 = (Button)popupView.findViewById(R.id.login);
                      btnbutton1.setOnClickListener(new Button.OnClickListener(){

             @Override
             public void onClick(View v) {
              // TODO Auto-generated method stub
              popupWindow.dismiss();
              mButton1 .setEnabled(true); 
             }   

            });



              popupWindow.showAsDropDown(btnbutton1, 50, 250);

        }



      });
    }



    @Override
    public void onBackPressed() {
        new AlertDialog.Builder(this)
            .setTitle("The door is open")
            .setMessage("Are you sure you want to leave?")
            .setPositiveButton("Leave", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();    
            }

        })
        .setNegativeButton("Stay", null)
        .show();
    }
Kaushik
  • 6,150
  • 5
  • 39
  • 54

4 Answers4

0

Try to set setBackgroundDrawable on PopupWindow that should close the window if you touch outside of it.

for example;

popup.setBackgroundDrawable(new BitmapDrawable(getResources(), ""));
Amy
  • 4,034
  • 1
  • 20
  • 34
0

Try this

mButton1.setOnClickListener(new OnClickListener() {


      public void onClick(View arg0) {
        mButton1.setEnabled(false);

        View popupView  = getLayoutInflater().inflate(R.layout.popupform,
                null);

        final PopupWindow popupWindow= new PopupWindow(popupView,
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setBackgroundDrawable(new ColorDrawable(
                android.R.color.transparent));
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);

                  Button btnbutton1 = (Button)popupView.findViewById(R.id.login);
                  btnbutton1.setOnClickListener(new Button.OnClickListener(){

         @Override
         public void onClick(View v) {
          // TODO Auto-generated method stub
          popupWindow.dismiss();
          mButton1 .setEnabled(true); 
         }   

        });



          popupWindow.showAsDropDown(btnbutton1, 50, 250);

    }



  });    
Mukesh Rana
  • 4,051
  • 3
  • 27
  • 39
0

You can use this in onCreate() :

this.setFinishOnTouchOutside(true);
SweetWisher ツ
  • 7,296
  • 2
  • 30
  • 74
Harsh Parikh
  • 3,845
  • 3
  • 14
  • 14
0

Set these two properties :

  myPopupWindow.setBackgroundDrawable(new BitmapDrawable());
  myPopupWindow.setOutsideTouchable(true);

Alternatively:

myPopupWindow.setFocusable(true);
SweetWisher ツ
  • 7,296
  • 2
  • 30
  • 74