1

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

Dennis
  • 323
  • 7
  • 17

3 Answers3

2

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.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • I don't think the compiler'll let you define `android:id` inside a Style, anyway the idea is correct and I agree with it. I would also add style's parent, so that 'common' TextField behaviour is garanteed. – Shine Aug 01 '12 at 15:32
  • @Shine - I just tried defining a style containing an `@+id/foo` and both the compiler and Android Lint were perfectly happy with it. I didn't test whether the id value would actually be applied to a view, but I don't see why it wouldn't. – Ted Hopp Aug 01 '12 at 15:38
  • Because, as you said, it's wrong to have the same android:id value for several views..I think it's a "conceptual" error but nevermind :) – Shine Aug 01 '12 at 15:45
  • Styles did the job. :) The android:id I set in the "parent" TextView was because I thought the children nodes should identify it with that. (But apparently not) However, many thanks for this solution. :) – Dennis Aug 01 '12 at 16:42
0

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.

Community
  • 1
  • 1
wojciii
  • 4,253
  • 1
  • 30
  • 39
0

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

Shine
  • 3,788
  • 1
  • 36
  • 59