Note that if you want you can use your custom view directly in your XML file, without having to manually add it to the layout at runtime. To do that: - declare your view as a static class or in it's separate java file, let's say mypackage.DrawPoints.Mems - write something like this in your XML: <LinearLayout ...attributes...> < mypackage.DrawPoints.Mems android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>
Note that in the XML layout editor you can use Control-Space to trigger auto-completion on the attributes and values. It doesn't know your custom class name though. By the way, how do you get: package <>.DrawPoints; <> is not a valid package name in java. Is that some kind of unicode chars? R/ On Tue, Apr 14, 2009 at 6:53 PM, gandor <gand...@gmail.com> wrote: > > Thanks. Yes now it works. Had to use event.getAction and draw > method. > > package <>.DrawPoints; > > 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.util.Log; > import android.view.MotionEvent; > import android.view.View; > import android.view.View.OnTouchListener; > import android.widget.LinearLayout; > import android.widget.TextView; > > public class DrawPoints extends Activity implements OnTouchListener{ > static int i = 0; > float x = 200; > float y = 200; > float x1 = 0; > float y1 = 0; > > /** Called when the activity is first created. */ > �...@override > public void onCreate(Bundle savedInstanceState) { > super.onCreate(savedInstanceState); > setContentView(R.layout.main); > //View topLayout = this.findViewById(R.id.layout_id); > LinearLayout layMain = (LinearLayout)this.findViewById > (R.id.layout_id); > // register for events for the view, previously > //topLayout.setOnTouchListener((OnTouchListener) this); > layMain.setOnTouchListener((OnTouchListener) this); > layMain.addView(new Mens(this)); > > } > > // the following callback is called when touch event on screen > happens > �...@override > public boolean onTouch(View v, MotionEvent event) { > String tag = null; > > switch (event.getAction()) > { > case MotionEvent.ACTION_DOWN: > { > x = event.getX(); > y = event.getY(); > } > } > return false; > } > > public class Mens extends View { > > public Mens(Context context) > { > super(context); > > } > > protected void onDraw(Canvas canvas) > { > Paint p = new Paint(); > p.setColor(Color.RED); > > canvas.drawLine(15, 15, x , y, p); > invalidate(); > } > > } > > } > > On Apr 13, 10:33 pm, Raphael <r...@android.com> wrote: >> A couple suggestions: >> >> - don't make your x/y fields static. >> - in onTouch, test that MotionEvent.getAction() is ACTION_DOWN. >> - to draw you should create a custom View class and override the >> onDraw method. See ApiDemos or the LunarLander samples for a start. >> >> R/ >> >> On Mon, Apr 13, 2009 at 5:14 PM, gandor <gand...@gmail.com> wrote: >> >> > Hi, >> >> > Want to draw a line when I touch screen at two points. >> > I can see OnTouch been invoked but after than everything breaks looks, >> > the application crashes >> >> > I am using drawLine in Canvas. >> > Please let me know what I am doing wrong >> >> > package <>.DrawPoints; >> >> > 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.util.Log; >> > import android.view.MotionEvent; >> > import android.view.View; >> > import android.view.View.OnTouchListener; >> > import android.widget.TextView; >> >> > public class DrawPoints extends Activity implements OnTouchListener{ >> > static int i = 0; >> > static float static_x = 0; >> > static float static_y = 0; >> > static float static_x1 = 0; >> > static float static_y1 = 0; >> >> > /** Called when the activity is first created. */ >> > �...@override >> > public void onCreate(Bundle savedInstanceState) { >> > super.onCreate(savedInstanceState); >> > setContentView(R.layout.main); >> > View topLayout = this.findViewById(R.id.layout_id); >> > // register for events for the view, previously >> > topLayout.setOnTouchListener((OnTouchListener) this); >> >> > } >> > // the following callback is called when touch event on screen >> > happens >> > �...@override >> > public boolean onTouch(View v, MotionEvent event) { >> > String tag = null; >> >> > if(i == 0) { >> > static_x = event.getX(); >> > static_y = event.getY(); >> > i = 1; >> > } else >> > { >> > static_x1 = event.getX(); >> > static_y1 = event.getY(); >> >> > } >> >> > if(i == 1) { >> > Paint p = new Paint(); >> > p.setColor(Color.WHITE); >> > p.setStyle(Paint.Style.STROKE); >> > Canvas canvas = new Canvas(); >> > canvas.drawColor(Color.BLUE); >> >> > canvas.drawLine(static_x, static_y, static_x1, static_y1, >> > p); >> > i = 0; >> > } >> >> > return false; >> > } >> >> > } >> >> > <?xml version="1.0" encoding="utf-8"?> >> > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/ >> > android" >> > android:id="@+id/layout_id" >> > android:orientation="vertical" >> > android:layout_width="fill_parent" >> > android:layout_height="fill_parent" >> > > >> > <TextView >> > android:layout_width="fill_parent" >> > android:layout_height="wrap_content" >> > android:text="@string/hello" >> > /> >> > </LinearLayout> > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Beginners" group. To post to this group, send email to android-beginners@googlegroups.com To unsubscribe from this group, send email to android-beginners-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en -~----------~----~----~----~------~----~------~--~---