views:

331

answers:

2

I have an XML file in my layout folder that has how i want my custom widget/view (not sure what correct terminology is here).

but how do i make it so that i can programatically, add one or more to an activity

the xml file is as follows

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:id="@+id/viewLog">
  <TextView android:id="@+id/viewLogClimbName"
            android:layout_width="fill_parent"
            android:singleLine="true"
            android:ellipsize="end"
            android:gravity="left"
  />
  <LinearLayout android:orientation="horizontal"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1">
    <LinearLayout android:orientation="vertical"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content">
      <TextView android:id="@+id/viewLogDate"
                android:layout_width="fill_parent"
                android:singleLine="true"
                android:ellipsize="end"
                android:gravity="left"
      />
      <TextView android:id="@+id/viewLogStyle"
                android:layout_width="fill_parent"
                android:singleLine="true"
                android:ellipsize="end"
                android:gravity="left"
      />
    </LinearLayout>
    <TextView android:id="@+id/viewLogDetails"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:ellipsize="end"
              android:gravity="left"
              android:layout_weight="1"
    />
  </LinearLayout>
</LinearLayout>

I have looked at the android how-to and dont really understand what its getting at

A: 

Say your layout was named res/some_layout.xml

  LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  View newView = inflater.inflate(R.layout.some_layout, null);

Then add() your newView to some other view, say a LinearLayout from your main layout, retrieved with findViewById().

Jim Blackler
how would I then get hold of an element of that, to set the value of one of the embedded text views?
DrogoNevets
also just had ago at seeing if i could figure it outthe 2nd line of you're response seems to kill it (if i comment it out it works)I changed it accordingly toView newView = inflater.inflate(R.id.viewLog, null);which as you can see is the id of my LinearLayoutor do i want the R.layout?
DrogoNevets
which i have now tried and it failed...?
DrogoNevets
To get ahold of the internal views use newView.findViewById().What error are you getting from the inflator?
jqpubliq
A: 
LinearLayout root = (LinearLayout) findViewById(R.id.my_root_viewgroup);
View newView = View.inflate(this, R.layout.my_layout_file, null);
root.addView(newView);

You can cast newView if needed, and you can locate views within that inflated view using newView.findViewById(R.id.my_other_child_view)

Joe
ok I have the following codehttp://pastebin.com/GtwdAUpFbut it still crashes (I get the dialog up in the emulator saying is not responding and to force close) have i mis understood something?
DrogoNevets
Based on that code, it seems like you should consider using a ListView with an Adapter, instead? You definitely don't want to add thousands of views, if `logs` contains that many entries.You can extend `ArrayAdapter<?>` to get the functionality you need. ListView is used for doing exactly this type of list behavior.
Joe