Activity containing image button take too much time to load


Activity containing image button take too much time to load



enter image description here



I have an Activity that has 50+ image button. Each image is about 30-40 kb. When i open the Acitivity it takes 2-3 second.
What can i do to open it fast.


<Button
android:id="@+id/desi_unique"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_margin="5dp"
android:background="@drawable/unique"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="5dp"
android:text="@string/unique"
android:textColor="@color/black"
android:textSize="15sp" />





You can use multiple AsyncTask to do that job.
– Raj
Jul 1 at 9:53





@Raj how i can.
– mukeshsoni
Jul 1 at 9:53





Try googling RecyclerView
– Digvijaysinh Gohil
Jul 1 at 9:57


RecyclerView





@DigvijaysinhGohil enogh knowledge of recyclerview. these buttons open recyclerview fragment that loaded data from server.
– mukeshsoni
Jul 1 at 9:59





Check updated answer pls
– Dennis
Jul 1 at 10:01




3 Answers
3



Simple answer: CACHING those images.



If you are gonna use Drawables, try COMPRESSING THOSE IMAGES.



How?



Use a library named Glide, by Google.inc. It will automatically cache images for you.
Of course, whenever you'll first load the activity, it will take some time, but once it's cached inside your memory, it won't take much time if you'll launch the activity next time.



Disclaimer: The above statement's scope is limited to a single launch of your application. Once you'll kill your application, you'll have to load them again.



BUT, loading so many images(from drawables) is not considered as a good practice, it hits the performance of your application by many folds. Thus you should change your UI.



But again, Recycler View sounds a better option when loading/displaying so many images.





how can i use recyclerview ? all button are static and images from drawable.
– mukeshsoni
Jul 1 at 10:08





Even RecyclerView can use Drawables. But as your buttons are static, you don't really have to. The thing is, real-world applications don't work like that. The data is always dynamic i.e. needs to be fetched from the server. And by data, I mean the images as well.
– Dennis
Jul 1 at 10:10





i'm compressing images and it's good idea.
– mukeshsoni
Jul 1 at 10:11





Yea, I always do that.
– Dennis
Jul 1 at 10:11



Using third party library like Glide or Picasso is efficient. I think when the number of images is more than 50 you can change the application UI and use recyclerView also. I think it's better way for this purpose.



You can use RecyclerView with GridLayoutManager to have the same UI and of course you can use one of third-party libraries like Fresco.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to add background colour in existing image using Swift?

Moria Casán

How to make file upload 'Required' in Contact Form 7?