Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
23 views4 pages

Drawing App

The document describes a drawing app that allows users to create colorful shapes on a canvas using their fingers. It includes Java code for the main activity and a custom drawing view, enabling color selection through buttons and free drawing functionality. The layout is defined in XML, featuring a drawing area and color buttons for user interaction.

Uploaded by

venerableredgaze
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views4 pages

Drawing App

The document describes a drawing app that allows users to create colorful shapes on a canvas using their fingers. It includes Java code for the main activity and a custom drawing view, enabling color selection through buttons and free drawing functionality. The layout is defined in XML, featuring a drawing area and color buttons for user interaction.

Uploaded by

venerableredgaze
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Scenario: Drawing App

You want to create a drawing app that allows users to draw colorful shapes
on the screen using their finger. The app should provide a variety of colors
for users to choose from and allow them to freely draw on the canvas.

Java Code (MainActivity.java):

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 androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private DrawingView drawingView;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

drawingView = findViewById(R.id.drawingView);

public void changeColor(View view) {

int color = Color.parseColor(view.getTag().toString());

drawingView.setPaintColor(color);

XML Layout (activity_main.xml):


<RelativeLayout 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:padding="16dp"
tools:context=".MainActivity">

<com.example.DrawingView
android:id="@+id/drawingView"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">

<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/color_button_selector"
android:tag="#FF0000"
android:onClick="changeColor" />

<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/color_button_selector"
android:tag="#00FF00"
android:onClick="changeColor" />

<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/color_button_selector"
android:tag="#0000FF"
android:onClick="changeColor" />

</LinearLayout>

</RelativeLayout>

Java Code (DrawingView.java):

import android.content.Context;
import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.graphics.Path;

import android.util.AttributeSet;

import android.view.MotionEvent;

import android.view.View;

public class DrawingView extends View {

private Paint paint;

private Path path;

public DrawingView(Context context, AttributeSet attrs) {

super(context, attrs);

init();

private void init() {

paint = new Paint();

paint.setColor(Color.RED);

paint.setStyle(Paint.Style.STROKE);

paint.setStrokeWidth(5f);

path = new Path();

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

canvas.drawPath(path, paint);
}

@Override

public boolean onTouchEvent(MotionEvent event) {

float x = event.getX();

float y = event.getY();

switch (event.getAction()) {

case MotionEvent.ACTION_DOWN:

path.moveTo(x, y);

break;

case MotionEvent.ACTION_MOVE:

path.lineTo(x, y);

break;

case MotionEvent.ACTION_UP:

break;

invalidate();

return true;

public void setPaintColor(int color) {

paint.setColor(color);

}
}

You might also like