- How to Programmatically Add Views to Views
- 7 Answers 7
- Android — get children inside a View?
- 8 Answers 8
- add Dynamic child views vertically to ConstraintLayout
- Add and Remove Views in Android Dynamically?
- 10 Answers 10
- Layout
- Control
- Other tricks
- Source code
- Android-er
- Friday, November 27, 2015
- Add and Remove view dynamically, keep track of child views
- 6 comments:
How to Programmatically Add Views to Views
Let’s say I have a LinearLayout , and I want to add a View to it, in my program from the Java code. What method is used for this? I’m not asking how it’s done in XML, which I do know, but rather, how can I do something along the lines of this sample code?
Like one can do in Swing.
7 Answers 7
Calling addView is the correct answer, but you need to do a little more than that to get it to work.
If you create a View via a constructor (e.g., Button myButton = new Button(); ), you’ll need to call setLayoutParams on the newly constructed view, passing in an instance of the parent view’s LayoutParams inner class, before you add your newly constructed child to the parent view.
For example, you might have the following code in your onCreate() function assuming your LinearLayout has id R.id.main :
Making sure to set the LayoutParams is important. Every view needs at least a layout_width and a layout_height parameter. Also getting the right inner class is important. I struggled with getting Views added to a TableRow to display properly until I figured out that I wasn’t passing an instance of TableRow.LayoutParams to the child view’s setLayoutParams.
The best way I found is to use the inflate static method of View.
where yourViewXML is something like R.layout.myView
please notice that you need a ViewGroup in order to add a view (which is any layout you can think of)
so as an example lets say you have a fragment which it view already been inflated and you know that the root view is a layout, and you want to add a view to it:
EDIT:
Kotlin code for the example above (view is the getView() of a fragment)
This is late but this may help someone 🙂 🙂 For adding the view programmatically try like
This will create your entire view programmatcally. You can add any number of view as same. Hope this may help. 🙂
LinearLayout is a subclass of ViewGroup, which has a method called addView. The addView method should be what you are after.
The idea of programmatically setting constraints can be tiresome. This solution below will work for any layout whether constraint, linear, etc. Best way would be to set a placeholder i.e. a FrameLayout with proper constraints (or proper placing in other layout such as linear) at position where you would expect the programmatically created view to have.
All you need to do is inflate the view programmatically and it as a child to the FrameLayout by using addChild() method. Then during runtime your view would be inflated and placed in right position. Per Android recommendation, you should add only one childView to FrameLayout .
Here is what your code would look like, supposing you wish to create TextView programmatically at a particular position:
Step 1:
In your layout which would contain the view to be inflated, place a FrameLayout at the correct position and give it an id, say, «container».
Step 2 Create a layout with root element as the view you want to inflate during runtime, call the layout file as «textview.xml» :
BTW, set the layout-params of your frameLayout to wrap_content always else the frame layout will become as big as the parent i.e. the activity i.e the phone screen.
If not set, because a child view of the frame, by default, goes to left-top of the frame layout, hence your view will simply fly to left top of the screen.
Step 3
In your onCreate method, do this :
(Note that setting last parameter of findViewById to null and adding view by calling addView() on container view (frameLayout) is same as simply attaching the inflated view by passing true in 3rd parameter of findViewById() . For more, see this.)
Источник
Android — get children inside a View?
Given a View how can I get the child views inside it?
So I have a custom View and debugger shows that under mChildren there are 7 other views. I need a way to access these views but it doesn’t seem like there is a public API to do this.
EDIT:
My custom view inherits from AdapterView
8 Answers 8
If you not only want to get all direct children but all children’s children and so on, you have to do it recursively:
To use the result you could do something like this:
For example, within an activity / view:
or if you have a reference to a view:
I’m just going to provide this answer as an alternative @IHeartAndroid’s recursive algorithm for discovering all child View s in a view hierarchy. Note that at the time of this writing, the recursive solution is flawed in that it will contains duplicates in its result.
For those who have trouble wrapping their head around recursion, here’s a non-recursive alternative. You get bonus points for realizing this is also a breadth-first search alternative to the depth-first approach of the recursive solution.
A couple of quick tests (nothing formal) suggest this alternative is also faster, although that has most likely to do with the number of new ArrayList instances the other answer creates. Also, results may vary based on how vertical/horizontal the view hierarchy is.
Источник
add Dynamic child views vertically to ConstraintLayout
Android ViewGroup has the following methods to add (child) Views:
I know we can easily define horizontal/vertical orientation to LinearLayout in xml/programatically and add child views, eg.
Similarly with RelativeLayout, we can use ViewGroup ‘s addView and RelativeLayout.LayoutParams ‘s method:
I am also aware that we can use LinearLayout as a child inside ConstraintLayout and play with it.
Is there any solid and recommended way to add child views to ConstraintLayout dynamically?
UPDATE: Let me give a not-so-simple example which I what I want to achieve.
Suppose you have a View 1, View 2 and View 3. All V1, V2 and V3 are aligned vertically one below another and are complex views consisting of multiple TextView s and ImageView s. Based on user action and what server sends information, I need to add multiple V1 and V2 (can be 1 pair of V1-V2 and can be 3 pairs of V1-V2)between original V2 and V3. If I am using ConstraintLayout, would it be best if I add multiple constraints programatically when I can easily use LinearLayout with vertical orientation?
Now, in terms of efficiency, performance and less-and-beautiful-code, is ConstraintLayout best for this requirement as compared to Linear Layout?
Источник
Add and Remove Views in Android Dynamically?
How do I add and remove views such as TextView s from Android app like on the original stock Android contacts screen where you press a small icon on the right side of a field and it adds or deletes a field which consists of a TextView and an editTextView (from what I can see).
Any examples on how to achieve this?
10 Answers 10
ViewParent s in general can’t remove views, but ViewGroup s can. You need to cast your parent to a ViewGroup (if it is a ViewGroup ) to accomplish what you want.
Note that all Layout s are ViewGroup s.
I need the exact same feature described in this question. Here is my solution and source code: https://github.com/laoyang/android-dynamic-views. And you can see the video demo in action here: http://www.youtube.com/watch?v=4HeqyG6FDhQ
Layout
Basically you’ll two xml layout files:
- A horizontal LinearLayout row view with a TextEdit , a Spinner and an ImageButton for deletion.
- A vertical LinearLayout container view with just a Add new button.
Control
In the Java code, you’ll add and remove row views into the container dynamically, using inflate, addView, removeView, etc. There are some visibility control for better UX in the stock Android app. You need add a TextWatcher for the EditText view in each row: when the text is empty you need to hide the Add new button and the delete button. In my code, I wrote a void inflateEditRow(String) helper function for all the logic.
Other tricks
- Set android:animateLayoutChanges=»true» in xml to enable animation
- Use custom transparent background with pressed selector to make the buttons visually the same as the ones in the stock Android app.
Source code
The Java code of the main activity ( This explains all the logic, but quite a few properties are set in xml layout files, please refer to the Github source for complete solution):
Источник
Android-er
For Android development, from beginner to beginner.
Friday, November 27, 2015
Add and Remove view dynamically, keep track of child views
I have a old example of «Add and Remove view dynamically». Somebody ask how the Remove button keep track of the views. So I modify the example to explain in more details.
In this example, when user click on the Add button, it will create a child View, addView. The addView have a Button, buttonRemove. The buttonRemove have its own OnClickListener object. The OnClickListener object refer to the specified addView when it is created.
That means the OnClickListener object of the buttonRemove in the first child view(«abc») not the same object in the second child view(«abcdef»), not the same object in the third child view(«abcdefghi»). All have its own individual OnClickListener object. And each OnClickListener object refer to a specified addView object. Such that it can keep track of the child Views, addView.
MainActivity.java
layout/row.xml, the layout of the child views.
6 comments:
Hey i am just taking reference your code but how i will compare the text of your child view because if its will be same no problem but if its different then i need to replace the text with top parent to nearest child. Is it possible i am using AutocompleteTextview and Assign the same Text to the new View ??
How you compare strings?
In Java, to compare strings, using StringA.equal(StringB), NOT (StringA==StringB).
Yes exactly i am doing same but like in your row.xml i have taken a Autocompletetextview which is inflating There is no issue regarding the add view but when i remove and specific view from Number 1 to 10 suppose i remove view number 5 so the text in 4 and 6 should be same so i need to save the selected content to some list but in view how can i do that ?? i stuck only when i remove a particular specially not from bottom ??
Sorry, I don’t understand your problem.
I tried to change the TextView to AutoCompleteTextView, and found no any problem. Please read Add and Remove view dynamically, with EditText/AutoCompleteTextView inside.
how to get each edit text value which will we add can you put code with example like one more button is submit then when click on it i get all text from all edittext and set in one textview.
i am doing a task in which i will show spinner dynamically with add button event. now on each click event of spinner of dynamic spinner i have some value related to selected item of spinners. but the issue is when i have multiple spinners and selects item from lets say 2nd spinner item the related value of 2nd spinner should be changed but it is changing the last displayed textview value which is related to last spinner.
like i have
1) spinner with textview(dynamic)
2)on each event of add button spinner and textview will displays
3)now lets say i will click button for 4 times which displays(4 spinners and textviews)
4)the process should be select an item of 1st spinner the related data should be display on its textview(related data on textview will come from db)
5)the process should continue many times
6)Now the issue is when i select 2nd spinner item or 3rd spinner item its related data must display on textview 2nd and textview 3rd. but when i selected 2nd or 3rd spinner item the related data is displaying on 4th spinners texview
Источник