Popup Window in Android

Introduction:
    Popup window is like a dialog box that gains complete focus when it appears on screen. Like the activity popup also has its own GUI which the android developer can design.
Popup appears in front of activity and gains focus. Popup are usually used to show some additional information or something user wants to know after an event takes place.

              1.PopupMenu has support only android API 11 and above

              2.PopupWindow can support all android versions API


demo




Using this code

activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:gravity="top|center_horizontal"
 android:background="#C89191"
 android:orientation="vertical" >

<Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginTop="30dp"
 android:gravity="center"
 android:text="Android" />
</LinearLayout>

MainActivity.java:
package com.example.popupwindow;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {


String popUpContents[];
PopupWindow popupWindowDogs;
Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

button=(Button)findViewById(R.id.button1);
List<String> dogsList = new ArrayList<String>();
dogsList.add("Samsung");
dogsList.add("Google");
dogsList.add("HTC");
dogsList.add("SONY");

// convert to simple array
popUpContents = new String[dogsList.size()];
dogsList.toArray(popUpContents);

/*
* initialize pop up window
*/
popupWindowDogs = popupWindowDogs();
button.setOnClickListener(new OnClickListener() {
@Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 popupWindowDogs.showAsDropDown(v, -5, 0);
 }
});

}

public PopupWindow popupWindowDogs() 
{

 // initialize a pop up window type
 PopupWindow popupWindow = new PopupWindow(this);

 // the drop down list is a list view
 ListView listView = new ListView(this);
 // set our adapter and pass our pop up window contents
 listView.setAdapter(dogsAdapter(popUpContents));
 // set the item click listener
 listView.setOnItemClickListener(new OnItemClickListener() {

 @Override
 public void onItemClick(AdapterView<?> arg0, View v, int arg2,
 long arg3) {
 // TODO Auto-generated method stub
 Context mContext = v.getContext();
 MainActivity mainActivity = ((MainActivity) mContext);
 // add some animation when a list item was clicked
 Animation fadeInAnimation =  AnimationUtils.loadAnimation(v.getContext(), android.R.anim.fade_in);
 fadeInAnimation.setDuration(10);
 v.startAnimation(fadeInAnimation);
 // dismiss the pop up
 mainActivity.popupWindowDogs.dismiss();
 // get the text and set it as the button text
 String val =(String) arg0.getItemAtPosition(arg2);
 Toast.makeText(mContext, val, Toast.LENGTH_SHORT).show();
 }
});
// some other visual settings
popupWindow.setFocusable(true);
popupWindow.setWidth(250);
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
// set the list view as pop up window content
popupWindow.setContentView(listView);

return popupWindow;
}

/*
* adapter where the list values will be set
*/
private ArrayAdapter<String> dogsAdapter(String dogsArray[])
{
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,  android.R.layout.simple_list_item_1, dogsArray) {

 @Override
 public View getView(int position, View convertView, ViewGroup parent)  {

  // setting the ID and text for every items in the list
  String text = getItem(position);

  // visual settings for the list item
  TextView listItem = new TextView(MainActivity.this);

  listItem.setText(text);
  listItem.setTag(position);
  listItem.setTextSize(22);
  listItem.setPadding(10, 10, 10, 10);
  listItem.setTextColor(Color.WHITE);
  return listItem;
 }
};
return adapter;
}
}

Happy Coding...
Previous
Next Post »