I have a source code of an app for android which get location from network or gps. There is the next code:
// Callback method for the "both providers" button.
public void useCoarseFineProviders(View v) {
mUseFine = false;
mUseBoth = true;
setup();
}
There is a button, and on the onClick event call to "useCoarseFineProviders", my question is that I want to delete this button and call this method from the onCreate method, but I don't know how to do this.
I need to learn so much things. Thanks for your help.
Your code isn't actually doing anything, or using the view parameter passed in. so if you want to move it to onCreate, just do it- take the body of the function, paste it into the bottom of onCreate, then delete this function and the code (probably in your xml) telling it to call this function in onClick.
Related
I am creating an activity that as a dialog format: it does not cover the whole screen, but only part of it. What I did was in the onCreate() method of this activity, after calling setContentView(), I call:
window.setLayout(windowWidthInDp, LayoutParams.WRAP_CONTENT);
And it does not work. I need to do the following instead to make it work:
window.getDecorView().post(new Runnable() {
#Override
public void run() {
window.setLayout(windowWidthInDp, LayoutParams.WRAP_CONTENT);
}
});
Again, this is AFTER setContentView() is called.
Why do I have to pose it into the message queue instead of calling it directly?
Thanks!
Setting the content view just gives the layout to the Android framework. The layout hasn't yet been fully configured. This doesn't happen until the Android framework gets control back (ie: in the next event loop). This won't happen until the onCreate() method ends.
By posting your code to a Handler, you delay the execution of that code until after the Android framework has fully configured the layout.
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.
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've created a button, if I click it the following will start:
public void onClick(View v) {
// TODO Auto-generated method stub
Crunch();
tts.speak("Kurze Pause", TextToSpeech.QUEUE_ADD, null);
Unterarm();
SitUp();
}
By calling Crunch(), I want to setContentView(R.layout.1), when it is finished I want to change the contentView to another layout, by calling Unterarm(). Then to another one by calling SitUp().
How can I handle it in the best way? I've created all of the layouts and methods, but it isn't working.
It's not working because it will execute all methods without any delay between them so you will get the feeling that only the last one is working and if the last one is restoring the initial state, you'll feel like nothing is happening.
I suggest you wrap the method inside an asyncTask and set some delay between the method calls. It should be straight forward enough.
You have to use animations, this is from the offical documentation
http://developer.android.com/training/animation/crossfade.html
hope it helps :)
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)