I have a simple linear layout. I am adding button dynaically through code one below other with some space in between. I need to draw a vertical line between those two buttons (Vertical arrow headed line in specific).
Could you please let me know how to draw a vertical line from buttom of button1 to top of button2.
I used DrawLine() to draw a line but its getting drawn below button2 with some offset.
Here is my code:
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class SampleMethodActivity extends Activity {
Button b,b1;
public int width,height,bottom;
LinearLayout ll;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sample_method);
ll = (LinearLayout) findViewById(R.id.my_linear_layout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(0, 12, 0, 40);
b = new Button(this);
b.setText("This is a sample text");
b.setLayoutParams(params);
b.setGravity(Gravity.CENTER);
ll.addView(b);
b1 = new Button(this);
b1.setText("This is a sample text to chck the width and height of a button1 and need to check how long it gets stretched and to check the width");
b1.setLayoutParams(params);
b1.setGravity(Gravity.CENTER);
ll.addView(b1);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
width = b.getWidth();
height = b.getHeight();
bottom = b.getBottom();
DrawView dv = new DrawView(this,width/2,bottom);
ll.addView(dv);
}
}
Below is DrawView class:
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();
int x,y;
public DrawView(Context context,int x,int y) {
super(context);
this.x=x;
this.y=y;
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setColor(Color.WHITE);
paint.setStrokeWidth(3);
System.out.println("X: "+x);
canvas.drawLine(x, y, x, y+400, paint);
}
}
Drawing a Line with View
If you only need a straight horizontal or vertical line, then the easiest way may be to just use a View in your xml layout file. You would do something like this:
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@android:color/black" />
Set first button gravity to left
and other to right
and in between them add a view in between them and set it's gravity to center
.
Second Way
Add button (weight 45) view(10) button2(weight45) and you are done. and you can also create an xml and inflate it to LinearLayout.
For vertical line width to 1dp and for horizontal line height to 1dp
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With