onClick graph, display X and Y axis lines with values of X and Y on Text Label in XY Plot.
Asked
Active
Viewed 2,162 times
1 Answers
0
Firstly, Welcome To Stack Overflow!
Secondly,
I assume you mean that you want to draw the origin of the axes on the point the user pressed? Also, I'm assuming you are using a CustomView with CanvasHolder. Take Holder to be the CanvasHolder of the activity/screen. Also, I'm displaying the text using Canvas.drawText
. For this, you will need a Paint in which you can set all the attributed. Refer the Canvas Documentation if you need any help with that. P.S. You can also use a textView to display your text. A Simple search will get you to that. Here is the code
Step 1+2:
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:id="@+id/Horizontal"
android:background="@android:color/darker_gray"/>
<View
android:layout_height="match_parent"
android:layout_width="1dp"
android:id="@+id/Vertical"
android:background="@android:color/darker_gray"/>
</LinearLayout>
Step 3:
@Override
public boolean onTouchEvent(MotionEvent event) {
int X = event.getX(), Y = event.getY();
View horiz = (View) findViewById(R.id.horizontal);
View vert = (View) findViewById(R.id.vertical);
horiz.setY(Y);
vert.setX(X);
}
And step 4 is very simple, I've explained it in the comment. You should be able to do it without any difficulties
Hope I Helped! :D

Ishan Manchanda
- 459
- 4
- 19
-
Thank you so much... But I am not using CustomView with CanvasHolder... Is there any alternate way to get the solution??? – Srikanth Oct 17 '16 at 14:18
-
1) Add a LinearLayout that covers the screen. 2) Add a view to it (Refer: http://stackoverflow.com/a/10282253/6740515) 3) In the onTouch, use setX and setY to set the positions. 4) Add a TextView and change its text to the 'drawText' text in your java file. If you have any problems, drop a comment, I'll put in the whole code. If you think the answer is satisfactory, mark it as accepted :D – Ishan Manchanda Oct 17 '16 at 14:35