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 :)  

Mukesh Yadav

Hi Guys I am from Delhi working as Web/Mobile Application Developer(Android Developer), also have knowledge of Roboelctric and Mockito ,android test driven development... Blogging has been my passion and I think blogging is one of the powerful medium to share knowledge and ideas....

3 comments:

  1. plz give some output screens to understand

    ReplyDelete
  2. Hey Mukesh,

    thanks a lot for sharing your work, its been a great help!

    however i tried to bind two textviews to adapter , but they are nor displayed on screen , can you please share code to bind them properly..!!

    please please please help..!!

    ReplyDelete
  3. Thanks Mukesh Brother,
    It is helpful for android developer.
    Can I get this project source code link.

    ReplyDelete

 

Copyright @ 2013 Android Developer Blog.

Blog Widget by LinkWithin