Can I add views to a scrollview without inflating/re-inflating layouts? - android

I would like to take an existing ScrollView with a view in it, and add more views, dynamically (at runtime) to the ScrollView container.
Is it possible to add these views without having to create a new layout and inflate it? If so, what's the general process for adding these views dynamically?
For the sake of this question, assume the views are TextView...
Thanks!

A ScrollView can only have one child, so it doesn't make sense to add more children to it directly. Lets say your ScrollView has a LinearLayout inside of it, then you can add more views to the LinearLayout:
LinearLayout layout = findViewById(R.id.my_linear_layout);
TextView textView = new TextView(this);
layout.addView(textView);

Related

Different layoutparams for different views

Is it possible to set one LayoutParams to RelativeLayout(it is inside another Layout) and set another ones to a view that is in this Layout.
RelativeLayout inside which another Relative Layout with some LayoutParams and inside 2nd Reltive layout a view with another LayoutParams
Is situation like possible?
try by,
1.giving id to each views you wanna resize
2.initialising those ids to a member variable
3.setting the layoutparams using setLayoutParams(left,top,right,bottom) method.

Creating a layout Multiple times in Android

How to create a whole layout (Relative/Linear) multiple times in Android? I want the same layout to be created multiple times inside a horizontal scroll view.
You can use RecyclerView for Horizontal scrolling-
or-
Take horizontal scrollview reference in java code by findViewById.
Create one other xml for view which you want to display multiple
time.
inflate that view by getlayoutinflator. Create a loop in
view.
create a linearlayout at runtime and add those view to it by add
view
and add linearlayout to horizontal scroll view. by addview()
take a idea and modify the below code
scrollview = findViewByID(scrollview);
LinearLayout ll = new LinearLayout(this);
for(your loop){
View v= getLayoutInflator().inflate(R.layout.xml);
ll.addView(v);
}
scrollview.addView(ll);
Either you need to add inflated child views to the root view like below
RelativeLayout rootView = (RelativeLayout)findViewById(R.id.rootView);
View child = getLayoutInflater().inflate(R.layout.child, null);
rootView.addView(child);
OR you can define and include that layout multiple times inside other.
Check this link http://developer.android.com/training/improving-layouts/reusing-layouts.html
Include your reusable layout like this
<include
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="#layout/reusabelLayout" />

edit layout of existing child in RelativeLayout

I have a RelativeLayout with a number of children. The RelativeLayout is the main layout of my activity. I need to add to one of the children the layout value layout_alignParentBottom="true". I need to do this programmatically. the link How to set the android property layout_alignParentBottom to a button dynamically is not working for me because, basically, I don't want to have to inflate the RelativeLayout as I already have it at the setContentView(...). Any ideas how to do this?
I figured it out:
RelativeLayout.LayoutParams layout = (LayoutParams) theChild.getLayoutParams();
layout.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

Add CustomViews to a LayoutView dynamically

I have a LayoutView in a ScrollView named MyLayout and I want to add a View composed by LayoutView with TextView and EdiText inside this ScrollView according a value,
for example with a method
addMyCustomViewToMyLayout(int x){
....
}
that adds the desired number of elements to the MyLayout in the ScrollView,
The CustomViews added to the ScrollView should be linked to the activity to get and set the value from-to the app.
Is possible?
How could I do?
There's nothing very special about ScrollViews. You can think of them as a boundless View. While normal Layouts like a LinearLayout are bound to an area, then ScrollView will allow its child to be whatever height it wants to be (or width in the case of a HorizontalScrollView).
ScrollViews contain one child. The child size is bound by the width of the ScrollView, but it is not bound by the height. Thus, the child will measure itself based on the idea that it has enough vertical space to show everything.
In this case, you create or inflate your MyLayout however you want. Then you add it to the ScrollView. If you want to add other Views to it, you add it to your MyLayout layout view. Just let the ScrollView do its thing.

How to merge the three views into a blank view and display?

I want to set a ScrollView for some views. I need to merge some view into one view and add to ScrollView.
The three views are listView, custorm view and Gallery.
Who knows how to merge the three views into a blank view and display?
In XML you can structure your app like the following snippet:
<ScrollView>
<LinearLayout>
<CustomView/>
<ListView/>
<Gallery/>
</LinearLayout>
</ScrollView>
Alternatively in code, you can do it like so:
ScrollView SV = (ScrollView) findViewById(R.id.my_scrollview);
LinearLayout LL = (LinearLayout) findViewById(R.id.my_linear_layout);
LL.addView(my_customView)
LL.addView(my_istView)
LL.addView(my_gallery)
SV.addView(LL);
hope that gives you some pointers in how to achieve what you'd like to
You can create those objects and assign them inflated elements. I mean, if the listView is in listView.xml you can inflate it with getLayoutInflater().inflate(R.layout.listView,null) and put in a ListView object. Then you can add it to a ViewGroup.
Inflate the ScrollView, you can create programmatically too. ScrollView scroll=new ScrollView(this). Don't forget adding LayoutParams to the view if you do it programmatically.
ViewGroup scrollView = (ViewGroup) getLayoutInflater().inflate(R.layout.scrollView, null);
Then you inflate or create the other views the same way. Or you can get it from an activity with findViewById(R.id.idOfView1).
Finally just add the views you've created, inflated... with scrollView.addView(View child).
You need a ViewGroup to add views as childs.

Categories

Resources