Android varying number of textviews to display - java

For exmaple I have int variable "number" that is passed throughout the program
and if the number = 7 I want to create 7 identical textviews and create 7 IDs for each of them
if the number = 5 I want to create 5 textviews etc.
My guess is that I should not create any textviews in my xml file and rather create views in my java code. Is this the right approach?

I would do this with a XML holding the EditText and a ListView in another xml, then in the Adapter of that ListView you can inflate and instanciate the EditText XML items.
This way you stay much cleaner and it might be less work if you change style etc. You do not need to take care of a scrollview yourself for more items.
Also you can have items and ListView adapting to screendensity, screensize, orientation etc. without going through a hell of code - if this is a requirement.
If the last item in the list needs to be a button, you could inflate and instanciate such an item layout too in the Adapter.

If your layout is unknown at compile time (for example, the number of Views is not known), then your only choice is to do it programatically.

Related

Is there a better way to add an xml item to a linear layout so that each elements in the xml item has its own id?

I want to create some CardView items dynamically, depending on items I get via a REST call. I created a CardView XML layout that contains many single elements with many settings. I don't want to write this XML layout in code to create it dynamically. Is there an easy way to do this work?
Each CardView item has its own name etc. therefore, the IDs of the elements inside the CardView must be different.
I think it would be a good idea that you take a look at this tutorial. https://guides.codepath.com/android/using-the-recyclerview
Basically what you want to do is create a basic CardView item, with its correspondent ViewHolder and inflate the contents of each item when you get the result from the API call.

Adding Heavy UI in in each ListView Item

I am bit more concern of OOM on having a heavy UI on each item of a listview, example I have 100 items.
Below is the image that I wanted to practice and copy the UI layout. What would be the possible layout or how can I implement this type of UI. Each items are scrollable until the bottom, with separate contents but same layout. Please see my update below.
Update: I am planning to use card ListVIew, please guide me.
You can use simple listView or card ListView but make sure listView raw does not have many nested layout for avoiding nested hierarchy you should use Relative Layout in list raw
I think using a listview with custom row layout will be better for your app.This will help you to reuse the same layouts which are inflated when loading listview.Also please check the size of images you used in the rows.

Can i change the value in a Text field on listview

I have started to manage the Points and Details of a sport competition. I have displayed Competitor's ID and Name from Database. But i need to Get the Points which they got at run time Dynamically.
And the List of competitors must be changed Dynamically for verity of Games. There are Various games like Tennis, Chess, Running and so on..
Every Game that contains different Players and different no.of players. Can we Use this with ListView?
I need to solve this problem of Giving the points to the Competitors..
You could use the listView.setAdapter(); to set the data on the listView. Also, you could add a TextView as part of the implemented getView(position, converView, view) and return it to the listView.
You could also have a tableLayout inside of the listView. You would have a seperate layout.xml file for the tableLayout. This would have 3 columns. And return this as part of the getView() method.
You Can use the ListAdapter with TextViews and Data place holder

Java ListView class?

What is the difference between a listview layout xml and a Listview Java class? I am trying to make it when a user inputs text and presses enter it comes on a set position on the right but when the user receives a reply it would show up on the left side of the list view. Like text messaging on android phones. Does anyone know how i can do this? With a java class or an xml layout? I want animations and dynamic content on the listview as well. Any ideas?
ListActivity provides you all the built-in features of and methods of ListView but only ListView can be added into the whole Activity. By using ListView in xml, you add multiple Views such as Buttons, EditText on the same Activity.
Basically, the xml is for seperating the design (the visuals) from the code (functionality).
Although you can do some funcationality via xml, it's usually better to do it in Java code in your class.
I guess you can create a ListView that will iterate through 2 different list enteries:
The first will show the data (the text in your case) on left and the second entry will show on the right. For each list entery you will create a different xml layout with widgets aligned to the left and right as needed.

Android: create a listview that covers half the screen and updates itself

I'm writing an app in which you enter a few letters and it creates a list of words that use those letters in that order (but not necessarily consecutively).
I would like my listview to only take up the screen underneath the text entry field and two buttons (the text field and buttons are in a linearLayout and on the top of the screen not taking up much room).
Also, I have an arrayList of all the words I want to show in the list but I don't know how to make it update with the new arrayList each time the search function is invoked.
Thanks for any help.
Also may you might want to check out adding a header to the list view... something like...
lv = getListView();
LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup) inflater.inflate(R.layout.myheader,
lv, false);
lv.addHeaderView(header, null, false);
Maybe put your first LinearLayout (the small form) and the ListView in a second LinearLayout (vertical).
Then for your data, you can use an ArrayAdapter (or extend it but it seems like your list item layout should be simple so no need really) to link your array to the ListView, and you should be able to find adequate methods in that class among : notifyDataSetChanged(), getFilter(), various methods to update the inner adapter ArrayList etc.

Categories