Skip to content

Instantly share code, notes, and snippets.

@Evin1-
Last active July 26, 2017 21:31
Show Gist options
  • Save Evin1-/33965fd6456d1ddf1f40d44d2c742ab9 to your computer and use it in GitHub Desktop.
Save Evin1-/33965fd6456d1ddf1f40d44d2c742ab9 to your computer and use it in GitHub Desktop.
Minimal CustomView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.myapplication.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<com.example.myapplication.CustomRectangle
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
public class CustomRectangle extends View {
private Rect rectangle;
private Paint paint;
public CustomRectangle(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
rectangle = new Rect(100, 100, 300, 250);
paint = new Paint();
paint.setColor(Color.YELLOW);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.BLUE);
canvas.drawRect(rectangle, paint);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment