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

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

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.

android studio issue related to emulator and real time mobile phone

I created new login page in android studio. that activity xml looks good on Emulator but in real time mobile its looks like irregular alignments what can I do for that? I used Linear layout for that.
The most efficient solution is to use ConstraintLayout.
ConstraintLayout has a flat view hierarchy, unlike other layouts, so it performs better than various other layouts. Only a single layout can handle your UI.
Or you may use a RelativeLayout, and then use LinearLayout as a child inside RelativeLayout, i.e., you will need to create a hierarchy with some nested views, which will adversely affect performance.
Most probably you hard-coded pixels, try converting to ConstrainsLayout. 1 LinerLayout can not provide enough flexibility if the layout is not simple.

Android keep button or view in front of all activities within the app without using baseActivity

After a lot of time spent I can't find the solution. I want a button or any clickable view should stay visible for all activities and it should be only for one specific app not like chat heads. I am basically making a library so that's why I can't use base activity.I have attached the image as well for a better explanation. How can I achieve this any suggestions? thankyou...
I
It sounds like you need a ViewOverlay. This is the API documentation.
ViewOverlay is usually tied to a single view, but if you wrap it in a fragment, you should be able to attach it to each view in your application. This should create the effect of an of an application scoped overlay.
There may be a more elegant way of doing this, but I am not aware of it.
EDIT: You can also wrap your layouts inside a frame layout(s) along with a seperate nested view (the view that you want to keep on top of the stack).
Frame layout creates a 'stack' of inner views. If you use this approach, you can programmatically ensure that there are exactly two views present and visible as children of your frame layout at all times. one will be the layout tied to your current activity. The other will be the view that you want to be overlayed.
I know that the term 'programmatically ensure' is vague. This is because there are many ways to make this happen. It is up to you to decide which way best suits your needs.

Android: Create ImageViews as and when they are needed

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.

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.

Categories