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
- 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 


Sponsored Links

Other Threads

1. Need Help - N1 emergency calls terus

Suhu suhi.

Tiba2 kmrn N1 saya emergency calls only seharian. Sudah sy restart ga bs
juga. Saya search network munculny cm telkomsel. Saya turn off lalu on lg
juga tidak bisa. Lalu sy ganti indosat ke hp lain dan N1 saya isi simpati
lha 2 2 nya works. Bisa telp bs sms. Walopun ada keanehan sinyal di N1
sering nya 1 bar saja ataupun hilang , tp tetep bs telp dan di telp.

Nah lalu saya kembalikan lg indosat ke N1 tetep aja tidak bisa.

Oh ya lupa. Uda saya flash rom juga tetep sama ga bisa.
Saya pakai rom cm 6.1.1

Mohon saran dan bantuan. Tq guys.

Sent from n1+simpati



2. Paging Ian (

Maaf mod dan rekan2,

Buat bro, Ian. Mohon japri saya yak. Saya email belum di reply.



3. Why is the GIT repository so slow lately?

4. Build SDK for another platform from Linux

5. Honeycomb Emulator Landscape issue on Ubuntu 10.04

6. ListView items appear 2 or 3 times

7. RECEIVE_BOOT_COMPLETED not sent after power off? (works on restart)