Android custom widget xml

I have an XML file in my layout folder where my custom widget / view is (not sure what is the correct terminology here).

but how can i do it so that i can programmatically add one or more to the activity

xml file looks like this

<?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 looked at android instruction and don't understand what this means:

+2


a source to share


2 answers


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 use newView if needed and you can find views in this enlarged view using newView.findViewById(R.id.my_other_child_view)

+3


a source


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 into another view, say LinearLayout

from your main layout, retrieved with findViewById()

.

0


a source







All Articles