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

Making Round Corner Background in Android using xml
Create one new XML like bg_corner.xml & Put this xml as Background for any View
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
  <corners
      android:topLeftRadius="0dp"
      android:topRightRadius="30dp"
      android:bottomRightRadius="30dp"
      android:bottomLeftRadius="30dp" />
  <stroke
      android:width="3dp"
      android:color="#000000" />
  <solid
      android:color="#800000c0"/>
</shape>
(This will make Background Image like this)


(OR)
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
  <corners
      android:radius="30dp" />
  <stroke
      android:width="3dp"
      android:color="#000000" />
  <solid
      android:color="#800000c0"/>
</shape>
(This will make Background Image like this)