I'm fairly new to android and was wondering if in my activity I can display different text views for a certain amount of seconds before they vanish, replaced with a different text view.
Is there any way I can handle these events within an activity? I am creating a simple game and would like the Views in the activity to display after one another.
I could not find any help online (may have been searching for the wrong thing)
You can definitely use a timer variable to keep track of time and then use the setVisibility() method to set the textview visible(View.VISIBLE) or invisible(View.INVISIBLE).
For example:
TextView textView=(TextView)findViewById(R.id.text);
textView.setVisibility(View.INVISIBLE);
Related
I'm studying Android Studio and I ended up with a problem, and I needed a light.
I made several TextView on my application screen
and when I push a button I would change the contents of all TextViews that are on the screen.
In case I was using findViewById (); and setText (); to change the content of each one of them, and is working well.
More in case are several TextView and some of them will receive the same value for example 10 of them I go for the same String and another 3 will receive a different string
Thinking about having multiple TextViews that will receive the same string, is there any way to create a kind of clone of it that when it changes the others change together, to decrease repetitive commands?
Easy solution
fun setText(text:String, vararg views:TextView){
views.forEach{it.setText(text)}
}
But you should avoid calling findViewById multiple times for same view, it takes a lot of time, so you should cache your textviews, and be aware of state changes.
Right solution
If you have more than a few textViews, you should use recyclerview. It allows you to render different type of views, and bind them from your datasource. Here is a Guide for you.
For the development of my app, I realized I needed a complicated view (let's call it foo), it contains three ImageButtons, a progress bar, and three TextViews, all of which are dynamically changed by interacting with the same view's elements. To make this work, I extended foo from RelativeLayout, dynamically created the sub-views then added them to foo (this.addView(...)).
What I planned to do next was add them dynamically to a ScrollView. I did this and put three foos for testing. The result was extreme lag. I'd press an ImageView (which should change its image on press), and it would take 2 seconds to do so.
My final aim would be to support 50 of these foos at a time and have them work smoothly, with the user having the option of loading more (without overwriting the previous ones) if he/she so chooses. All interactions will use the internet (I dunno if that's relevant), but the testing was done with all the network tasks commented out.
My questions are thus:
Is the strategy I was using (ScrollView & add foos to them) viable, and the lag is from some other issue (the specific code in question, in which case I'll provide some code)? Or is it really a bad idea to do that?
What would be the best way to reach my goal here (assuming 1 is bad)?
What I already know:
I've researched my problem a bit, and most online sources recommend using a ListView. I didn't read much into it but from what I got:
I'd have to redo the design using xml rather than dynamically
The different components and their values will be stored each on it's own array which is extremely unacceptable in my situation (changing the sub-view's values should be done very simply and should not appear in the main activity)
I can't (or it's difficult to) set OnClickListener's for the different sub-views (as only the main foo view will get one)
I also tried this method (ScrollView and add to Views) with another View and had 20 of them run at the same time seamlessly, but that one had been extended from View and only used canvas to draw text with no sub-views.
Thanks in advance.
I have some activities displaying the current date and time.
I want to make only one text view (layout) txtTime showing time and it will be updated each second by a handler in main activity.
Then others views of other activities can re-use the text view txtTime by including.
(I want only 1 handler updating date/time value and all app activities can display this value)
In fact, I don't know how to create/access a text view like that txtTime.
As usual I find a text view by (Activity).findViewById(R.id...).
But since it does not belongs to any activity I got stuck there.
My pb:
How to use a TextView as an application circle object which doesn't belongs to any activity?
How to change its text value(Date or Time)?
Do you have any suggestion/solution for me?
Thanks,
# The reponse of Remy:
You want something like that : static final TextView appTimeText = new TextView(appContext);, And in the handler appTimeText.setText("Text"); Now when you create the latout in any activity simple add it. – Remy
Tell me if I understand you currectly, you dont want to make your textview inside an any activity, because you want to use it in all the life circle of the application?
So if im understand you currect you want the application context.
Then you can retrievegetResources()
Im hope im understand you properly.
If you want other activities to get the text view from the 'txtTime' layout then <include #layout/txtTime' /> in the other layout.
I have a dynamic UI that I need to generate and I would like to know what the best approach would be to do this and why? The application that I need to make will either way only have a single activity in which to display various different views which are generated by code, so not in an XML file.
So basically I want to draw one set of views and have a user interact with them (textview, button, radio button, edittexts etc). Then save the data he generated, clear the canvas or screen and within the same activity generate the next set of views for the user to interact with.
I have done extensive research and I know that I can use either a Fragment or an Activity with a LinearLayout to achieve this, but I am not sure which would be better and why?
Thanks,
Wihan
I have a ListView that displays a set of notes, each with varying ammounts of data (i.e. some have a due date, others don't).
Currently, each view in the list is a RelativeLayout containing a TextView for each field, plus two Button and a CheckBox. I then simply hide the unused fields by setting visible false on each one.
This has worked well, but I'm about to add a lot more data fields to the notes and inflating that many unneeded views for each row will surely kill my app. I need a more dynamic solution.
I've decided the best way to go is to create a custom view. How can I implement/design my view so that it can display a variable number of text fields without creating/destroying textviews each time (which would be quite expensive and worse than my current situation), or maintaining a large pool of hidden textviews?
You can create a class that extends LinearLayout
and use addView to dynamically place your views.
Sounds like you might want to look into a view with a stub. The stubs will save space until they are inflated, so each row will be lighter until it is used on a heftier view. If you have a relatively low number of these larger views you might save a bit of overhead.