I have a system which works like this:
- I start a spinner animation in my view when a network request is fired.
- When the request finishes (within a AsyncTask) the Animation is stopped.
This works fine except when the display turns off because of lack of user action. This code won't work in this situation:
imgVwSpinner.post(new Runnable() {
#Override
public void run() {
imgVwSpinner.clearAnimation();
imgVwSpinner.setImageResource(R.drawable.refresh);
}
});
Is there a workaround to ensure the animation is cleared?
you can flag the state in your handler , and check that state back in onResume
Related
I have a service running in background and when some conditions are met I need to show a window at the top of the activity. I should be able to navigate between activities and that window should always stay static on top till the user dismiss it.
Options:
I tried with an activity using Dialog Theme. The problem with this approach is that it will be on top of the activity that was at that instant. If I open another activity the "activity dialog" will be behind this new activity.
I thought to implement a BaseActivity that has a frame layout in order to show this "window" every time you open a new Activity. The problem is that is being show again and it shouldn't, the window should be static meanwhile the activities are changing. Also not sure how to call transaction manager inside a service to add the fragment.
I read something about having a Service that will attach a View on the Window Manager. The problem with this is that I am not sure if it's a good practice.
Any thoughts? Thanks!
I have one solution for that
1. create a static variable in app
public static Boolean isMatch=false ;
2. either create dialog or layout that you have to show in activity and manage its show and hide.
2. Create Thread in activity and add 1000 timer
Thread thread= new Thread() {
int wait = 0;
#RequiresApi(api = Build.VERSION_CODES.M)
#Override
public void run() {
super.run();
while (wait < welcomeScreenDisplay) {
sleep(100);
if(isMatch)
{
dialog.show();
}
wait += 100;
}
}//run
};//welcome thread
t.start();
3. in service when the condition is true update the value of the variable
isMatch =true ;
you can send a declare an explicit broadcast receiver for your app and once the service is done then automatically will send a message to the parent activity. In this activity you will have a view at the top with visibility gone, and once you receive the broadcast change the visibility
I'm using AnimationDrawable to show missing network connection.
Show/hide logic is linked to network status change receiver. It works fine.
But when start activity knowing status and try to start animation - animated drawable shows and freezes on first frame. I've read in documentation - 'do not start animation in OnCreate'.
So I wrote code in onResume, but animation still not playing - only shows first frame.
Starting from button or event works fine.
Tried to start with separate thread and wait some time - but this doent sounds good.
Any idea?
This code works when called from net status change handler
private void _NetStatus(boolean start)
{
if (start)
{
m_NetStatus.setVisibility(View.VISIBLE);
m_NetStatusFrameAnimation.start();
}
else
{
m_NetStatusFrameAnimation.stop();
m_NetStatus.setVisibility(View.INVISIBLE);
}
}
Hmm. After trying some samples advising me to use new Runnable at end of onCreate - I tried to start animation when activity shows on screen:
#Override
public void onWindowFocusChanged(boolean hasFocus)
Works fine for now.
I'm having trouble putting this problem into searchable terms. I'm working on an Android application, and specifically the splash screen for my app. The app needs to fetch data from an external web service (a blocking function call), while it does this the user gets a nice title, image and progress bar. When the data arrives the user is redirected to the main menu. Its a simple screen, everything being defined in the xml layout file, my problem is that I just get a black screen for a few seconds and then the main menu. If I press back I get the splash screen with the progress bar spinning away happily.
Here is what I have so far:
public class SplashActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
}
#Override
public void onStart(){
super.onStart();
DatabaseManager db = new DatabaseManager(this.getBaseContext());
db.fetchExternCatalog(); //doesnt return until data arrives
Intent intent = new Intent().setClass(this, MainMenuActivity.class);
startActivity(intent);
}
}
It seems the screen isnt actually drawn until the activity is running (after onCreate(), onStart(), etc). I thought onStart() would be the perfect place to put this, but apparently not.
So how do I draw everything on the screen and make my blocking function call after so the user actually sees the splash screen while the data is downloaded?
You're going to be locking up the UI thread which is why i believe you are seeing a black screen. Use an AsyncTask or create your own thread pool for DB operations.
As far as hitting the back button and seeing your old activity, You need to tell android not to store the activity in it's stack. This should help:
http://developer.android.com/guide/topics/fundamentals/tasks-and-back-stack.html
you need to use the ProgressDialog class to build the dialog, and then run the blocking method inside a thread.
I'll post an example in a minute (gotta get near a PC :p)
private void showSplash(){
progressDialog = ProgressDialog.show(this, "Hello! ima title", "Im the message you see.");
progressDialog.show();
Thread t = new Thread(new Runnable(){
public void run(){
// Put your blocking method here.
// You may need to build in a "hey, im done downloading" variable to get it to close down right
progressDialog.dismiss();
}
});
t.start();
}
I have an app that gets the longitude and latitude on a push of a button. I want to know how to get it so I don't have to push the button every time that I want to refresh to see the location. I know that I can take out the button function and it will automatically update but I need it to only show when the button is selected to on. Is there a way that I can refresh the button without having to keep clicking it?
Rather than having the button call a refresh function, have it set a boolean value "isButtonPressed" to true. And then when the button is pressed again (turned off), set it back to false. Then somewhere in your application that gets called continuously (do you have a game loop? update function? something like that?) do this:
void Update() {
if (isButtonPressed) {
Refresh();
}
}
Make sure you initialize the boolean value to false, set it to true when the button is pressed, and then set it to false when the button is pressed again. You can do that like this:
void ButtonPressedCallback() {
isButtonPressed = !isButtonPressed;
}
This is all making the assumption that you are running a game and would have a game loop, or that you have some sort of update function that's called infinitely in your application until it exits. If not, consider looking at Android activities in order to do something like that.
private Handler mHandler = new Handler() {
#Override public void handleMessage(Message msg) {
update(); // The refresh stuffs
mHandler.sendMessageDelayed(Message.obtain(mHandler, -1), 1L); // wait
}
}
With this you wont need a button at all. In my opinion, I don't know why you have a button if you don't want to click it....
I have a button and while this button is playing an animation, I'm not able to click on button. I've set click listener and touch listener but in debug mode it's not entering in OnClick and in onTouch methods. Do you know why? Thanks
edit: I've tried something like:
AsyncTask task = new AsyncTask() {
#Override
protected Object doInBackground(Object... objects) {
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Toast toast = Toast.makeText(MyActivity.this, button1.getText(), Toast.LENGTH_SHORT);
toast.show();
}
});
return null;
}
;
};
task.execute(button1);
but it's not working
Edit: here is the full source code
Before Android 3.0, using any of the animation classes only changes where the view is drawn - it won't adjust its touchable-bounds during (or after) the animation:
http://android-developers.blogspot.com/2011/02/animation-in-honeycomb.html
You could:
Make a ViewGroup which moves its children every onDraw
Override your View's onDraw to move itself - Could use margin or padding, or position (if view is in a FrameLayout or something similar).
Only use 3.0+ devices
Override the parent's onTouch (or onInterceptTouchEvent), calculate where the View is being drawn (you can get how far into and the offset from real position from the Animation *) and handle accordingly... * Looking at your code (since you generate a random direction each time it finishes), this might not be possible without tracking which directions you've previously take..
you are facing same issue that i was recently ... when you apply animation on button or any other view and use setFillAfter(True) it means that the image of view is moved not the actual view thats why its not listening to your click listener because its just image of view not your actual view you have to do something like that i explained in answer to my own question according to your situation... means you have to also move the actual view on the end place of animation and use setFillAfter(false) so that when you click after anmation then it should be an actual view not just image used for animation purpose by android
check this link....
EditText stucks after animation and alive back on scrolling......?
In your code use setFillafter(false) and actually place your button at end position of animation by somehow like setting margin or according to your layout use appropriate properties for placement. By Applying these changes your click listener will work perfectly.
==> if you are trying that your button's click listener work while its moving (being animate) then as far as i know its not possible because android uses just image of your view to perform animation not the actual.
This might be a threading problem. You should read Event dispatch thread and how to do thing in android async by reading painless threading and take a look at AsyncTask JavaDoc.
In short: the main thread should not be blocked, since it is used for responing to UI events, such as button presses. Therefore, whenever you do something that take more than some milliseconds, you should do it asynchroniously in another thread.
I think there are two things
1)You can handle one event at one time for one object.Like animation is playing on you button that means you can not click on that untill one work get completed(As i understand you animation is on button not on other part of screen)
2)Second if you have playing on rest part of screen and you want to stop it on click of button.Then i think its a problem of focus.once set onfoucslistener to button and check that when you are clicking it Is it getting focus?
I would put the animation into the async task and then the button click should be handled normally on the main thread I think. Because the way you do it at the moment is: The animation starts and blocks the main thread. This means that the click event can't be excecuted in the async task
You must try to use AsyncTask . Programming Android without it would be and horrible usability serious problem.
See this link for how use an asynctask.
Here an example:
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// Initialisation of the Activity
MyLongTask task = new MyLongTask();
task.execute("http://blog.fr4gus.com/api/test.json");
}
#Override
protected void onPause() {
// If you wanna make something in the pause of the Asynctask
}
class MyLongTask extends AsyncTask<String, Void, Void>{
#Override
protected void onPreExecute() {
// Before executing what you want to execute
}
#Override
protected Void doInBackground(String... params) {
// what you want to execute come here :D
return null; // Or whatever you want pass to onPostExecute
}
#Override
protected void onPostExecute(Void result) {
// Here we can update for example the UI with something (who knows :? )
}
}
}
MODIFIED
You must always extend the Asynctask Activity , since you are trying to pass params through it , you must define them in the head of your extended class :
class MyLongTask extends AsyncTask<String, Void, Void>{ ...
Then first is the doInBAckground param , next is the onPreExecute param , and the last is the onPostExecute param . That way you can send the params like a Button.
You can learn more about here or in my first link.