Android: Create ImageViews as and when they are needed - java

I have a task in my application and I'm just trying to think of the most efficient way of programming it.The aim is that when my application receives some new data then it should show a new ImageView on the screen, so if it receives three sets of data then three ImageViews should be shown on the screen and so on. This will only ever be up to 32 ImageViews.
So my question is, should I create the 32 ImageViews in my XML code so that they're already there but only made visible when they are needed or should I create them programmatically in my Java code as an array and create completely new ImageViews when new data is received?
Just looking for the most efficient way to program this for the best performance and cleanest code

You should try recyclerView/ListView/GridView. They take care of create & reusing ImageViews for you.
If you really want to manage ImageViews yourself, e.g. you want to scroll quickly without jank caused by creating & initializing ImageViews, you could create 32 ImageViews programmatically at first, and use them when needed, like ImageViews pool.

Related

can I create many different indexed ImageViews using the same background? (android)

I'm making some sort of orb-dodging game and I want to create many different orbs. usually, to create an Image\button\whatever I make a single thing in the XML and assign it to an object on the java code. it does work well when there are a few objects but can I make an essentially unlimited amount of orbs using a single picture?
XML was introduced at the beginning of Android for "easier" layouting (it turned out it wasn't so much, got own problems, currently new approaches are more popular), but all of its possibilities can be achieved with code. e.g. creation of new TextView instead of findViewById form XML: textView = new TextView(this); (this for Activity, if you are in Fragment just use requireContext() instead)
remember that Views created this way won't appear on the screen, they must be added to Activitys/Fragments layout (using setContentView or onCreateView respectively)
btw. making a game using "unlimited amount" of Android framework Views is a very bad idea, you will get VERY poor performance... at least use Canvas drawing

Android Best Practice: Dynamically adding buttons to a ViewPager or Pre-Instantiating Them?

So I have a question about best practice for dynamically creating and sizing buttons within a ViewPager, based on a changing external state.
Essentially, I have a scrolling view and I want my ViewPager to contain a number of buttons of a specific size and count depending on what part of the scrolling view is currently visible.
My question is about deciding the best implementation of this feature, I see two options: would it be simpler to
Constantly create and scale new buttons whenever the scrolling view moves
Make the viewpager itself contain a scrollview and fill it with all of the pre-scaled buttons on app startup. Then, whenever the user scrolls the main scrollview the viewpager's scrollview (which contains the buttons) will scale programatically
Any ideas on which would be the simpler and more robust system?
Not much of an answer but I will leave a comment for ya! Basically you can do it either way, both aren't to difficult to accomplish, however I would probably go the dynamic route because it will always scale correctly. Doing a set amount will only work until devices become larger, or if you are targeting tablets or tvs then it will start to become extremely messy in the xml file for the layout. Dynamically adding also gives you far more control and saves time later on, you can simply change a number and have 100 more then going through and manually adding even 10. Hope this helps!

Two Separate Scrollable Fragments on the Same View - Android

I'm trying to create a basic UI for a simple Temperature Conversion app (new to android developing), and I can't figure out how to create a SINGLE page with two SEPARATE scrollable view pagers, one occupying the top half and the other occupying the bottom one.
I am using Eclipse. Feel free to ask for any other information required for your answer.
This is a rough drawing of what the layout should look like.
I'm trying to create a basic UI for a simple Temperature Conversion app
I do not know why a temperature conversion app would need one ViewPager, let alone two in the same activity at the same time.
I can't figure out how to create a SINGLE page with two SEPARATE scrollable view pagers, one occupying the top half and the other occupying the bottom one
In terms of the ViewPagers themselves, use a LinearLayout or something to vertically stack them.
Your bigger headache will come with the PagerAdapter implementations. At least one of these will have to be something other than FragmentPagerAdapter or FragmentStatePagerAdapter. Those implementations assume that they are the only such adapter for your activity, and I would expect that having two will cause collisions.

Create a view class that is a combination of two views Android

In my app, I have a screen that displays some images next to text. These images are downloaded from the internet, so there will be some latency in displaying them. Right now I have an ImageView and a ProgressBar overlaying each other, and toggling the visibilities when the Bitmap becomes available. Is there any way to combine the two into one class that will handle it all in case I want to use this somewhere else?
One way to go about solving the problem is to make your own custom view which extends ProgressBar and draw the image in the ondraw(Canvasn canvas) using a Rect. That way, your image can be embedded in your view and you can always make it reusable by allowing your self to set the image via a setter/resource xml files which specify the attributes that go along with your custom view. Here's a reusable ProgressButton that I wrote that maybe of use to you, it shows how I did something similar to what I described (it's one which I think works well):
PregressButton
Yes there are a number of ways to combine widgets into a single custom class. Have a read of Custom Components. This should help you decide which approach you want to use for your situation.

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.

Categories