Android Studio set a string in several textview - android

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.

Related

Display items (TextView, ImageView etc) on timers in Android

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);

Avoiding lag in displaying complex views with ScrollView

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.

In Android, how can I make a form that changes depending on categories ? (the amount of inputs varies)

I'm doing a reminder application for Android that has an activity which is populated with inputs that depend on the category of the reminder. The amount of inputs (EditText) changes depending on the category, so im a little confused on how to tackle this case.
I propose 3 solutions here, but I'm not sure if there is a 4th solution or a better way to do it. I'm open to criticism. These are my choices:
I can make fragments for each category
Or I can make a common xml layout file that contains a number of inputs that reflect the category with most inputs, and find them programatically and assign them their properties
Or I can make the inputs programatically.
I'm trying to find the most elegant solution here. Thank you for your help.
What you can do is write all the inputs in one file,then set their visibility to hidden, and only programmatically change the visibility to show when you want it to. Tht way if you want to show three inputs, you can change the viibility of the three inputs and the rest of them would still be hidden, hence solving the issue.
Use some kind of adapter-based View. I recommend RecyclerView.
With it, you can covert your form objects into scalable View hierarchies in an elegant, efficient View, using an Adapter object.
You can use <include> to add your different screens and assign the visibility programatically

How to handle ListView in this situation Android?

I ran into the situation that I need a way to edit the data of list-view item from another activity. I can handle the click event on each item, then edit it on the fly. However, I still prefer to handle all the editing in a separate activity. My listview item is customized from BaseAdapter.
Here is my main page,
Each item within the ListView, contains two other TextView. When the I hit the menu setting, it will go to another activity where I can edit and update the information:
I'm currently having two solutions in mind. Either retrieving data from the previous activity and update the whole ListView (which I think it's kinda expensive in the case user just edit one item). Or I can just get rid of the ListView and create a bunch TextView in the xml, by doing this I can just reference to each TextView by their id. So my question is, which way is preferred in this case? Any suggestion would be greatly appreciated.
Your ListView is displaying Email, Name, Headline, etc? That should be a fixed XML layout with TextView entries, I think. ListView is for variable numbers of elements. You CAN implement it with a ListView, but I wouldn't.
However, your concern about updating the whole list being overkill, I wouldn't worry about that either. You're talking about 7-10 fields. The amount of time Android needs to run through its lifecycle and display everything will dwarf you updating a few fields.
You can use SharedPreferences for this. You can create a wrapper class through which you can access the preferences.Thats the usual way to go about solving these kind of problems. You can check this for details.
You can have it as a variable in your application class, so that you can access that in a global context.
Use text views instead. List View code has been optimized for large amounts of data only and not recommended for small data.

How can I avoid duplicate ids in android child components?

I've just ventured into the fun world of Android development, but had a very quirky problem with the test app I was working on.
The app uses a TableLayout where each TableRow contains an EditText and some Buttons.
The TableRows can be added and removed at runtime. It all appeared to be working okay, until I accidentally tilted my device. The display responded and rearranged the layout, but suddenly all of the values were the same on each row.
After some head-scratching I figured out what was going on. Because of the orientation change Android was restarting the activity. When this happens Android tries to save and then restore your instance state, but it does this by storing data relative to the component id.
In my case, because the rows are all created from the same layout, then the EditText in every row has the same id. The result as far as I can tell, is that when the info is saved it is being overwritten for each row, so that the last row wins out.
When restoring there is only one value associated with that id and so it gets applied to every row!
In my case I was able to work around it as I didn't really need to keep the values anyway. In my onSaveInstanceState I DON'T call super.onSaveInstanceState, and likewise in onRestoreInstanceState.
So that finally brings me to my question!
What if I DID want those individual row values to be saved and restored? Is there an accepted way of generating unique ids on reused components (in my case the TableRow)?
If I were you, I would not use your_view.setId(your_new_id) on an EditText view, because this makes your app less stable: What if another view happens to have the exact same Id as your_new_id?
I would use your_view.setTag(some_unique_tag) and then use findViewWithTag(some_unique_tag) to look up the EditText view again.
findViewWithTag(some_unique_tag) could be any Object - I personally prefer String because then it's easy to make some descriptive and unique tags.
Remember, it's only the Views that you use .setTag on that has tags.
In addition to setId there is a generateViewId method in the View class. If you want it pre 17 versions you can just copy it from sources.
You could generate your TableRows in Java and use View.setId(). You might also put the table row in a XML layout file, load it via java & set the Ids - but seems more tricky.
http://developer.android.com/reference/android/view/View.html#setId(int)

Categories

Resources