I am not doing any network operation like downloading images or media in my Ui thread. i have a very complex UI that takes around 8 seconds to load and black screen comes in between , is there any way i can optimize it or show some kind of progress bar while my second activity loads its UI and remove the progress when i am done.
I cannot use asynctask doInBackground() since everything is UI only --Like For loops for creating LinearLayout childs dynamically, Putting views side by side on relativelayout and reordering them as per business rule. changing various image colors as per business rule etc.
Is there any way i can optimize this and reduce loading time?
Why you need such a complex layout in the first place? if the layout is too complex then it should NOT be. Try to find a simpler layout, if you must follow the current layout then I suggest an idea but it is not recommended.
Here is the idea, it's based on iOS development, you should take a snapshot of the complete layout (without any actual content, just layout) and display on top of your layout when you first load your activity. After all of your underneath layout already loaded, then you remove the snapshot on top and make the real layout visible (all the process must show the loading progress to avoid user interact with your UI). Although this is not ideal, it should help in your case.
Related
I am creating layouts at run time using heavy data, and adding these layouts to a scroll view. After the view get created, its working fine. The problem here is, the data is very heavy and it takes more than a minute to create the screen, which is not so good user experience. I want to create layouts only for the part of screen that are visible, and rest I can create on scrolling the scroll view. Pls suggest how is that possible? Also, If someone has a better approach, Pls suggest.
You can start by creating only a set number of views each time[1], but always add a dummy 'loading' view at the end of the list if there are more views 'pending'. As soon as the user scrolls the ScrollView at the end of the list, start loading the next part of views on a background thread, and as soon as they are built, remove the dummy loading view, and add the new views into your container.
An other approach would be to start loading the next group of views, as soon as the previous group is done finish, but that might be a waste of resources.
An even better approach, is to combine those two methods described, and always have the next group of views being created, if the user is halfway done scrolling to the end
You can check how to know when the scrollview scrolls to the bottom here: Android: Detecting When ScrollView Hits Bottom
[1] Since you care about UX I would suggest that the number of rows should depending on the row's height and device max height. I.e. 4 views on a small device, 6 on a medium, 10 on a large.
This question is a continuation of "loading long webview over 500kb".
So as the topic suggest it reduces file loading over 500kb long. My idea was to use 5 webviews and merge them together on a UI threading which displays them on the screen. Reason for this is 5 webviews should make it loading seem faster since it only requires to load 20% of the current total data string to be placed in the webview.
Now the problem is actually synchronizing it meaning webview1 when reaching the maximum scrolling index goes to an event handler which goes to webview2 and webview going under the scrolling index would go back to webview1. This applies to all webviews. Could someone direct me to an override event handler that when the scroll goes over the initial 100% of the amount it then gives me the handler.
If it is possible the simpler option would be to split the data into separate views, then you could use a AbsListView.OnScrollListener (only available with AbsListView and not WebView) and a list or grid view to present the different views. This seems like a much cleaner way than switching between views but I don't know how you have your layout and perhaps this isn't possible. You would have to arrange your views into a list and then render each view on a back ground thread and push it back into the view when it is ready. You should check out PullToRefresh for a great example on how to do this.
If the above doesn't suite you another option would be use the onTouchEvent to inform you when a user might be scrolling and try calculate using whatever scroll offset methods are available in your Layout/View, when you have hit the bottom but this is very complex. ScrollView has methods to inform you if you have over scrolled which might help
Is it possible to animate the background image of a layout (RelativeLayout, LinearLayout, etc.) in Android? In one of my applications I am fetching an image from the internet and displaying it as the background image once it's loaded. It would be really cool if the background image was swapped out using a fade effect or something like that.
I've tried quite a bit, but couldn't seem to find a solution. It's always the layout that's animating and not the image itself.
Might be your best bet to just layer your layout in a FrameLayout or RelativeLayout with a ViewSwitcher behind it. See the demo (and related layouts) in the Android API Demos app (that uses an ImageSwitcher, which is a special-case adapter-based subclass of ViewSwitcher). It does mean an extra ViewGroup level in your app, but unless you're doing something especially complicated (nesting these in ListView rows or something) it should perform just fine, and handles the transition animations for you.
You could also just use WebImageView from DroidFu, which does most of the annoying legwork for you.
My app i'm currently developing needs to refresh a Linear Layout that i'm dynamically adding Views too. I currently am adding all of the Views within the UI thread but have a separate Dialog class which upon a button click within the Dialog will need to refresh/Re draw the Linear Layout. Is there a way I can refresh the Linear Layout within a different class (trying to stay very object oriented in my program design) or a way to use a game loop for a Linear Layout to refresh the View every 3 seconds? Thanks!
I'm pretty sure you don't need to manually "redraw" anything. As soon as you change the contents of your layout (and as soon as android can get around to it (which will be a LOT faster than 3 seconds)) you'll see the updated screen. As to what thread you do this from, you can only change your layouts from the main thread, as far as I know, but you can call Runnable methods in that thread via a handler from anywhere.
We have an data-driven activity which constructs a large set (typically, up to 100) of Button-like components and places them in a Scrollable. Each of the buttons is inflated from a resource and are based on a RelativeLayout. The button has two text views, two image views - both from resources - and has a background of a 9-patch.
The size, position, text, and configuration of the image views are all driven from a database query.
Unfortunately, this is taking between 2-3 seconds to layout on an HTC Desire, 3-5 seconds on an HTC San Francisco or HTC Sapphire.
Once the initial layout is done, the view performs beautifully.
We have tried a number of strategies which made no difference:
time the database query - this was not a significant part of the delay.
cache the buttons, to reduce the amount of layout inflation.
constructing a subtree of views outside of the “live” view hierarchy, then connecting it when complete.
doing the same on another thread, but connecting the subtree to the activity on the UI thread.
We have an indefinite progress indicator (spinner/throbber) which spins while the query runs on another thread, but then freezes when layout gets going.
So, my question are these:
how can I make the layout of the views seem more responsive or
how can I avoid the thobber locking up while layout occurs
Thanks.
Edit
The scroller is set to scroll horizontally and vertically. So we have a grid which the screen is a viewport on.
This makes using the built in ListView (at first glance) unsuitable for the task.
The progress indicator will not redraw during layout, because it's all happening in the UI thread.
To improve performance you should use a ListView, it's scrollable and the items can be customized (you can reuse your RelativeLayout based "button") with a custom adapter, and it allows to recycle the items while scrolling. See Recycling views in a listview, worth it?, and above all this excerpt from CommonsWare's book on Android.