Showing posts with label Android code sample: PopupMenu. Show all posts
Showing posts with label Android code sample: PopupMenu. Show all posts

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.

Friday, March 30, 2012

Example of using PopupMenu

Android 3.0(API Level 11) provide android.widget.PopupMenu class, to displays a Menu in a modal popup window anchored to a View. The popup will appear below the anchor view if there is room, or above it if there is not. If the IME is visible the popup will not overlap it until it is touched. Touching outside of the popup will dismiss it.

Example of using PopupMenu

Create a file /res/menu/popupmenu.xml to define the PopupMenu.
<menu xmlns:android="http://schemas.android.com/apk/res/android">
  <group android:id="@+id/group_popupmenu">
      <item android:id="@+id/menu1"
          android:title="Popup menu item 1"/>
      <item android:id="@+id/menu2"
          android:title="Popup menu item 2"/>
      <item android:id="@+id/menu3"
          android:title="Popup menu item 3"/>
  </group>
</menu>


Main Java code
package com.AndroidPopupMenu;

import android.app.Activity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidPopupMenuActivity extends Activity {
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
    
      Button button = (Button)findViewById(R.id.button);
      TextView text = (TextView)findViewById(R.id.text);
      ImageView image = (ImageView)findViewById(R.id.image);
    
      button.setOnClickListener(viewClickListener);
      text.setOnClickListener(viewClickListener);
      image.setOnClickListener(viewClickListener);
    
    
  }

  OnClickListener viewClickListener
  = new OnClickListener(){

  @Override
  public void onClick(View v) {
   // TODO Auto-generated method stub
   showPopupMenu(v);
  }
   
  };

  private void showPopupMenu(View v){
   PopupMenu popupMenu = new PopupMenu(AndroidPopupMenuActivity.this, v);
      popupMenu.getMenuInflater().inflate(R.menu.popupmenu, popupMenu.getMenu());
    
      popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
   
   @Override
   public boolean onMenuItemClick(MenuItem item) {
    Toast.makeText(AndroidPopupMenuActivity.this,
      item.toString(),
      Toast.LENGTH_LONG).show();
    return true;
   }
  });
    
      popupMenu.show();
  }
}


Main layout, main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical"
  android:gravity="center_horizontal">

  <TextView
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="@string/hello" />
  <Button
      android:id="@+id/button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="A Button"
      />
  <TextView
      android:id="@+id/text"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="A TextView"
      />
  <ImageView
      android:id="@+id/image"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:src="@drawable/ic_launcher"
      />

</LinearLayout>


Related:
- PopupWindow