I’m new to android and programming in general so go easy!
I have created a – probably overcomplicated but it does what I want – main.xml with my layout which goes something like this:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_scrollview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<LinearLayout
...
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/title"
android:gravity="center"
/>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:stretchColumns="0"
>
<TableRow>
<TextView/>
<Spinner/>
</TableRow>
<TableRow>
<TextView/>
<Spinner/>
</TableRow>
</TableLayout>
<TableLayout
android:id="@+id/main_tablelayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:stretchColumns="*"
>
<include
android:id="@+id/include"
layout="@layout/price0"
/>
<TableRow>
<TextView/>
<EditText/>
</TableRow>
</TableLayout>
</LinearLayout>
</ScrollView>
This is one of the options that I’m trying to include (price1.xml):
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="@string/price1"
android:gravity="center_vertical"
/>
<EditText
android:id="@+id/price1"
/>
</TableRow>
</merge>
I am trying to dynamically change the include depending on what value one of the spinners has, but I get a force close on starting the app.
The logcat error is:
01-18 15:31:42.826: ERROR/AndroidRuntime(719): FATAL EXCEPTION: main
01-18 15:31:42.826: ERROR/AndroidRuntime(719): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
01-18 15:31:42.826: ERROR/AndroidRuntime(719): at android.view.ViewGroup.addViewInner(ViewGroup.java:1976)
01-18 15:31:42.826: ERROR/AndroidRuntime(719): at android.view.ViewGroup.addView(ViewGroup.java:1871)
01-18 15:31:42.826: ERROR/AndroidRuntime(719): at android.widget.TableLayout.addView(TableLayout.java:421)
01-18 15:31:42.826: ERROR/AndroidRuntime(719): at android.view.ViewGroup.addView(ViewGroup.java:1828)
01-18 15:31:42.826: ERROR/AndroidRuntime(719): at android.widget.TableLayout.addView(TableLayout.java:403)
01-18 15:31:42.826: ERROR/AndroidRuntime(719): at android.view.ViewGroup.addView(ViewGroup.java:1808)
01-18 15:31:42.826: ERROR/AndroidRuntime(719): at android.widget.TableLayout.addView(TableLayout.java:394)
01-18 15:31:42.826: ERROR/AndroidRuntime(719): at com.kavi.eta.Calculate$1.onItemSelected(Calculate.java:36)
01-18 15:31:42.826: ERROR/AndroidRuntime(719): at android.widget.AdapterView.fireOnSelected(AdapterView.java:871)
01-18 15:31:42.826: ERROR/AndroidRuntime(719): at android.widget.AdapterView.access$200(AdapterView.java:42)
01-18 15:31:42.826: ERROR/AndroidRuntime(719): at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:837)
01-18 15:31:42.826: ERROR/AndroidRuntime(719): at android.os.Handler.handleCallback(Handler.java:587)
01-18 15:31:42.826: ERROR/AndroidRuntime(719): at android.os.Handler.dispatchMessage(Handler.java:92)
01-18 15:31:42.826: ERROR/AndroidRuntime(719): at android.os.Looper.loop(Looper.java:123)
01-18 15:31:42.826: ERROR/AndroidRuntime(719): at android.app.ActivityThread.main(ActivityThread.java:3647)
01-18 15:31:42.826: ERROR/AndroidRuntime(719): at java.lang.reflect.Method.invokeNative(Native Method)
01-18 15:31:42.826: ERROR/AndroidRuntime(719): at java.lang.reflect.Method.invoke(Method.java:507)
01-18 15:31:42.826: ERROR/AndroidRuntime(719): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-18 15:31:42.826: ERROR/AndroidRuntime(719): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-18 15:31:42.826: ERROR/AndroidRuntime(719): at dalvik.system.NativeStart.main(Native Method)
Looks like I’m doing the java code wrong but I can’t figure out how to properly remove the default include and replace it with another layout:
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) {
int index = formatSpinner.getSelectedItemPosition();
if (index == 0) {
TableLayout tl = (TableLayout)findViewById(R.id.main_tablelayout2);
tl.removeView(findViewById(R.id.include));
LayoutInflater inflater = getLayoutInflater();
tl.addView(inflater.inflate(R.layout.price1, tl, true));
}
Sorry about the long post but I’ve searched everywhere and can’t find a clear answer. Hopefully this’ll at least provide a decent-ish example…
Didn’t really find a proper solution to this, but for completeness, this is what I ended up doing:
I kept getting a NullPointerException when using findViewById for any of the views in the price1.xml (ie The xml being included). Is this by design?
This was a problem as I had to dynamically add/remove certain views depending on the user input.
I still have an include tag in my main.xml, which I remove by looking up the position relative to it’s parent (again, I couldn’t get the findViewById to work but this just as effetive). I then inflate another xml file in it’s place, depending on the user input.
I have also cleaned up my main.xml to remove unnecessary nesting of layouts and ditched the merge tags and used TableLayout as the root tags for my included xml.
That seems to be my UI troubles sorted (for now!)…