show PopupWindow - Android User Interface

Android examples for User Interface:PopupWindow

Description

show PopupWindow

Demo Code


import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import java.util.List;

public class Main{
    //w w w .ja v  a  2  s  .  c om
    public static void showPopup(Context context, View view,
            List<String> list, final PositionClick callback) {
        PopupWindow popup = new PopupWindow();
        ListView lv = new ListView(context);
        popup.setFocusable(true);
        popup.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
        popup.setContentView(lv);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                if (callback != null) {
                    callback.onItemClick(position);
                }
            }
        });
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,
                android.R.layout.simple_list_item_1, list);
        lv.setAdapter(adapter);
        popup.setWidth(view.getWidth());
        popup.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        popup.showAsDropDown(view);
    }
}

Related Tutorials