StartingGroovy
StartingGroovy

Reputation: 2860

Android View Or Activity?

I have a Java class that I'm debating what I should make it into for my Android app. I'm unsure whether I should make it an Activity, a View, or both.

The reason I am unsure has to do with the it having constructors that have parameters, a component listener, and draws on the canvas.

To be clear, when attempting to make it a View, I had issues with the constructor. If an activity, I'm unsure how to interact between the two (ie component resize, color changed, etc) - perhaps with Intents? So logically, maybe the best way is a mixture of a View and Activity. I'm rather new to this thinking, so please keep that in mind.

I'm wondering how those who have created a few Android Apps go about determining what your previous Java classes should be. My class seems that it could go either way.

Below is part of my class (in Java, but I also have it converted to Android):

public class GraphDisplay extends JPanel implement Serializable {

    public static final int BLUE_RED            = 0;
    public static final int BLACK_WHITE         = 1;

    protected int displayBoard;
    protected int centerXcoord;
    protected int centerYcoord;

    private GeneralPath circlePath;
    private GeneralPath rectPath;

    private Color triangleColor;
    private Color rectColor; 

    /*
     * Create an instance of GraphDisplay using the default
     *    blue/red display
     */
    public GraphDisplay() {
        this(BLUE_RED, 0);

    }

    /*
     * Create an instance of GraphDisplay using the board value
     *    and index of 0. Throws exception if model isn't a 
     *    prederfined value
     */
    public GraphDisplay(int board) {
        this(model, 0);

    }



    public GraphDisplay(int board, int index) throws IllegalArgumentException {
        if(board == BLUE_RED || board == BLACK_WHITE) {
            displayBoard = board;
            this.index = index;

            if(displayBoard == BLUE_RED) {
                setBackground(Color.blue);
                triangleColor = Color.red;
                rectColor = Color.red;
            } else {
                setBackground(Color.black);
                triangleColor = Color.white;
                rectColor = Color.white;
            }

            compListener = new GraphListener();
            addGraphListener(compListener);

            mListener = new GraphMouseListener();
            addMouseMotionListener(mListener);

            trianglePath = new GeneralPath();
            rectPath   = new GeneralPath();

            setMinimumSize(new Dimension(400, 400));
            setPreferredSize(new Dimension(400,400));   

        } else {
             throw new IllegalArgumentException("Improper model")
        }
    }


    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;

        centerXcoord = getWidth() / 2;
        centerYcoord = getHeight() / 2;

        drawTriangle(g2);
        drawCircle(g2);

    }

    private void drawTriangle(Graphics2D g2) {
        //code for drawing triangle with lines
    }

    private void drawRectangle(Graphics2D g2) {
        //code for drawing circle 
    }

    public void setTriangleColor(Color color) {
        triangleColor = color;
        repaint();
    }

    public Color getTriangleColor() {
        return triangleColor;
    }

    //Omitted other getters/setter for brevity. 
    //The above get/set methods are a variety of what the class entails

    protected class GraphMouseMotionListener extends MouseMotionAdapter {
        public void mouseMoved(MouseEvent me) {
            int x = mouseEvent.getX();
            int y = mouseEvent.getY();

            //if in component display message
        }
    }

    protected class GraphComponentListener extends ComponentsAdapter {
        public void componentResized(ComponentEvent ce) {
            super.componentResized(ce);
        }
    }


}

So my question is, would you make it an Activity that draws on a View, a View, or something else? If it's not too much trouble, can you elaborate on why you'd do it the way you mention? (if splitting it between an Activity and View, can you provide a brief example, it doesn't have to involve my class - just looking to understand the concept)

Upvotes: 1

Views: 2741

Answers (1)

Wolfcow
Wolfcow

Reputation: 2735

You would use an Activity that can use a view you create in your xml file by extending Activity for your class and then in the onCreate method you say setContentView(R.layout.your_view);

for example:

Your Activity Class:

public class YourClass extends Activity {

    private Button exampleButton;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        exampleButton = (Button)findViewById (R.id.myButton);

        exampleButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {   
                Toast.makeText(YourClass.this, "I did something", Toast.LENGTH_SHORT).show();   
            }
        });
    }
}

Your XML View:

  <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        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="Hello World" />

        <Button android:id="@+id/myButton"
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content"/>

    </LinearLayout>

So the XML is where you define your view, what components will be in it, how it is laid out and so on.

Then the Activity Class is where you bring your view to life and add the functionality and so on. You can also programatically add components to your view but I won't go into that right now.

You could then look into Overlays (some examples would be map overlays to see how they draw on a canvas in the Activity) to get an idea and then apply it to your situation. For example: http://code.google.com/android/add-ons/google-apis/reference/com/google/android/maps/ItemizedOverlay.html

hope that helps.

Upvotes: 3

Related Questions