Created
January 30, 2017 14:38
-
-
Save Zhuinden/e975e45f9133724d86595367345f3331 to your computer and use it in GitHub Desktop.
Custom ViewGroup example
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 class MyCustomViewGroup extends RelativeLayout { | |
public MyCustomViewGroup(Context context) { | |
super(context); | |
init(context); | |
} | |
public MyCustomViewGroup(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(context); | |
} | |
public MyCustomViewGroup(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
init(context); | |
} | |
@TargetApi(21) | |
public MyCustomViewGroup(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { | |
super(context, attrs, defStyleAttr, defStyleRes); | |
init(context); | |
} | |
private void init(Context context) { | |
if(!isInEditMode()) { | |
// ... | |
} | |
} | |
@OnClick(R.id.button) | |
public void doSomething() { | |
// ... | |
} | |
@Override | |
protected void onFinishInflate() { | |
super.onFinishInflate(); | |
ButterKnife.bind(this); | |
} | |
@Override | |
protected void onAttachedToWindow() { | |
super.onAttachedToWindow(); | |
// pretty much `onStart()` | |
} | |
@Override | |
protected void onDetachedFromWindow() { | |
// pretty much `onStop()` | |
super.onDetachedFromWindow(); | |
} | |
} |
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
<?xml version="1.0" encoding="utf-8"?> | |
<fully.qualified.MyCustomViewGroup xmlns:android="http://schemas.android.com/apk/res/android" | |
android:orientation="vertical" android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<Button | |
android:id="@+id/button" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_centerInParent="true" | |
android:text="Go to next"/> <!-- centerInParent works because --> | |
<!-- custom viewgroup is also RelativeLayout --> | |
</fully.qualified.MyCustomViewGroup> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment