I can use multiple recyclers views in xml and assign separate ids and populate them as I want with different adapters and so on.
But I have to create recycler view in xml and initialize in Java and populate them with respective name.
Is there some other means to do. Like with include layout using loops. Can you guyz suggest something.
Like play store has multiple horizontal scrolling layout one below the other just like that.
I'm making an app where there's one main activity that is a Fragment. I basically have a few tabs and each tab/page layout I want a different layout. For one of them, I want to know if I can dynamically create a table of, say 20-ish rows with X amount of columns. Instead of having to manually create each row inside the layout .axml file.
The reason for wanting to dynamically create it is that I'm obtaining some data from online that I want to automatically put inside a table.
You will only need to create 1 layout for the item and it will be your item 'template' to inflate it into your ListView dynamically. Take a look at this RecyclerView example (which is the improved ListView)
I have a layout which takes user's children information. Let's say that this layout takes info about the children's first name, last name, and age. I am taking inputs with EditText. And there is a spinner which shows children Allergy type, on selecting 1 Allergy type it has to fetch its details from webservice and to fill those details in a textview.
So in this way the User can add as many children as he wants. The problem occurs there. Let's say the user has added that layout 4 times now, he selects the spinner of children 1 and service gets called and it fetches the information and fills it in the last layout textview that was added.
where as it is expected the details should fill in each textview of each layout added accordingly.
Confusions :
How can I exactly get which view is clicked and then how to take action in the same layout of that view not the one which is added recently?
I am inflating layout which has the specific set of fields specified above. So I am maintaining the Array List of each layout added , so Is there a work around to get exactly the same view and its corresponding view in that layout ??
UPDATE 1: Some Idea of my question
There is a main activity it has 1 button named "Add More Children". When user click on this the layout which contains the children info adds in the specified area in the ScrollView, so that user can add as many children as much he want.
so basically I have drawn some views below where as there are too many views in the following layout named children layout.But this layout shows what type of work is need to be done . so see below and have some idea
For demonstration you can see there are different edit text and spinners. Spinners get updated from Webs service and each children may have different data loaded in spinner from webservice. this whole layout inflates into the mainactivity. on button click named Add Children . so on that button I am adding this in the scrollview
private View addChildLayout(int childLayoutid, LinearLayout Targetlayout) {
//where childLayoutid is a layout resourse id of childern layout
// where Target layout is a scrollview in my fragment
LayoutInflater inflater = LayoutInflater.from(getActivity());
View inflatedLayout1 = inflater.inflate(childLayoutid, null, false);
inflatedLayout1.setId(numOfChildAdded);// numOfChildAdded is int number of children added by user so far
Targetlayout.addView(inflatedLayout1, 0);
numOfAddedChildLayout.add(inflatedLayout1);//this is an array list i am creating to keep track of each layout added by user on add button click
return inflatedLayout1;
}
after adding this in layout another method I call that finds the ids of this layout and sets the click listener of each views i.e spinners or edit text or whatever is needed. Now suppose User has added one children. and clicks on the spinnerAllergy types that fetches sub category and populate spinnerSubCatogry, and spinnerSubcategory when clicked the web service again gets called and gets details of allergy and fills in the Textview (the large box shown in picture)
Now the main problems comes in when user added Children no 2. now let suppose user has added 2 childs , but he clicks on the spinnerAllergy of Child1 , the child1 spinners runs the web service but populates the spinnerSubcategory of Child2.
It Looks like that when the new child is added the click listener refers to new layout which is newly added .
so that is the main problem . I want each view in each layout work accordingly in its views and boundaries. I mean child1 layout views click listener should populate its views not the currently added layout views.
I think that is enough information to get some clear idea.
Please help me with this, I've been stuck here for 2 days. Where as all my design is working good.
Well as you stated above that you are keeping track (List of ) every single view you have added. I will suggest you to use that
Here are the lines you are using and setting the id so its mean each parent has the different id where as their child views has the same id ,
View inflatedLayout1 = inflater.inflate(childLayoutid, null, false);
inflatedLayout1.setId(numOfChildAdded);
As you are adding each layout with different ID why dont you simple get the parent and then again get the child with the specific id , for demonstration
Suppose there is a textview in you layout , and that under the Linear layout where as that linear layout has a relative layout as a root/main layout , and every main layout has different id as you have done above. so this is how you will go to the top(parent ID)
ViewGroup row = (ViewGroup) yourTextView.getParent().getParent;
TextView textView = (TextView) row.findViewById(R.id.tvClassLevels); // the next view you can find
so here I will suggest to do this with each view and then set click listener.
this may not be more efficient but this would work. I have done that once When I was inflating my custom layout but keep that in mind every time inflating though may be quick but it would be costly , you need to implement some logic near to getview (as we have its implementation in adapter)
Why I am doing getParent().getParent() twice
As I said that my textview is in linearlayout and that linear layout lies in the main/root layout and hence as we want to approach the mainlayout because we know it is the only one which has different Id , so we are doing getParent() twice as Textview has LinearLayout as first Parent and then the root layout comes, so in this way if you have a view in another layout , you need to dig it by yourself.
Again I am saying , it may be not a cool or best implementation , but it works . At least it worked for me.
I think the problem is : Different views in your layout have SAME id since you're inflating from the same xml layout.
You have different approaches to solve this:
Keep track of a variable and increment the count whenever you add a Child View. Once the inflation is done, you call setId on the newly created View(just as mentioned in Abdul Salam Ali's answer). Although view ids in android are generated at compile time(correct me if I'm wrong), this should work perfectly in practice.
Create a Ids resource file in res/values folder and pre-define a few Id values for future use. It's guaranteed that all id values will be distinct. But as you say, users can add as many child info as they want, this may not be the best choice.
Use Random to generate different ids.
Please note that even if you have same Ids in your layout, your code logic would be correct as long as the childs of ViewGroup on which you call findViewById(or sth similar to this) has a unique id. See this
For some suggestion to your design.
Use the listview and adapter design instead of generating the view in programming.
a. Add the new item
Used floating action button. https://material.google.com/components/buttons-floating-action-button.html#buttons-floating-action-button-floating-action-button
B. Remove the item
Used some list control or multi selector to remove the undesired items.https://material.google.com/components/lists-controls.html#lists-controls-types-of-list-controls
Use listview to show your item instead of generating layout in your code.
https://material.google.com/components/lists.html#lists-usage
To edit the specific child information, suggest to use the full screen dialog for editing.
https://material.google.com/components/dialogs.html#dialogs-full-screen-dialogs
The above is my suggestions toward your design, since it would be difficult if you want to get the clicked view's parent to find your actual position in the layout, and then send to to listen the corresponding on click event. It would be hard to maintain and your code will be hard to debug if there is any nested view in your parent. Try to separate each part to help your debug what is wrong in your code.
I'm working on an android project where I have a set of views (2 TextViews and some checkboxes in a checkbox group) to replicate many time in the same activity.
Is it possible to define the layout only for one set and instantiate it many time?
Also, the views are grouped in a Relative layout, is it possible to position the without the id attributes (to avoid id duplication)?
I would use a ListView for this. Even if you got like 5 items it workd fine. If you got many more items it still works perfect. Take a look at this example.
You can do this by defining the fields you want to reuse in their own xml. you can then use the 'include' tag for where you want them to display.
http://developer.android.com/training/improving-layouts/reusing-layouts.html
You do need to define the id's to position them in the relative layout. What is your concern about replicating the id's.
The other thing worth mentioning is how to use findById() when using 'include'. You can put a id on the include tag (which is effectively the relative layout viewgroup). Find that group first (Cast to viewgroup) and then do a findbyId on that group for what ever view you are after.
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.