3

I have this situation: an abstract activity and a series of activities that extend this one. The difference from parent activity is just a piece of layout, and some methods, how can I do with the layout? I declare 'n' very similar layout ? or I can only declare one and then extend only to the extent that change? and in this case, how?

I give you an example.

In MyAbstractActivity i call setContentView(R.layout.abstract_layout), where abstract_layout.xml has this layout:

<LinearLayout>
    <Button id=1/>
    <Button id=2/>
    <Button id=3/>
    <Button id=4/>
</LinearLayout>

Now in MyConcreteActivity that extends MyAbstractActivity suppose that we want a textfield below Button with id=2, how we can do?

Thanks in advance!

EDIT: Solved using ViewStub in superclass and changing it in subclasses.

ech0s7r
  • 337
  • 2
  • 14

1 Answers1

1

You would need a separate layout for each of your subclasses. Then in your subclass onCreate you could do:

setContentView(R.layout.concrete_layout_1);
super.onCreate(savedInstanceState);

And avoid calling setContentView in your abstract activity. Depending on the code you currently have in MyAbstractActivity#onCreate, you may have some refactoring to do.

In the end there isn't really a concept of inheritance for layouts, although you can do some creative things with <include ...>; you might check Creating Reusable UI Components if you care about DRY.

Matthew
  • 44,826
  • 10
  • 98
  • 87
  • i solved declarin ViewStub in superclass and changing it in subclass – ech0s7r Apr 27 '11 at 20:31
  • Hi Matthew. Just wondering why should we avoid calling `setContentView` on abstract activities. I was about to do just that when I found this question... – rfgamaral Oct 25 '11 at 00:17
  • Here is an example of how to set the content views: http://stackoverflow.com/a/8821459/3681880 – Suragch Dec 30 '14 at 22:38