Android Views - How to Configure a "Loose" Piece of View Element Through XML?

This is what i'm trying to achieve:

  1. Use a TextView in a TabWidget (i.e. call TabHost.TabSpec.setIndicator(View) instead of TabHost.TabSpec.setIndicator(String)).

  2. And i want to configure that TextView in XML instead of building it programatically.

So initially, in my layout XML, i had:

<TabHost
    android:id="@+id/mainTabHost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="30px"/>
    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingTop="30px">
    </FrameLayout>
</TabHost>

Now i want to put in the TextView configuration somewhere. It does not actually "belong" nested anywhere within the layout hierarchy. But, since the layout XML have to be a valid XML of course, it can't have more than one root element. So i'm forced to nest the TextView somewhere. Hence i just put it one level below the root (TabHost):

<TabHost
    android:id="@+id/mainTabHost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="30px"/>
    <TextView
        android:id="@+id/tvTabIndicatorPreferences"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="@string/tab_name_preferences"/>
    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingTop="30px">
    </FrameLayout>
</TabHost>

And in my code i do:

final TabHost mainTabHost = (TabHost) this.findViewById(R.id.mainTabHost);
mainTabHost.setup();
final TextView tvTabIndicatorPreferences = (TextView) this.findViewById(R.id.tvTabIndicatorPreferences);
final TabHost.TabSpec tabSpecPreferences = mainTabHost.newTabSpec("tabPreferences");
tabSpecPreferences.setIndicator(tvTabIndicatorPreferences);
tabSpecPreferences.setContent(R.id.scrlTblPreferences);
mainTabHost.addTab(tabSpecPreferences);

but there's a problem here. When i try to run the application, i get an exception which says

Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

which is by all means logical and reasonable. The TextView is already a child of the TabHost. i can't just set it as the child of another element. (Note: Putting the TextView nested under the TabWidget yields the same thing.)

so i'm forced to call mainTabHost.removeView(tvTabIndicatorPreferences) before calling the setIndicator.

It works actually, but this way of doing feels very wrong to me. Firstly, i have to put the TextView in the XML where it didn't belong, then in the code remove it from where it didn't belong before using it.

In such a scenario, what's the Right Way to achieve this?


Asked by: Kelvin664 | Posted: 20-01-2022






Answer 1

Step #1: Pull the TextView out into a separate layout file (e.g., res/layout/indicator.xml).

Step #2: Call tabSpecPreferences.setIndicator(getLayoutInflater().inflate(R.layout.indicator, null));

Step #3: There is no Step #3.

Answered by: Lucas847 | Posted: 21-02-2022



Similar questions

android - How can I configure how the header of an alert dialog

Is there a way to configure how the header of an alert dialog looks? It nows has an icon (on the left) with text as title. Is there a way to add view on the same line? Thank you.


android - How can I configure how the header of an alert dialog

Is there a way to configure how the header of an alert dialog looks? It nows has an icon (on the left) with text as title. Is there a way to add view on the same line? Thank you.


android - Configure ambient media player source code in my eclipse?

Im trying to see working of ambient media player. I checkout from url https://ambientmp.svn.sourceforge.net/svnroot/ambientmp/Ambient/trunk to my eclipse work space. when build the app it is showing errors as Unbound classpath variable: 'M2_REPO/hotsax/hotsax/0.1/hotsax-0.1.jar' in project '...


android - How can I configure the color of the text in a dialog

In android selection dialog, like this example, http://labs.makemachine.net/2010/03/android-multi-selection-dialogs/ How can I specific the color of the text in the dialog? e.g. the color of the text "Mars", "Jupiter"?


Can't Configure Android SDK in Eclipse in Ubuntu After Upgraded to 10.10

I have installed eclipse 5.2 in ubuntu 10.10. In the 'Install New Software', it show Android Developments Installed. And when i goto preferences window, Android is not listed in the left panel. Already i was running Android SDK in ubuntu 10.04, after i upgraded ubuntu OS, eclipse is not showing the Android.


Is it possible to configure an Android install to run a single app?

Is it possible to configure the Android OS to run only a single app? Basically what I want to do is customize an Android device so that it boots up and runs one application only, and for that application to be switched to the front of the screen automatically. Also, when it gets closed, to be started up and switched to again. Any ideas? Thanks, -David


android - Help me configure OpenGL for 2D

I'm writing my first 2D app for Android using OpenGL. I'm writing it on my Desire, so my screen coords should be 0,0 to 799,479 in landscape mode. I'm trying to get OpenGL to use this range in world coordinates. The app, such as it is, is working fine so far, but I've had to tweak numbers to get stuff to appear on the screen and I'm frustrated by my inability to understand the relationship between the projecti...


java - CRest / How to configure to accecpt only JSON responce

I am using CRest to deserialize a JSON stream on Android. My first steps looks very promising. To get the JSON stream from the server and not the XML one I use the following construct: (Accept: application/json) @EndPoint("http://localhost:8080/myserver/rest") @Param(name = "Accept", value = "application/json", dest = Destination.HEADER) public interface VersionService { @Conne...


How to configure Android in Eclipse

can any one help me how to configure Android in Eclipse


java - How can I configure a SAX parser in the Sun JVM to match the Android one?

Is there a way to configure features/properties on the SAX parser so that it matches the default Android one? I have implemented a SAX parser for an Atom feed and I'd like to be able to unit test it without running it via an InstrumentationTestCase. The differences I see right away is that in startElement(), localName has the element name when running on Android, yet the "name" method parameter is populated when r...


memory leaks - How to configure an Android Virtual Device to work on my home computer

My computer confguration is: Processor: Interl Celeron 1.8 MHz RAM: 1 GB When I've run Android emulator it stuck in a cycle, because of lack of memory. How to setup it to run on my computer? Is there any chance? intepenit Thanks.






Still can't find your answer? Check out these communities...



Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android



top