Tuesday, April 14, 2015

Auto dismiss popupWindow with AsyncTask

This example we will open PopupWindow together with a AsyncTask. Once the AsyncTask count for 10 seconds, it will dismiss the PopupWindow; to show how to dismiss PopupWindow outside itself.




/res/layout/popup.xml, the layout of the PopupWindow.
package com.example.androidpopupwindow;

import android.support.v7.app.ActionBarActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;

public class MainActivity extends ActionBarActivity {

 LinearLayout mainLayout;
 ProgressBar progressBar;
 TextView msg;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  mainLayout = (LinearLayout)findViewById(R.id.mainlayout);

  progressBar = (ProgressBar) findViewById(R.id.progressbar);
  msg = (TextView)findViewById(R.id.msg);
  
  final Button btnOpenPopup = (Button) findViewById(R.id.openpopup);
  btnOpenPopup.setOnClickListener(new Button.OnClickListener() {

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

    Button btnDismiss = (Button) popupView
      .findViewById(R.id.dismiss);
    btnDismiss.setOnClickListener(new Button.OnClickListener() {

     @Override
     public void onClick(View v) {
      // TODO Auto-generated method stub
      popupWindow.dismiss();
     }
    });
    
    TextView me = (TextView) popupView
      .findViewById(R.id.me);
    me.setText(popupWindow.toString());

    //Center the PopupWindow
    popupWindow.showAtLocation(mainLayout, Gravity.CENTER, 0, 0);

    //Auto dismiss PopupWindow in 10 sec
    new DismissAsyncTask(progressBar, msg, popupWindow).execute();
   }
  });
 }

 public class DismissAsyncTask extends AsyncTask<Void, Integer, Void> {

  ProgressBar taskProgressBar;
  TextView taskMsg;
  PopupWindow taskPopupWindow;

  public DismissAsyncTask(ProgressBar targetProgressBar,
    TextView targetMsg,
    PopupWindow targetPopupWindow) {
   taskProgressBar = targetProgressBar;
   taskMsg = targetMsg;
   taskProgressBar.setVisibility(View.VISIBLE);
   taskPopupWindow = targetPopupWindow;
  }

  @Override
  protected Void doInBackground(Void... params) {
   for (int i = 0; i < 10; i++) {
    publishProgress(i);
    SystemClock.sleep(1000);
   }
   return null;
  }

  @Override
  protected void onProgressUpdate(Integer... values) {
   taskProgressBar.setVisibility(View.VISIBLE);
   taskProgressBar.setProgress(values[0]);
   
   taskMsg.setText("Updated by : " 
    + taskPopupWindow.toString() 
    + " - " + values[0]);
  }

  @Override
  protected void onPostExecute(Void result) {
   Toast.makeText(MainActivity.this, 
    "Dismiss PopupWindows: " + taskPopupWindow.toString(), 
    Toast.LENGTH_LONG).show();
   
   taskProgressBar.setVisibility(View.INVISIBLE);
   taskPopupWindow.dismiss();
  }
 }

}

/res/layout/activity_main.xml, the layout of the main activity.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mainlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    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="com.example.androidpopupwindow.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />

    <Button
        android:id="@+id/openpopup"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Open Popup Window" />
    
    <ProgressBar
        android:id="@+id/progressbar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:max="10"
        android:progress="0"
        android:visibility="invisible" />
    <TextView
        android:id="@+id/msg"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

MainActivity.java
package com.example.androidpopupwindow;

import android.support.v7.app.ActionBarActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;

public class MainActivity extends ActionBarActivity {

 LinearLayout mainLayout;
 ProgressBar progressBar;
 TextView msg;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  mainLayout = (LinearLayout)findViewById(R.id.mainlayout);

  progressBar = (ProgressBar) findViewById(R.id.progressbar);
  msg = (TextView)findViewById(R.id.msg);
  
  final Button btnOpenPopup = (Button) findViewById(R.id.openpopup);
  btnOpenPopup.setOnClickListener(new Button.OnClickListener() {

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

    Button btnDismiss = (Button) popupView
      .findViewById(R.id.dismiss);
    btnDismiss.setOnClickListener(new Button.OnClickListener() {

     @Override
     public void onClick(View v) {
      // TODO Auto-generated method stub
      popupWindow.dismiss();
     }
    });
    
    TextView me = (TextView) popupView
      .findViewById(R.id.me);
    me.setText(popupWindow.toString());

    //Center the PopupWindow
    popupWindow.showAtLocation(mainLayout, Gravity.CENTER, 0, 0);

    //Auto dismiss PopupWindow in 10 sec
    new DismissAsyncTask(progressBar, msg, popupWindow).execute();
   }
  });
 }

 public class DismissAsyncTask extends AsyncTask<Void, Integer, Void> {

  ProgressBar taskProgressBar;
  TextView taskMsg;
  PopupWindow taskPopupWindow;

  public DismissAsyncTask(ProgressBar targetProgressBar,
    TextView targetMsg,
    PopupWindow targetPopupWindow) {
   taskProgressBar = targetProgressBar;
   taskMsg = targetMsg;
   taskProgressBar.setVisibility(View.VISIBLE);
   taskPopupWindow = targetPopupWindow;
  }

  @Override
  protected Void doInBackground(Void... params) {
   for (int i = 0; i < 10; i++) {
    publishProgress(i);
    SystemClock.sleep(1000);
   }
   return null;
  }

  @Override
  protected void onProgressUpdate(Integer... values) {
   taskProgressBar.setVisibility(View.VISIBLE);
   taskProgressBar.setProgress(values[0]);
   
   taskMsg.setText("Updated by : " 
    + taskPopupWindow.toString() 
    + " - " + values[0]);
  }

  @Override
  protected void onPostExecute(Void result) {
   Toast.makeText(MainActivity.this, 
    "Dismiss PopupWindows: " + taskPopupWindow.toString(), 
    Toast.LENGTH_LONG).show();
   
   taskProgressBar.setVisibility(View.INVISIBLE);
   taskPopupWindow.dismiss();
  }
 }

}



Remark for AsyncTask:
- In this implementation, I haven't stop the AsyncTask if the PopupWindow dismissed by button. So the associated AsyncTask still run in background. I intentionally implement like this, to show the behavior of AsyncTask.
- Starting with HONEYCOMB, AsyncTask are executed on a single thread, that means one by one (read Run multi AsyncTask at the same time). If user start a PopupWindow A and AsyncTask A, and dismiss PopupWindow A with button, AsyncTask A still running. Then start PopupWindow B and AsyncTask B. It will wait AsyncTask A finished, then start AsyncTask B,...then finish, then dismiss PopupWindow B.

download filesDownload the files.

No comments: