Drawing vertical lines in android
android
Solution
So far from your desired use case, I don't see any reason for you to use a custom view for this. You can set a custom background with repeat enabled:
res/drawable/MyBackground.xml
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/lines_image"
android:tileMode="repeat" />
Then for your view, set the background:
res/layout/whatever.xml
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/MyBackground" />
OR
myView.setBackgroundResource(R.drawable.MyBackground);
lines_image.png should be a 20px (or however much space between you want) wide image with your red line on the right.
It's an important concept for UI development. Don't do complicated things in code when a simple image solution will suffice.
If you absolutely **MUST** Do this in code, Then you just do the drawing in a loop for the width of the canvas.
private static final int LINE_SPACING = 20;
@Override
public void onDraw(Canvas canvas) {
for (int x = 0; x < canvas.getWidth(); x += LINE_SPACING) {
canvas.drawLine(x, 0, x, canvas.getHeight(), paint);
}
}
Problem
i try to draw vertical lines in android. ``` DrawView drawView; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); drawView = new DrawView(this); setContentView(drawView); } public class DrawView extends View { Paint paint = new Paint(); public DrawView(Context context) { super(context); paint.setColor(Color.RED); } @Override public void onDraw(Canvas canvas) { canvas.drawLine(0, 100, 0, 0, paint); } } ``` What this draw just one line. What i want to do draw this lines in whole screen. How can i do that? It must draw everywhere in screen vertically. It now draws vertically but just one.