Tuesday, 25 November 2014

Custom Popup Menu

Custom Popup Menu

Simple example for custom popup menu:




public class MainActivity extends Activity 
{
Button btnOpenPopup;
PopupWindow popupWindow;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnOpenPopup = (Button) findViewById(R.id.openpopup);
btnOpenPopup.setOnClickListener(new Button.OnClickListener() 
{
@SuppressLint("InflateParams") @Override
public void onClick(View arg0) 
{
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup_window_custom, null);
popupWindow = new PopupWindow(popupView,LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
popupWindow.setBackgroundDrawable(new ColorDrawable());/*(this is important)Must Use*/
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
// popupWindow.showAtLocation(btnOpenPopup, Gravity.NO_GRAVITY, 0, 0);/*No use with this*/
// popupWindow.showAsDropDown(btnOpenPopup);
popupWindow.showAsDropDown(btnOpenPopup,btnOpenPopup.getWidth() / 2, 0);
}
});
}
}

Click this Link to Download Source Code Click here

5 comments: