1

I have read the following links:

How to draw a line in android

http://marakana.com/s/android_2d_graphics_example,1036/index.html

http://developer.android.com/reference/android/graphics/Canvas.html

Activity.setContentView, View.setContentView?

How to create an activity without 'setContentView(R.layout.main)'

http://www.jayway.com/2009/03/26/layout-resources-in-android/

I have read the previous articles that talk about about my problem but I can't solve my problem.

My problem is that I want to draw straight lines to simulate the connection between images, but basically is to paint straight lines.

All the examples I've seen have the main class that inherits from the activity and have another inner class that inherits from the View. And in the main class set the user interface that contains the View class, so we have a empty user interface. Something like this:

public class ActivityMain extends Activity{

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    MyView view1 =new MyView(this);
    setContentView(view1);       
}

public class MyView extends View {
    public MyView(Context c) {
        super(c);
    }
}
//And more code
}

I want to set my user interface (xml file) in the class that inherits from the activity (as usual):

public class ActivityMain extends Activity{
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.union);
}
}

And I want from the class that inherits from activity to draw straight lines, I don't want to have any class that inherits from View.

I tried the following but I don't see any line:

public boolean onTouchEvent(MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();

switch(event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        Canvas canvas = new Canvas();
        Paint paint = new Paint();
                    paint.setColor(Color.BLACK);
        canvas.drawLine(0, 0, 300, 700, paint);     
    break;
    case MotionEvent.ACTION_MOVE:
    break;
    case MotionEvent.ACTION_UP:
    break;
}   
return true;
}

In the previous method I want to draw a line from the corner 0,0 to the corner 300,700. It's a simple test. But I don't draw anything and I don't know why.

Community
  • 1
  • 1
Adrian
  • 316
  • 6
  • 22
  • Can you say why you don't want to subclass a view? Views are what hold canvasses that go on your screen. When you create a canvas like that in your activity it's not attached to anything, you're just drawing on a surface that is never part of the screen. If you had a view you could draw on that view, but I'm not sure why you're opposed to it. – Tim Oct 20 '12 at 16:14
  • aaaaaammm ok. THANKS! But if I use a class that extends a View class how can I set the content of the user interface in this class?because I can't use setContentView(R.layout.xml_file), if it's possible I'll use a View class. – Adrian Oct 20 '12 at 16:24
  • I'm not quite sure what 'set the user interface' means, but you could setup your UI inside your own activity, and then the activity could call a method on your custom view like (drawLine(start, end)), which your view would handle and draw the line. – Tim Oct 20 '12 at 16:28
  • I refer to the elements that I show on screen: images, buttons, etc ...the .xml file. For example: – Adrian Oct 20 '12 at 16:34
  • you can tell me the code to do this???thanks – Adrian Oct 20 '12 at 17:51

1 Answers1

0

You can extend a view and override the onDraw and manage all your lines there.

Then, you can add this view (that can be full screen) in another xml (that can be used in an Activity) like this:

<my.package.myView
    android:layout_width=@"fill_parent"
    android:layout_height=@"fill_parent"/>

Be sure that the layout you use in your main activity is a RelativeLayout so you can fill the entire screen AND be on the top of your views. To place your view on top of your views be sure that you have your view in the end of your activity xml.

Tiago Almeida
  • 13,437
  • 3
  • 62
  • 80