Dennis
Dennis

Reputation: 333

Android: Override own XML View in XML

I am looking for a way to override my own XML view in android. So I only have to specify settings (like background, border, default content) once - and use it again.

The problem is that I can't find anything about it. (The things I found are about using a java class in XML to override, this is not what I want)

This is some non-working code, but I hope it explains it well enough to you:

main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <field android:text="@string/str1" />
    <field android:text="@string/str2" />
    <field android:text="@string/str3" />
    <field android:text="@string/str4" />
    <field android:text="@string/str5" />
</LinearLayout>

code representing field (field.xml):
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/field"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@layout/field_background"
    android:text="@string/default_string"/>

As you see field itself is once defined and used many times with one customization. (Another string as text set)

This doesn't work at all, but I hope someone knows a way to make something like this work. (Without coding, just XML)

Thanks, Dennis

Upvotes: 1

Views: 1357

Answers (3)

Ted Hopp
Ted Hopp

Reputation: 234797

I'd suggest that you set up a style. In some file (usually styles.xml) in res/values, add a style for your field:

<style name="DefaultTextFieldStyle">
    <item name="android:id">@+id/field</item>
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:background">@layout/field_background</item>
</style>

Then in main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <TextView style="@style/DefaultTextFieldStyle"
        android:text="@string/str1" />
    <TextView style="@style/DefaultTextFieldStyle"
        android:text="@string/str2" />
    <TextView style="@style/DefaultTextFieldStyle"
        android:text="@string/str3" />
    <TextView style="@style/DefaultTextFieldStyle"
        android:text="@string/str4" />
    <TextView style="@style/DefaultTextFieldStyle"
        android:text="@string/str5" />
</LinearLayout>

I just copied your values, but it's wrong to have the same android:id value for several views in a layout and it's weird to have a layout as a background.

Upvotes: 2

Shine
Shine

Reputation: 3818

No need for custom components here. If I understood your question, styles should be enough to do what are you looking for.

Just define A serie of TextViews inside the LinearLayout, and let them inheritate the common parts (layout_width, layout_height, etc.) from a Style you can define as described here.

The only changing part will be android:text, as required

Upvotes: 0

wojciii
wojciii

Reputation: 4325

I don't know how to do what you want without code .. I would define my own custom component. Also see Different formats for different Textview defined by Custom style which shows how to style TextViews which could be what you want.

Upvotes: 0

Related Questions