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. Need help in customizing EditText

Hi

 I have to modify the EditText view, just like a form text.

Please look at the attachment ..

Thanks in advance....




-- 
Thanks and Regards



 Raju .G

-- 

<<attachment: edittext.JPG>>

2. (WTA) Cari Milestone BNIB

Apakah benar Milestone 1 sudah discontinued, perasaan beberapa minggu lalu
masih dijual di SS tetapi kok sekarang sudah tidak di tawarkan lagi ya?
Dimanakah yg masih sedia Milestone 1 BNIB barangkali ada rekan-rekan yang
tahu atau malah masih menjual?
Japri saja kalau dirasakan menganggu...terima kasih.

st3v4nt

-- 
===============
"

3. Droid 2 siap Gingerbread

4. Setting up a test project issues

5. Should I ProGuard all the library projects associated with my app?

6. Closed WTA: Menjadikan dell streak Hot spot

7. Best way to provide access to a Service?