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