Greets. I’m working on a project and thought I would roll up a composite component. I laid them out in xml and wrote up a new class that extends LinearLayout. Basically, I will need about five or six of them in an Activity. I want to create these inside of an Activity programmatically because I don’t know how many I will need before runtime. Also, I created an xml file to set them up which I haven’t included below. I am having a problem with each of the methods I have found to do this.
Here are the method segments inside of the class I made that extends LinearLayout:
public ExtendedLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
...
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
((Activity)getContext()).getLayoutInflater().inflate(R.layout.extended_linear_layout, this);
...
}
And inside the Activity, these are the two solutions that appear to be the closest to what I’m looking for.
Don’t know where to get the attrs from using this method:
ExtendedLinearLayout customViewClass = new ExtendedLinearLayout(this, attrs);
layout.addView(customViewClass)
Dont know how to specify that the inflater should use the ExtendedLinearLayout with this method:
LayoutInflater inflater = LayoutInflater.from(this);
inflater.inflate(R.layout.extended_linear_layout, parent_layout);
I will probably settle for using a ListView – but it seems a bit overkill for five to six items. Also, having worked with Flex before, this at least seems like a reasonable solution to the problem.
Thanks,
Randall
—————-UPDATE——————-
I’ve tried the method as discussed below. Once I set everything up I’m getting an InflationException: Binary XML file line #8: Error inflating class. I feel like the xml looks just fine. One thing I may be confused about is where to call the inflater. Right now, I’m calling the inflater in onFinishInflate() of the custom class as well as in the Activity as follows:
public class ExtendedLinearLayout extends LinearLayout {
...
public ExtendedLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.extended_linear_layout, null);
...
}
}
And in the activity:
public class TestActivity extends Activity {
LinearLayout list;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list = (LinearLayout) findViewById(R.id.simple_list);
runTestCode();
}
private void runTestCode() {
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ExtendedLinearLayout layoutOne =
(ExtendedLinearLayout) inflater.inflate(R.layout.extended_linear_layout, list);
}
}
Thanks and hope you’ve got time to take a look. I like to figure out stuff by digging around online but this one has had me cooked for a number of hours since yesterday morning. I’m about 2 inches away from just refactoring this into a ListAdapter.
—————-UPDATE——————-
Here is the xml file
<?xml version="1.0" encoding="utf-8"?>
<com.anotherandroidblog.ExtendedLinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
>
<TextView
android:id="@+id/text_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
></TextView>
<TextView
android:id="@+id/text_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
></TextView>
<TextView
android:id="@+id/text_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
></TextView>
</com.anotherandroidblog.ExtendedLinearLayout>
You need to use the full class name in the XML file: instead of making the root element
LinearLayoutmake it the full class name forExtendedLinearLayout(including package name). Then theLayoutInflaterwill know what to do.EDIT: also, the
attrsoption isn’t really workable (or at least I don’t think so). I haven’t found a way to generate at runtime the actuallyattrsobject instance: there are no public constructors and I expect that’s another one of the things the framework does under the hood that’s just not accessible.