Learn How to Draw Line Using Canvas in Android

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.    
  18. </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.    
  10. public class MainActivity extends Activity {  
  11.     DrawLine drawLine;  
  12.     @Override  
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         drawLine = new DrawLine(this);  
  16.         drawLine.setBackgroundColor(Color.CYAN);  
  17.         setContentView(drawLine);  
  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.    
  8. class DrawLine extends View {  
  9.     Paint paint = new Paint();  
  10.     public DrawLine(Context context) {  
  11.         super(context);  
  12.         paint.setColor(Color.BLACK);  
  13.     }  
  14.     @Override  
  15.     public void onDraw(Canvas canvas) {  
  16.         canvas.drawLine(50100600600, paint);  
  17.         canvas.drawLine(505507700, paint);  
  18.     }  
  19.    
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.    
  24. </manifest> 
Step 6
 
Clipboard03.jpg


Similar Articles