Reusing views/code-

by Richard » Thu, 22 Apr 2010 12:04:37 GMT


Sponsored Links
 I have an ImageButton in the layout of several of my Activities that
launches an "information" dialog.

At the moment I'm adding the (almost the) same the ImageButton XML and
(almost the) same onClickListener java code to every Activity that
needs it.  In each activity I'm giving the ImageButton a different id
so that I can reference it.

This seems like poor design because the code is essentially the same
across all activities and I don't get to take advantage of code-reuse.

Is there a smarter way to reuse a view in the layout of several
different activities?

--



Reusing views/code-

by ~ TreKing » Thu, 22 Apr 2010 21:59:17 GMT


 



What I would probably do, based on the info you provided:
- Encapsulate the similar onClickListener code into one class. Take care of
differences via parameters or by subclassing.
 - Create a single XML layout file that defines the ImageView with it's
properties as it will be across all activities with a single ID. Use the
"include" tag in the other Activities layouts to re-use the view in those
layouts.
- Create some helper function to find the ImageView in question for a given
activity and set the appropriate click listener on it.

Hope that helps.

-------------------------------------------------------------------------------------------------
TreKing - Chicago transit tracking app for Android-powered devices
 http://sites.google.com/site/rezmobileapps/treking 

--


Sponsored Links


Other Threads

1. receiving broadcast intent, while appl is closed.

If appl is closed, in which there is broadcast receiver, after
receiving it appl perform some activity.   This intent is broadcast
by  other appl but the receiving appl is closed will it receive the
broadcast intent.
--~--~---------~--~----~------------~-------~--~----~

2. New Posting at funfashion4u (on 14 & 15 Apr 09)

Budget of 
India< http://www.***.com/ ;
Need
of Monica is 
here< http://www.***.com/ ;
power
of a man < http://www.***.com/ ; Lion
and Tiger < http://www.***.com/ ; Pipe
Kitty Hello < http://www.***.com/ ;
Japanese
Monkeys < http://www.***.com/ ;
Driving
Test Girl < http://www.***.com/ {*filter*}.html> Eat
Chinese Foods< http://www.***.com/ ;
Emergency
Phone Ahead< http://www.***.com/ ;
Elephant
Sitting < http://www.***.com/ ;
Elephant
Playing 
Game< http://www.***.com/ ;
We
Love with 
Computer-10< http://www.***.com/ ;
We
Love with 
Computer-9< http://www.***.com/ ;
We
Love with Computer
CDs< http://www.***.com/ ;
We
Love with 
Computer-8< http://www.***.com/ ;
We
Love with 
Computer-7< http://www.***.com/ ;
We
Love with 
Computer-6< http://www.***.com/ ;
We
Love with 
Computer-5< http://www.***.com/ ;
We
Love with 
Computer-4< http://www.***.com/ ;
We
Love with 
Computer-3< http://www.***.com/ ;
We
Love with 
Computer-2< http://www.***.com/ ;
We
Love with 
Computer-1< http://www.***.com/ ;
Windows
Hardware < http://www.***.com/ ;
System
of 
Communication< http://www.***.com/ ;
Funny
Shaving 
Mirror< http://www.***.com/ ;
Shifting
of Computer< http://www.***.com/ ;
Connected
Well < http://www.***.com/ ; Cat
Enjoy < http://www.***.com/ ; Hot New
Trends for girls-14< http://www.***.com/ ;
Hot
New Trends for 
girls-12< http://www.***.com/ ;
Hot
New Trends for 
girls-11< http://www.***.com/ ;
Airman
and Colonel< http://www.***.com/ ;
Old
Times Army 
Days< http://www.***.com/ ;
Soldier
and Girl 
Friend< http://www.***.com/ ;
Navy
Chief and 
Admiral< http://www.***.com/ ;
Platoon
Sergeant < http://www.***.com/ ;
Israeli
Soldier < http://www.***.com/ ;
Commanding < http://www.***.com/ ; Dessert
Talks < http://www.***.com/ ; Soldiers
and Drill 
Sergeant< http://www.***.com/ ;
Banjo
Players < http://www.***.com/ ; Great
Writer < http://www.***.com/ ; Boot
Camp Range < http://www.***.com/ ;
General
Motors and Bill
Gates< http://www.***.com/ ;
Microsoft
Future Nest 25 
Years< http://www.***.com/ ;
Food
Most Damaging< http://www.***.com/ ;
A
marriage 
desperate< http://www.***.com/ ;
Marriage
studies 
find< http://www.***.com/ ;
They
think what 
will?< http://www.***.com/ ;
Arrested
for 
speeding< http://www.***.com/ ;

--~--~---------~--~----~------------~-------~--~----~

3. Porting android-2.6.29 to gumstix Overo Earth linux-omap3-2.6.29 kernel version

4. Android doesn't support ICC Profile related API?

5. android_on_davinci

6. com.android.layoutlib.bridge.BridgeCanvas has a bug when drawing a round rect

7. Re-installation failed due to different application signatures