0

in my app i am trying to implement the design of facebook homepage.

I have 7 different activities in my app. For all the 7 activities the buttons in the title(application bar) and footer part to be the same and same actions.

Now my question is, whether i have to create separate 7 layouts for all the 7 activities and i have to write the same button actions in each activity or it can be written once and can be used in all 7 activities ?

if it can be used by writing only once, how to do it pls suggest me a best way ?

  • You can include XML's into a layout to reutilize your code and actions. http://stackoverflow.com/questions/2732682/simple-example-of-merge-and-include-usage-in-android-xml-layouts – Lobo Jun 05 '12 at 10:43

2 Answers2

1

You should probably create a Fragment that contains the buttons, an then reuse the Fragment in every Activity.

Check this out for more info about Fragments:

http://android-developers.blogspot.no/2011/02/android-30-fragments-api.html

Zelleriation
  • 2,834
  • 1
  • 23
  • 23
0

you should create Header and Footer xml files separately

and then use "<include other layout>" Android Layout

to include your header and footer on each Screen.

let me clear if any doubt i have done this, very easy.

also you can check this SO thread

Community
  • 1
  • 1
swiftBoy
  • 35,607
  • 26
  • 136
  • 135
  • hi RDC - how to write a common button action for the button in so that it can be used in multiple activities... – Siva Kannabiran Aug 03 '12 at 03:11
  • can you be more specific i didn't get you well, you want to put a common button in multiple activities and that button does different task in different activity or the same ? – swiftBoy Aug 03 '12 at 06:09
  • RDC - can you pls say me for both actions, that is same button action for multiple activities and different btn action for each activity.... – Siva K Aug 03 '12 at 06:44
  • okay what i would say is it's already 2-3 line of code only so not a big deal you should just get the Button id and load activity in onclick() where you want, see [**this**](http://rdcworld-android.blogspot.in/2012/04/start-activity-from-activity.html).... While we can create a global method to do this but still we need to get instance of current & target activity then pass it to that method so the complexity will be same. correct me if I'm wrong. – swiftBoy Aug 03 '12 at 08:57
  • i am using a button, from , in Activity1 and Activity2. The onClick action of the button in both activities are to show a pop up view. Instead of writing the onclick action in both activities, how can we have it in global and used in those two activities – Siva K Aug 03 '12 at 09:55