0

I am trying to do inheritance between activities in my application.
I want to create "general activity" style for my all application.

For example, this "general activity" includes toolbar and statusbar. I want to show both in each activity in my program.

I wrote the BaseActivity layout like that:

<include layout="@layout/titlebar" />
<!-- Some control that can contain layout -->
<include layout="@layout/statusbar" />

And MainActivity that extends BaseActivity.
I want to insert in code the layout of the MainActivity to the specific space on BaseActivity.

Is it possible to do that? If yes, how can I set the layout of my inheritance activity in specific spase on this BaseActivity? There is any control that can contain layout?

nrofis
  • 8,975
  • 14
  • 58
  • 113

2 Answers2

1

Sure, its possible. You'll do it like you do any Java inheritance. You'll want to create a BaseActivity which will extend Activity. Your other Activities, will extend BaseActivity. Not always, but generally, you'll want your BaseActivity to be abstract class. You don't want to create instances of it. So, you should call setContentView from the EXTENDING Activity. That way each Activity will have its own layout.

Givi
  • 3,283
  • 4
  • 20
  • 23
  • I have `BaseActivity` and `MainActivity`. How can I put the layout of my `MainActivity` in the specific space on the `BaseActivity`? – nrofis Oct 19 '12 at 12:53
  • I see, but if I call to `setContentView` its deletes my toolbar and statusbar, and show the new layout insted. How can I put the new layout between them? There is an control that can contain a layout? – nrofis Oct 19 '12 at 13:01
  • Hmmm.. in that case I think you'll want to use the Tag in your XML. Make your statusbar and toolbar in a different XML and then where ever you want. Example: – Givi Oct 19 '12 at 13:04
  • I did it in my `BaseActivity` layout. Thats the reson that I have created this class. You say that I must to use the `` tag in each activity? And the `BaseActivity` is not necessary? – nrofis Oct 19 '12 at 13:09
1

This can be possible from java side (Note that I didn't tried yet), here is an example Reusing layout XML and the code behind.

This is can also be done via layouts.

You have to make a separate layout (xml) for you toolbar or status bar, and then you have to include in required layout.

Re-using Layouts will be helpful link and Android Layout Trick #2: Include to Reuse

Community
  • 1
  • 1
Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
  • I did the tag in my BaseActivity. I just want to put layers in the middel of them. To do that I must to use the tag in each activity? And the BaseActivity is not necessary? – nrofis Oct 19 '12 at 13:14
  • What do you mean by 'I did the tag in my BaseActivity.'? Are you using include in java code? – Pankaj Kumar Oct 19 '12 at 13:27
  • No, in my xml layut file of my BaseActivity. I changed my question for more information... – nrofis Oct 19 '12 at 13:30
  • Yes you have to include titlebar and statusbar in each layout in which you want to use. And this is not an inheritance behavior (as u are thinking), but you have one benefit, if you have any change in title or status you need to change only one/two file. – Pankaj Kumar Oct 19 '12 at 13:34
  • I understood, I just wanted to create a general style activity. I call that inheritance because in C#, for example, it can be done with inheritance. You can write general form and expand it in its sans form. Thank you – nrofis Oct 19 '12 at 13:42