Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I draw a vertical line in an Activity when a button is pressed?

Tags:

android

draw

line

I want to draw a straight vertical line in my Android activity when a button is pressed. Please explain how I can draw the line with a position & length I want.

Elaboration: I have a linear layout with vertical orientation. A set of buttons constitute this linear layout. When I press one button, I want a line to appear to the right of these buttons, as if dividing the screen halfway - a straight line of width=2dip and height=200dip.

like image 750
kiki Avatar asked Sep 28 '10 04:09

kiki


2 Answers

To draw dynamically u can use below code snippet:

 View view = new View(this);
 view.setLayoutParams(new LayoutParams(2,LayoutParams.FILL_PARENT));
 view.setBackgroundColor(Color.BLACK);
 layout.add(view);
like image 38
Lakshmanan Avatar answered Sep 18 '22 21:09

Lakshmanan


I have solved it myself. All you need to do is define a View with appropriate parameters and fill the background with color. You may want to use nested linear layouts for positioning the line correctly.

<View 
      android:id="@+id/View01"
      android:layout_width="2dip"
      android:layout_height="500dip"
      android:background="#2B497B"
/>

So if it may be useful to anyone else, I have posted the answer here myself!

like image 146
kiki Avatar answered Sep 16 '22 21:09

kiki