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?
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.
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.
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.
Related
How do run a function after the view and its subviews have finished loading like in iOS (viewDidAppear / viewDidLayoutSubviews)
I basically want to run a UI animation after the program loads but as my programs too heavy(and has many subviews) the animation doesn't run at all(I tried handler.postDelayed and it worked fine but I wanted to know the more efficient way to do this(for performance reason as a phone I tested on is quite old and takes 4s to load while another takes just a second, etc.).
I think you can use the ViewTreeObserver for that as stated here.
You will have something like:
yourView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
// do something here but make sure you unregister if you just want to get notified only once..
}
});
The developers page is stating the following: Register a callback to be invoked when the global layout state or the visibility of views within the view tree changes
You can then add this listener on the root view of your activity for example in onCreate and inside that method you can execute your animation.
The method gets called whenever the layout state or the visibility of the view gets changed. It is not like viewDidAppear but it is something similar to "viewDidLayoutSubviews".
You can also try to run the animation in onResume but that will run the animation whenever the activity resumes not only the first time you create it and I am not sure if you want or not that.
The answer I expected at first was the show method. Unfortunately if show has a lot of work to do, there is a delay between method call and screen appearance. So how does one get notified when screen appears?
Using show() should be fine, the screen will be shown straight after so just put stuff you want done at the end where the delay should be negligible.
If the work that show() does is in a super class, then just override it, call super.show() and then do your stuff after, like so...
#Override
public void show () {
super.show();
doAnyOtherSlowStuffThatMightNeedDoing();
doTimeCriticalStuff();
}
I should probably add that a better solution would be to not do slow stuff in the show() method to start with, but that's a whole other debate.
I am not sure if this is possible or not but what I am after is that, I have a method which I called in my onCreate and the method runs when the app is starts, basically this method does bunch of things like put numbers on Textview, change colour of text etc (this method does around 12 things atm). I have a button, what I want to do is, when the button is pressed I want to stop using the method that was called on start. For example;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpMethod();
removeMethod();
}
public void removeMethod(){
Code for button and listener
.......
.......
.......
.......
{
you can do it with putting it in a thread, you can pause/stop as you wish.
here is simple tutorial on thread in java, you can put your in a thread which you want to do in setUpMethod().
http://www.tutorialspoint.com/java/java_multithreading.htm
and use Thread control methods to control the execution of code.
http://www.tutorialspoint.com/java/java_thread_control.htm
You just need to read through setUpMethod(), work out what the reverse of each action of it is, and put all those undo steps in removeMethod(). Post the code of setUpMethod() if you need more help.
If you do those things on main thread, you can't stop, because everything you do is on one threat. 12 things should finish and after this removeMethod() will be called.
Simply revert all those 12 changes to initial state in removeMethod method -set text, textColor and other things to initial values
To "stop the method being called again when the button is pressed", create a boolean somewhere, and add the line
if (wasRunAlready) return;
to the start and
wasRunAlready = true;
to the end of setUpMethod(). It will then be impossible for the code inside to run twice.
I'm developing an Android 3.1 application.
I want to execute an AsyncTask after activity is shown. I want to show something to user before execute AsyncTask.
I've read that it is not recommend to execute AsyncTask on onCreate().
Where I have to execute AsyncTask on onStart() or onResume()?
I want to left enough time to show activity before execute it.
onCreate(), onStart() and onResume() are lifecycle methods called by the operating system and shouldn't be called directly. You can however override them to have your code executed at these stages of the activities lifecycle:
However, if you want your AsyncTask to start after all of your Views have been inflated and drawn to the screen then you need to put the code in this:
toReturn.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout() {
toReturn.getViewTreeObserver().removeGlobalOnLayoutListener(this);
// asyncTask.execute();
}
});
In the above example toReturn is a view in your onCreate() method. It can be any view you like.
This pulls a ViewTreeObserver from the View and add's a listener to it which will be called when the view has finished being drawn to the screen. It's important you keep the "removeGlobalOnLayoutListener()` line in as this will stop the code firing every time the View is drawn.
Answer is in onResume()
I hade same requirement in my activity where i need to show some list with other buttons and images..
List were getting data from server so used AsyncTask for that..
But before that required to show empty listview and other part of the screen..
so first when it goes to onCreate() I set empty arraylist to listview's adapter then in onResume() call the Asynctask and in that task fill the ArrayList and call adapter.notifyDataSetChanged()
Then another problem occure..when i go to next activity and come back it always call the asynctask even if i dont require..
So had put some condition like if(arrayList.size()==0) then call asynctask else dont.
You can put yur code in the onWindowsFocusChanged method. You can use a thread inside it to manage the timer to start your specific asynctask.
Be aware that this would be performed each time your activity have the focus, not only the first time you launch your activity (I don't know if this could be a problem for you).
implement a View object and override the onDraw().
that way you'll know exactly when the first screen is visible to the user
Wondering if there is any way to build and fire an event (e.g. on click event) in android applications programmatically.
Thanks.
sorry, it seems question is not clear enough, let me explain a bit more:
I have an Activity (lets call it A) with multiple views (5 ImageView for example).
And a normal Java class (lets call it B) which is used by my Activity.
There is an instance of B in my Activity.
If user click on a View (Image View) the view OnClickListener calls a method in B
In B, if operation is successful, it will call back a method in activity again.
in activity method, It will change image or state for clicked ImageView.
in the other hand:
click on view (x) in Activity -------> B.doSomething() --------> A.bIsDone() -----> change image for view (x)
with normal execution, its working and there is no problem.
the problem is that I want to simulate this process to be automatic, my code looks like this:
while (!b.isFinished()) {
try {
b.doSomething(<for View x>);
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
The issue is that, it won't render anything, until the end of the loop.
I tried to put the loop in another Thread, but its throwing exception:
android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
what can i do for this issue? i was think generate an click event on view (by code), to see it can help or not.
what is your solution?
Thanks
In Your Activity B, create a Handler
Handler mHandler = new Handler();
In your doSomething() inside B update your ImageView using mHandler.post(Runnable r).
The Runnable should include code how to update UI.
The problem is likely caused because you're trying to update B Components using A Thread. I suppose you could also use runOnUiThread(Runnable r) aswell.
Original Answer before Question was edited.
dispatchKeyEvent(KeyEvent event) for Keys
dispatchTouchEvent(MotionEvent) for Touch
Refer to other dispatchXXX Functions. Google is your friend. :)
Here's a summary of dispatchKeyEvent from the docs.
public boolean dispatchKeyEvent(KeyEvent event)
Dispatch a key event to the next view on the focus path.
This path runs from the top of the view tree down to the currently focused view.
If this view has focus, it will dispatch to itself.
Otherwise it will dispatch the next node down the focus path.
This method also fires any key listeners.
If you are talking about Listeners, like OnClickListener, then yes: You can create your own listeners and trigger them.
Basically you start by defining an interface for that Listener. For a DaterTimePicker (which has a DaterPicker and TimerPicker) widget I once used
public interface OnDateTimeSetListener {
public abstract void onDateTimeSet(DatePicker datePicker, TimePicker timePicker);
}
The interface defines a single Method which has to be implemented by your listeners.
Then in your class you do something like
public class DateTimePickerDialog extends AlertDialog {
private OnDateTimeSetListener onDateTimeSetListener;
private DatePicker datePicker;
private TimePicker timePicker;
public void setOnDateTimeListener(OnDateTimeSetListener l) {
this.onDateTimeSetListener = l;
}
private onDateTimeSet() {
if(onDateTimeSetListener!=null)
onDateTimeSetListener.onDateTimeSet(this.datePicker, this.timePicker);
}
private doSomething() {
// Do your code here
// fire up the event once finished
onDateTimeSet();
}
}
setOnDateTimeListener() is used to set listeners from outside of the class (i.e. in your main activity).
onDateTimeSet is used internally (hence declared private) to fire the event and to check if onDateTimeSetListener was set or else we'd get a NullPointerException if it wasn't set. If it was set, call it's onDateTimeSet method.
And in your main Activity you simply add an listener to it and add the code you need, like:
DateTimePicker dateTimePicker = (DateTimePicker)findViewById(R.id.datetime);
dateTimePicker.setOnDateTimeListener(new OnDateTimeSetListener () {
void onDateTimeSet(DatePicker datePicker, TimePicker timePicker) {
// Date/Time was changed
Log.d("OnDateTimeSet", "Date/time was updated");
}
});
Here you set up the listener the same way you'd set up an OnClickListener.
It's quite a bit of code for a you have to do for a simple event, but as far as I know it's the right way to implement this.
I'm not sure what you mean by build and fire an event and on which context (i.e. from a View, Activity or Service) but will give it a try.
To create a key event just use the KeyEvent Contructors.
For MotionEvents you can use the static obtain method from [here][1]
If you want to fire them use the previously mentioned methods on the target Activity (which can be fetch as the context (getContext()) if inside of a View)
Hope it helps, if it doesn't please provide with more details and/or example code.
[1]: http://developer.android.com/reference/android/view/MotionEvent.html#obtain(long, long, int, float, float, int)