rusky
rusky

Reputation: 65

Draw a Line with a Java class Android

I´m new to Android(Java). I want to draw a Line with this:

public void drawAline(int x1,int y1, int x2, int y2){
        Canvas canvas=new Canvas();
        paint.setColor(Color.BLACK);
        canvas.drawLine(x1,y1,x2,y2,paint);
    }

paint.setColor is working but its not drawing the line:

Call:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        drawView = new DrawView(this);
        drawView.setBackgroundColor(Color.RED);
        drawView.drawAline(0,10,1000,10);

        setContentView(drawView);

What is the problem?

Thanks!

Kind regards

Daniel

Upvotes: 0

Views: 794

Answers (2)

Sumanth Jois
Sumanth Jois

Reputation: 3234

You cannot do it like you are trying to do. basically in android every view i.e buttons or images or EditText are defined in xml files which will be wired to your java file(You can also define view in your java file). In your case, if you want to draw a line , for example considering the line as a image. You can do it like the below.

1) First Create a class which extends ImageView and override onDraw() method.

2)then define the view in your xml file

Java class

 package com.stack.line;
    public class CustomView extends View {
        Paint paint = new Paint();
        public CustomImageView(Context context) {
            super(context);
               paint.setAntiAlias(true);
            paint.setColor(Color.RED);
        }

        @Override
        public void onDraw(Canvas canvas) {
            canvas.drawLine(10, 100, 150, 300, paint);

        }

    }

custom_view.xml File

<LinearLayout

    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"



    >

<com.stack.line.CustomView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/custom"

    />

</LinearLayout>

MainActivity.java

public class MainActivity extends Activity{
  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom_view);

}
}

Hope this was helpful. ThankYou

Upvotes: 2

vishal
vishal

Reputation: 542

draw a line then use this code to draw line simply

public class MainActivity extends Activity {

DrawLine drawLine;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    drawLine = new DrawLine(this);

    drawLine.setBackgroundColor(Color.CYAN);

    setContentView(drawLine);
  }

    class DrawLine extends View {
        Paint paint = new Paint();
        public DrawLine(Context context) {
            super(context);
            paint.setColor(Color.BLACK);
        }

        @Override
        public void onDraw(Canvas canvas) {
            canvas.drawLine(50, 100, 600, 600, paint);
            canvas.drawLine(50, 550, 770, 0, paint);
        }

    }
}

Upvotes: 2

Related Questions