Sygnerical
Sygnerical

Reputation: 221

Call method without a triggering condition

I'm trying to create a program which will randomly and continuously display circles all over the screen that will change sizes. I've created a method that will draw circles but I'm not sure how to call it. In the past I've only done buttons which will call a method when clicked, but I don't know how to just call a method out of the blue. Is there a 'main method equivalent' in android which is automatically run?

Upvotes: 1

Views: 69

Answers (3)

Jack Xu
Jack Xu

Reputation: 75

You can create your own class enxtends View, and put the code that controlls the appearance of the circle in the mothod onDraw() which you should override.When you want to get the size of your circle changed, call invalidate().As a result, the method onDraw() is called and the appearance just changes.

Upvotes: 1

zzy
zzy

Reputation: 1793

There is a main method, but you can not touch it simply. In Android , the UI thread would be the main method you want though they are not the same thing.

You can run action in UI thread like this:

public static void runInUiThread(Runnable action) {
    if (Looper.myLooper() == Looper.getMainLooper()) {
        action.run();
    } else {
        new Handler(Looper.getMainLooper()).post(action);
    }
}

But according to your question ,you want to display something. It is not a google idea that draw it directly on Activity, you can create a class and extends View. The Override the onDraw method , you can put your drawing code into onDraw. This function will be called automatically when it need to be display.

Now you can put your custom view into layout and if the custom view need to be display in screen , it would.

Upvotes: 1

mattfred
mattfred

Reputation: 2759

The onCreate method of the activity showing the circles will be automatically run. From here you can start a background timer thread to randomly call the method creating the circles.

Upvotes: 1

Related Questions