Performance Issues while building custom DatePicker - java

for my project I need to build an own, custom DatePicker. I can't just use the standard DatePicker, because I need features like selecting multiple dates, disabling special dates, etc.
I planned to build my own custom DatePicker as close to the standard DatePicker as possible.
Therefore I have decided to use a ViewPager in which I nest a custom View that holds a custom ViewHolder containing just a TextView to Display the current Month and a RecyclerView to Display the Actual Days of the month in question.
The result looks something like that:
I don't think it is necessary to provide the whole code (adapters, layouts etc.) because the whole thing is fairly straight forward but if you need to see, I can add it after the fact.
Now to my problem: When I start the activity, the whole application stops for about 3 seconds, and I get the following logcat-output:
I/Choreographer: Skipped 111 frames! The application may be doing too much work on its main thread.
I narrowed the problem down to the RecyclerViews.
(when they are deactivated, there is no freezing/stuttering whatsoever)
When the activity starts, the application has to load 3 full Recyclerviews (because I set viewPager.setOffscreenPageLimit(2); )
and that initial loading causes the whole application to freeze for a short amount of time. After everything is loaded, the whole thing works completely lag-free.
To this problem I have 2 different solutions in mind:
Load the RecyclerViews in another Thread
The idea is, that the activity starts, and the RecyclerViews are only shown, when they are loaded. This wouldn't be perfect, but at least the activity would start without causing the whole application to freeze.
Substitute the RecyclerViews with something better performing.
Is there even a View/Structure that a) enables me to display a gridlike Structure and b) is better performing than a recyclerview?
Initially I chose the RecyclerView for displaying the days, because I thought that the Standard DatePicker uses it too, but after some digging in the integrated classes I am no longer so sure about this.
To me it seemed like the days are drawn without any View whatsoever. (I could be wrong about that)
Is there any possiblity, that one of those ideas could be working?
And if not, is there another way to fix the performance-issue?
Thanks in advance

Related

How to update content of the activity without creating new Activity?

I'm trying to make an app and I have made a blueprint for a specific activity, but I don't know how to implement it. The layout contains few buttons at the top of the activity, and each button features some information, which is displayed inside the view. The view which needs to be updated is present under the buttons. I don't want the activity to be changed, instead it should update the contents of the View, which is different for each category/button.
By doing some research I have realised that the "Tab Layout" can be used to achieve my requirements, but I don't want the tabs and I need some stylish buttons as a replacement.
I know I'm not the best at describing, so I have looked upon Dribble and found one design which is 100% similar to blueprint.
I want to achieve this using XML and Java using Android Studio! Every suggestion will be a great support foy my app.
Thanks a lot.
As far as I know, you could achieve that by using fragments (which is the same concept you would have used on TabLayout). I don't really know how much knowleadge you have on Android, but if you know what a Fragment is, it should be easy for you to recreate the idea.
You have 3 buttons for 3 different fragments, so you must design every fragment by separate and change it depending the button you click.

How to construct the RecyclerView for the following UI vertically?

The expected UI for my application should look like this:
I have thought about using a view pager but I cannot figure out how to change opacity of the next items or make the item in view to be centered and be elastic.
I have researched multiple open source GitHub repos and found two which seems similar to what I have approaching to do.
https://github.com/bloderxd/deck
https://github.com/Ramotion/garland-view-android
I do not know how to solve the problem of opacity or going to the next item using the next button in the UI (which was designed by me for a project).
So far, I could only create a simple XML representation of the UI without the view pager but I do not know what other libraries and views I need to implement to make it interactive and elastic like in the UI.
My request is how I could build the application level code for this UI. If there is any other obscurities in my questioning, feel free to comment and reach out because I am relatively new to this kind of development. Thank you.

Is it more efficient to declare a view invisible or set the background to transparent and change it's background resource when needed?

Trying to get the most of my app means making more efficient code to help the user have a better experience. So I was wandering if anyone could help me and let me know which is a better choice for efficiency.
I have views which come to life at various times in the program through animations but their places are still held in the main layout so the buttons are constantly in existence. I was trying to figure out whether it's more efficient to go ahead and set the background resources in the xml code and just set the visibility to invisible and then change it back to visible dynamically when the views are needed, or is better to set the background resource to transparent and change the background resource when needed dynamically?
I'm aware the difference is minimal but when trying to use an app the most frustrating thing can be speed so knocking off even a quarter of a second in loading time is a step towards more efficient and more complete experience for the user.
If you're concerned about having many views in your layout that are seldom used, check out the ViewStub class. It's basically a placeholder that you can use to lazy initialize views that might not always be required.

View different images depending on what item in my list view I click

I'm fairly new to Android programming and I've got this project I need to finish and I'm currently stuck.
I've got a standard listview in a Menu class with an array containing around 20 different elements. So what I want to do is load images in an imageview depending on which item in the listview I click, and since I'm a beginner the only idea I had was to make a new activity for each imageview which seems like a pretty bad way to do it since I'd need about 20-30 new activities..
To sum things up what I want is:
Code for making ONE activity that will display a different image depending on which item in the listview I click, probably pretty basic coding I want as simple solution as possible.
If possible I'm also looking for a solution that includes an SQLite database that stores the URL of an image and then display it in a single activity, also depending on which item I press in my current listview.
(I hope you understand my needs, and if you need I can also post my current code for the Menu class if it helps you help me) Or you can just show me a different way to make this work, I appreciate every answer! Thanks in advance!
NOTE
And please keep in mind, I'm a noob at Java and Android so keep it rather simple or at least explain what you do.
When you click on a list item, display the image in another view in the same layout, unless you want the image to take up the entire screen real estate. If you want it in the entire screen, go to a new Activity by sending the activity an Intent.
Activities are the "controller" of your application. They interact with the visible UI and the input from the user. You don't need a separate activity for each image, just an activity that's associated with a "place" in the UI (an ImageView) where you'll display the image.
I'd start by adding the images as resources under res/drawable before going on to databases.
You are going to have to do most of this yourself. There really isn't any substitute for taking the time to learn Java and Android. There are several tutorials and Android University classes under the Resources tab in the Developers Guide; I suggest you do all of them.

When to use new layouts and when to use new activities?

I'm making a game in Android and I'm trying to add a set of menu screens. Each screen takes up the whole display and has various transitions available to other screens. As a rough summary, the menu screens are:
Start screen
Difficult select screen
Game screen.
Pause screen.
Game over screen.
And there are several different ways you can transition between screen:
1 -> 2
2 -> 3
3 -> 4 (pause game)
4 -> 1 (exit game)
4 -> 3 (resume game)
3 -> 5 (game ends)
Obviously, I need some stored state when moving between screens, such as the difficulty level select when starting a game and what the player's score is when the game over screen is shown.
Can anyone give me some advice for the easiest way to implement the above screens and transitions in Android? All the create/destroy/pause/resume methods make me nervous about writing brittle code if I'm not careful.
I'm not fond of using an Activity for each screen. It seems too heavy weight, having to pass data around using intents seems like a real pain and each screen isn't a useful module by itself. As the "back" button doesn't always go back to the previous screen either, my menu layout doesn't seem to fit the activity model well.
At the moment, I'm representing each screen as an XML layout file and
I have one activity. I set the different buttons on each layout to call setContentView to update the screen the main activity is showing (e.g. the pause button changes the layout to the pause screen). The activity holds onto all the state needed (e.g. the current difficulty level and the game high score), which makes it easy to share data between screens. This seems roughly similar to the LunarLander sample, except I'm using multiple screens.
Does what I have at the moment sound OK or am I not doing things the typical Android way? Is there a class I can use (e.g. something like ViewFlipper) that could make my life easier?
By the way, my game screen is implemented as a SurfaceView that stores the game state. I need the state in this view to persist between calls to setContentView (e.g. to resume from paused). Is the right idea to create the game view when the activity starts, keep a reference to it and then use this reference with setContentView whenever I want the game screen to appear?
This question has been asked a lot. Did you read these other posts?
Android: What is better - multiple activities or switching views manually?. This link in particular talks about the Android Design Guidelines which "don't mention switching Views at all; it's centered around an Activity-as-View design."
Android - Should I use multiple activities or multiple content views
Android app with multiple activities
How to pass the values from one activity to previous activity
I'm not sure what you mean by the back button not always going back to the right screen correctly. I've got a game with a similar structure to yours, and the back button always takes the user correctly up the chain of activities.
Furthermore, using the onResume, onPause etc is somewhat necessary. What happens to your application if the phone rings? (Yes I know, some people still do strange things like using their phone to receive calls! :P) The OS tries to call the onPause method in your activity, but if that isn't implemented then your application won't act as expected. Another useful thing with onResume is it lets you update your tables as soon as the user returns to the view. For example, your player has just completed a level and is then brought back to the select difficulty screen. If you simply recover the previous screen from memory, it might not have been updated to take into account that a the level was just completed. However, if you put some code in onResume to handle that, then that will always be executed before the player sees the screen.
Lastly, you say transferring data around activities is a pain with intents - yes, that's probably true. But I usually find transferring any kind of complex data a pain, no matter how you do it. Are intents really that much worse, or is it just that things aren't as easy as you'd hoped? I don't mean any offense with that; I also often find things which intuitively seem easy to be rather frustrating to implement in code.

Categories