Introduction

This article explains how to draw a line using a canvas in Android.
In this I have drawn two lines that intersect each other. To draw these intersecting lines you need to first extend the view class that provides the onDraw() method where you will write code to draw lines. You will call the drawline() method of the Canvas class to draw the lines. The setColor() method will be provided by the Paint class to set the colors of both the lines.
Step 1
Create the project like this:
Clipboard02.jpg
Step 2
XML file:
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingLeft="@dimen/activity_horizontal_margin"
  6. android:paddingRight="@dimen/activity_horizontal_margin"
  7. android:paddingTop="@dimen/activity_vertical_margin"
  8. android:paddingBottom="@dimen/activity_vertical_margin"
  9. tools:context=".MainActivity">
  10. <TextView
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:text="Draw Lines"
  14. android:layout_centerHorizontal="true"
  15. android:textSize="20dp"
  16. android:textStyle="bold"/>
  17. </RelativeLayout>
Step 3
Create a Java class file and write this:
  1. package com.logacat;
  2. import android.os.Bundle;
  3. import android.app.Activity;
  4. import android.util.Log;
  5. import android.view.Menu;
  6. import android.app.Activity;
  7. import android.graphics.Color;
  8. import android.os.Bundle;
  9. public class MainActivity extends Activity {
  10. DrawLine drawLine;
  11. @Override
  12. public void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. drawLine = new DrawLine(this);
  15. drawLine.setBackgroundColor(Color.CYAN);
  16. setContentView(drawLine);
  17. }
  18. }
Step 4
Create another Java class file and write this:
  1. package com.logacat;
  2. import android.content.Context;
  3. import android.graphics.Canvas;
  4. import android.graphics.Color;
  5. import android.graphics.Paint;
  6. import android.view.View;
  7. class DrawLine extends View {
  8. Paint paint = new Paint();
  9. public DrawLine(Context context) {
  10. super(context);
  11. paint.setColor(Color.BLACK);
  12. }
  13. @Override
  14. public void onDraw(Canvas canvas) {
  15. canvas.drawLine(50, 100, 600, 600, paint);
  16. canvas.drawLine(50, 550, 770, 0, paint);
  17. }
  18. }
Step 5
Android manifest.xml file
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.logacat"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6. <uses-sdk
  7. android:minSdkVersion="7"
  8. android:targetSdkVersion="16" />
  9. <application
  10. android:allowBackup="true"
  11. android:icon="@drawable/ic_launcher"
  12. android:label="@string/app_name"
  13. android:theme="@style/AppTheme" >
  14. <activity
  15. android:name="com.logacat.MainActivity"
  16. android:label="@string/app_name" >
  17. <intent-filter>
  18. <action android:name="android.intent.action.MAIN" />
  19. <category android:name="android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </activity>
  22. </application>
  23. </manifest>
Step 6
Clipboard03.jpg