Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android- how to programmatically wedge a view between views in layout pre-loaded from the XML?

I'm developing an android app with fragments. While most of my layouts are pre-determined in the XML, I would like to programmatically insert a new view between views that were already loaded in a LinearLayout at startup.

enter image description here

How do I go about with this?

Thanks

like image 433
LoneDuck Avatar asked Sep 18 '12 22:09

LoneDuck


People also ask

What is the use of Layout_weight in android?

LinearLayout also supports assigning a weight to individual children with the android:layout_weight attribute. This attribute assigns an "importance" value to a view in terms of how much space it should occupy on the screen. A larger weight value allows it to expand to fill any remaining space in the parent view.

What is merge in XML?

The Merge Tag intention allows you to merge two sequential and compatible XML tags into one larger tag. The tags to merge must have the same name and attributes.


1 Answers

Its possible to specify index while u dynamically add a view to a LinearLayout.

Set height of the first view as

android:layout_height="0dp"
android:layout_weight="1"

Set height = wrap_content for the second view in XML

Then while u are adding new View dynamically, set its height = wrap_content and add it to the parent LinearLayout like this

 parentLinearLayout.addView(childView, index);     
                 //index = position where you want to insert the new view.

It might help you. :)

like image 86
Rahmathullah M Avatar answered Oct 24 '22 20:10

Rahmathullah M