Skip to content

Instantly share code, notes, and snippets.

@cnevinc
Last active September 23, 2015 12:44
Show Gist options
  • Save cnevinc/86474420cb9017e94b33 to your computer and use it in GitHub Desktop.
Save cnevinc/86474420cb9017e94b33 to your computer and use it in GitHub Desktop.
code snippet for custom view
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
setupBackPressed();
}
public static void addMe(Activity host) {
NotificationView v = (NotificationView) host.getLayoutInflater().inflate(R.layout.notification_list, null);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
host.addContentView(v, p);
}
public void setupBackPressed() {
this.setFocusable(true);
this.setFocusableInTouchMode(true);
this.requestFocus();
this.setOnKeyListener(new OnKeyListener() {
@Override public boolean onKey(View v, int keyCode, KeyEvent event) {
if( keyCode == KeyEvent.KEYCODE_BACK) {
removeFromParent();
}
return true;
}
});
}
public void removeFromParent() {
((ViewGroup) getParent()).removeView(NotificationView.this);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment