Showing posts with label Dismiss popup window on back pressed in android. Show all posts
Showing posts with label Dismiss popup window on back pressed in android. Show all posts

Thursday 6 December 2012

Close Popup window on click of backbuttun in android

 Hello Dorid Guys,  
 Yesterday, I stuck in a weird situation , the android popup button will not closed on pressing back  
 button . On android physical back button pressed I am calling popupWindow.dismiss();  
 But it not works for me :( .  
 After spending 1-2 hrs on it , finally I Solved this issue.  
 I am Doing following three steps :  
 1. Initializing the popup window first:  
    View view = LayoutInflater.from(MyDialogActivity.this).inflate(R.layout.poup_icon, null);  
    popupWindow = new PopupWindow(view, LayoutParams.WRAP_CONTENT,    
                 LayoutParams.WRAP_CONTENT, true);  
 2. After initialization call this  
     popupWindow.setBackgroundDrawable(new BitmapDrawable());   
 3. Finally, at the end inside your back button pressed code call this  
     @Override  
      public void onBackPressed() {  
       if(popupWindow != null)  
         popupWindow.dismiss();  
       else  
         super.onBackPressed();  
      }  
 Here is my full source code:  
 package com.popup.activities;  
 import android.app.Activity;  
 import android.content.Context;  
 import android.os.Bundle;  
 import android.widget.ImageView;  
 import android.widget.PopupWindow;  
 public class MyDialogActivity extends Activity {  
  Context mContext = MyDialogActivity.this;  
  PopupWindow popupWindow;  
  ImageView emojiIcon;  
  @Override  
  public void onCreate(Bundle savedInstanceState) {  
   super.onCreate(savedInstanceState);  
       setContentView(R.layout.activity_my_dialog);  
   ImageView emojiIcon = (ImageView) findViewById(R.id.motekon_icon);  
   //onclick of icom  
   emojiIcon.setOnClickListener(new OnClickListener() {  
   @Override  
     public void onClick(View v) {  
  View view = LayoutInflater.from(mContext).inflate(R.layout.popup_        icon, null);  
  popupWindow = new PopupWindow(view, LayoutParams.MATCH_PARENT,  
           LayoutParams.WRAP_CONTENT, true);  
      popupWindow.setBackgroundDrawable(new BitmapDrawable());  
      popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);  
      final GridView gridView = (GridView)view.  
                     findViewById(R.id.gridView1);  
  gridView.setOnItemClickListener(new OnItemClickListener() {  
  public void onItemClick(AdapterView<?> arg0, View v,   
                      int position, long arg3) {  
   popupWindow.dismiss();  
  }  
  });  
      gridView.setAdapter(your adapter);  
  }  
  });  
   }  
   @Override  
   protected void onPause() {  
  super.onPause();  
  if (popupWindow != null)  
  popupWindow.dismiss();  
   }  
  @Override  
  public void onBackPressed() {  
   if (popupWindow != null)  
  popupWindow.dismiss();  
   else  
     super.onBackPressed();  
  }  
 }  
 This is what I am doing in my code and it solve my problem.  
 May this will help some one.  
 Enjoy Coding :)  

 

Copyright @ 2013 Android Developers Blog.