Kenn de Leon
Kenn de Leon

Reputation: 27

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!

Upvotes: 1

Views: 1333

Answers (3)

Gagan
Gagan

Reputation: 755

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....!

Upvotes: 0

Maxim Efimov
Maxim Efimov

Reputation: 2737

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.

Upvotes: 4

jeremyvillalobos
jeremyvillalobos

Reputation: 1815

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.

Upvotes: 0

Related Questions