Android Paint example tutorial – 2017 New Fresh Code

Android Paint source code very simple for android beginners

Begin with simple android paint source code.

Android paint MainActivity.class source code –

package utube.attraction.com.nextgenearn.colorpaint;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {

    PaintView paintArea;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //requestWindowFeature(Window.FEATURE_NO_TITLE);
        //Use above code make your paint app full screen.

        paintArea = new PaintView(this);
        setContentView(paintArea);
        paintArea.requestFocus();
    }

    public class PaintView extends View implements View.OnTouchListener {

        List<Point> points = new ArrayList<Point>();
        Paint paint = new Paint();

        public PaintView(Context context) {
            super(context);
            setFocusable(true);
            setFocusableInTouchMode(true);

            this.setOnTouchListener(this);
            paint.setColor(Color.RED);
            //You can use spinner to choose color.
            paint.setAntiAlias(true);
            paint.setStrokeWidth(16f);
            //setStrokeWidth to change thickness of paint line.
        }

        @Override
        public void onDraw(Canvas canvas) {
            for (Point point : points) {
                canvas.drawCircle(point.x, point.y, 5, paint);
            }
        }

        public boolean onTouch(View view, MotionEvent event) {
            Point dot = new Point();
            dot.x = event.getX();
            dot.y = event.getY();
            points.add(dot);
            invalidate();
            return true;
        }
    }

    class Point {
        float x, y;

        @Override
        public String toString() {
            return x + ", " + y;
        }
    }

}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout

    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="utube.attraction.com.nextgenearn.colorpaint.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</RelativeLayout>

AndroidManifest.xml -
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="utube.attraction.com.nextgenearn.colorpaint">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Visit other android popular source code

Add a Comment

Your email address will not be published. Required fields are marked *