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

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.

Related

How can I create Views in xml which I can use for any layout?

When creating an ImageView for example, I know I can create it within a Layout:
Example a LinearLayout:
<LinearLayout...>
<ImageView
android:id="#id/hello_world_id"/>
</LinearLayout>
But can I define an View outside a layout, and then add it to any other layout?
I want to have a RelativeLayout which adds/removes views programmatically and dynamically, so that the RelativeLayout starts off with no views inside it, then I add some, remove some etc. Would there be any way to do this? Or is it better just to have these views already inside some other Layout, and then I add the Layout (whatever it is--containing my view(s)) to my RelativeLayout?
What you are searching for is LayoutInflator
Create a xml file - buttons.xml
<LinearLayout...>
<ImageView
android:id="#id/hello_world_id"/>
In your activity access it by
final LayoutInflater inflater
= (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.buttons,
null);// this is a layout in your master activity
lLayout = (LinearLayout)findViewById(R.id.layout1);
lLayout.addView(itemView);
Hope this helps.

Can't correct add child to LinearLayout

I've a simple task - add child views to a LinearLayout dynamically, but I don't know, how to do it in a proper way.
There are two ways:
1. Inflate view and passed parent container to the inflate method.
View view = LayoutInflater.from(getActivity()).inflate(R.layout.item, container, true);
At this case, when I've more than one child, LayoutInfater returns the same child view object every time, when inflate it. So I can proper initialized other child views.
2. The next way is to use LinearLayout method addView(View view).
The problem is, that the child view lose it's LayoutParams state.
And I must set new LayoutParams for child programmatically. It's a not good practice.
Also we can put child in the complimentary wrapper layout in his own layout resource file. But it's also a not good practice put layout to the wrapper.
My child resource:
<RelativeLayout android:gravity="center_vertical"
android:paddingLeft="25dp"
android:layout_width="match_parent"
android:layout_height="#dimen/list_item_height">
<TextView android:id="#+id/tv_name"
android:text="Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
So what is the proper way to add child to LinearLayout, without adding LayoutParams programmatically and without wrapper layout?
Thank's a lot for help!
Did you try this way?
View view= LayoutInflater.from(getActivity()).inflate(R.layout.item, null, false);
parentView.addView(view);
if you want to add child to a RelativeLayout,you need to set LayoutParams for child view ,and if you want to add child to a LinerLayout,you don't need to set LayoutParams,so if you want to custom layoutParams,i suggest you to use RelativeLayout,or you can make a wrapper view for child View ,just like:
//inflate a wrapper view
View child = LayoutInflater.from(getActivity()).inflate(R.layout.item, container, false);
//add to a linerLayout
LinerLayout.addChild(child)
Try this way,
View view = LayoutInflater.from(getActivity()).inflate(R.layout.item, **container**, false);
layout.addView(view);

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" />

Android: Adding a linearLayout in a LinearLayout from a separate xml file

I got a relative layout which contains another relative layout which I used to replace the "title", a linearLayout which I will use later as a "control panel", and a horizontalScrollView, where the horizontalScrollView contains a LinearLayout(let's name this linear layout - "hsc".
I also have another xml layout file named "entries" that contains an imageView.
My question is, how do i attach "entries" inside "hsc"? Or how to I populate "hsc" with multiple "entries"?
My main layout's structure looks something like this:
<RelativeLayout>
<relativeLayout1>
<linearLayout>
<horizontalScrollView1>
<hsc>
Thanks!
Try to use LayoutInflater. First get the hsc in code somehow like this
LinearLayout layout = (LinearLayout) findViewById(R.id.hsc_id);
Then you make new entrie
LayoutInflater inflater = (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
View entrie = inflater.inflate(R.layout.entries,
null, false);
and put one into another
layout.addView(entrie);
you can add multiple views by repeating child view creation process.
If your are going to populate the view, you may want to use a ListView (FragmentList or ListActivity).
In which case you use the tag
<RelativeLayout>
<relativeLayout1>
<linearLayout>
<horizontalScrollView1>
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
And then load the list with an Adapter.
http://www.vogella.com/articles/AndroidListView/
Your question seem to imply that you some-how need to traverse the xml tree, you don't, you just use android:id to find the resource from the code side.
create a linear layout dynamically and add some view in that.....
LinearLayout layoutContainer=new LinearLayout(your_activity.this); //create a linear layout dynamically
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.gravity = Gravity.RIGHT;
layoutContainer.setLayoutParams(lp);//apply attributes to your linear layout
View viewOther = LayoutInflater.from(your_activity.this)
.inflate(R.layout.layout_to_add, layoutContainer);//add some view to your linear_layout.
hope it helps....!

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

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);

Categories

Resources