Can anybody tell how to draw a line in Android, perhaps with an example?
To improve on the answers provided by @Janusz
I added this to my styles:
<style name="Divider">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">1dp</item>
<item name="android:background">?android:attr/listDivider</item>
</style>
Then in my layouts is less code and simpler to read.
<View style="@style/Divider"/>
if you want to do horizontal line spacing then do the above.
And for vertical line between two Views you have to replace android:layout_width parameters(attributes) with android:layout_height
If you're working with ConstraintLayout
you need to define at least 2 constraints for the line to show up. Like this:
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:background="@android:color/black"
app:layout_constraintEnd_toEndOf="@+id/someView1"
app:layout_constraintStart_toStartOf="@+id/someView2"
app:layout_constraintTop_toBottomOf="@+id/someView3" />
Though I defined 3 constraints.
This one draws 2 lines which form a cross on the top left of the screen:
DrawView.java
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
public class DrawView extends View {
Paint paint = new Paint();
private void init() {
paint.setColor(Color.BLACK);
}
public DrawView(Context context) {
super(context);
init();
}
public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public DrawView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
@Override
public void onDraw(Canvas canvas) {
canvas.drawLine(0, 0, 20, 20, paint);
canvas.drawLine(20, 0, 0, 20, paint);
}
}
The activity to start it:
StartDraw.java
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
public class StartDraw extends Activity {
DrawView drawView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
drawView = new DrawView(this);
drawView.setBackgroundColor(Color.WHITE);
setContentView(drawView);
}
}
package com.example.helloandroid;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
public class HelloAndroid2Activity extends Activity {
/** Called when the activity is first created. */
DrawView drawView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
drawView = new DrawView(this);
drawView.setBackgroundColor(Color.WHITE);
setContentView(drawView);
}
class DrawView extends View {
Paint paint = new Paint();
public DrawView(Context context) {
super(context);
paint.setColor(Color.BLUE);
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawLine(10, 20, 30, 40, paint);
canvas.drawLine(20, 10, 50, 20, paint);
}
}
}
final SurfaceView surf = (SurfaceView)findViewById(R.id.surface_home);
surf.setOnTouchListener( new SurfaceView.OnTouchListener(){
private boolean moving = false;//stupid state
public boolean onTouch(View v, MotionEvent event) {
switch( event.getAction() ){
case MotionEvent.ACTION_DOWN:
final int x = (int)event.getX();
final int y = (int)event.getY();
final Rect bounds = mTiles.getBounds();
moving = bounds.intersects(x, y, x+1, y+1);
return true;
case MotionEvent.ACTION_MOVE:
if( moving ){
final int x_new = (int)event.getX();
final int y_new = (int)event.getY();
mDrawTiles.draw( new DrawLogic(){
public void draw(Rect _surface) {
mTiles.setBounds(
x_new - mDrawWidth/2,
y_new - mDrawHeight/2,
x_new + mDrawWidth/2,
y_new + mDrawHeight/2);
}
});
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#c0c0c0"
android:id="@+id/your_id"
android:layout_marginTop="160dp" />