Last active
September 23, 2015 12:44
-
-
Save cnevinc/86474420cb9017e94b33 to your computer and use it in GitHub Desktop.
code snippet for custom view
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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