1

I'm writing an Android app in which I have this piece of code that creates a way for users to insert an amount of money. It includes setting the currency symbol and decimal mark on the basis of their location, and jumping focus from the integer textbox to the decimal textbox when the user hits the dot or comma.

Since I want to recreate this piece of code in several places in our app I want to keep it separate from the rest of a view. So I created a separate xml file and inflate it in a separate java file in which I keep the methods for the setting of the currency etc. I then "import" it in an xml file like this:

<com.example.android.ui.widget.AmountWidget 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
</com.example.android.ui.widget.AmountWidget>

The problem now occurs when I need to use this code several times in one page. Since the TextEdit boxes have an id, this id is reused if I use this code several times, and it effectively becomes invalid.

So my question then becomes; how can I reuse a piece of code in a way that I can set the id's for it everytime I invoke this piece of code?

1
  • Did you added include technique on your code? If yes how is it?
    – JJ86
    Oct 3, 2013 at 16:52

1 Answer 1

2

Use <include/> on your xml code. Here there is an official example.

EDIT

This link provides more info, in order to override the id of the included layout.

EDIT 2

After your last comment, even if I don't know the full structure of your project, here is my idea: you can override the id of the Layout on XML file, and in the code you can check the parent of the EditText with a switch case.

...
<include layout="@layout/your_layout"
         android:id="@id/layout1"/>
<include layout="@layout/your_layout"
         android:id="@id/layout2"/>
<include layout="@layout/your_layout"
         android:id="@id/layout3"/>
...
<include layout="@layout/your_layout"
         android:id="@id/layoutX"/>
...

Then in your code, after you point to the EditText use a case/switch.

By the way, if you have only a few lines you can avoid to use include; instead if you have a lot of lines, use a ListView.

7
  • Thanks for the tip. I have two questions about that though. 1) I see I can override the id, but as far as I can see I can only override the id of the part that you include, and not of the individual EditText boxes which are included in the part you include. and question 2) if I include the xml, does it also include the java code that comes along with it (which sets the currency symbol for example)?
    – kramer65
    Oct 3, 2013 at 15:18
  • And one more thing: what about Fragments, would that be a better idea in this case (don't have any experience with fragments)?
    – kramer65
    Oct 3, 2013 at 15:18
  • @kramer65 1) when you use include, everything of the layout is included; 2) no, of course the java code is various because it's up to you to manage the layout in your class; all the configuration you do on xml file is of course imported; FRAGMENT) you can use include on Fragment, but you don't to have to include a Fragment xml on a layout, this is not necessary!
    – JJ86
    Oct 3, 2013 at 15:33
  • Alright, thanks for that. But my main question; how can I override the id of the xml so that I can use the same xml several times within one page and still distinguiess between them by id.
    – kramer65
    Oct 4, 2013 at 7:39
  • @kramer65 we where so close to the answer, check this: developer.android.com/guide/topics/resources/… .
    – JJ86
    Oct 4, 2013 at 8:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.