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. Hardware platform thoughts

I've noticed that there are 533MHz and 1000MHz phones running Android,
both ARM9.  I've also noticed ARM9 laptops from Cherrypal running
533MHz, with Android.

This brings to mind an iPad competitor with Android.  Effectively, a
giant cell phone, with a GSM/3G chipset in it, accepting a SIM card,
etc.  The obvious problem here is that if it's a 3.2GHz ARM9 with 8
gigs of RAM, using the same app store as cell phones will create huge
issues for cell phone users with 1000MHz and 256MB.  Using a different
store creates fragmentation.

So I'm thinking, about phones, about netbooks, about netpads, about
whatever, and I'm thinking that anything these things do should be
feasible.  This brings a few thoughts to mind:

1.  My phone has a 533MHz CPU, and 256MB of RAM.  I believe it'd be
feasible to design an identical phone with 512MB of RAM and dual
533MHz ARM9 CPUs.  To that end, a netpad with Android and 2 x 533MHz
would be feasible and interesting.

2.  I'm not sure if 2 x 1GHz would be infeasible for a phone; but if
such a device was released, Android apps targeting it would have twice
as much CPU power as the higher end 1000MHz ARM9 phones, and four
times what a 533MHz phone has.  That may be a step better taken

3.  As you can tell, I'm interested in the general concept of SMP on
Android, especially on phones.  I know the kernel can handle SMP
easily enough (source code; not sure how Android itself is compiled).
This may be lacking for the ARM platform (due to lacking hardware,
possibly).  I wonder how this would affect real-time response, GPS/
Bluetooth/phone call performance, etc. as well as power usage compared
to just a single chip at twice the speed.  Using multiple packages
would dissipate heat better too; but of course the phone's a
restricted box with no fans, so this becomes fruitless eventually.  Of
course, lower power usage overall means less heat produced.



2. Sound question

Hopefully I can get some advice here on this.  I also think that this
may have been asked before, but I want to get some new

For applications that work like musical keyboards (think simple 8-16
notes for now), what sound tools are best for this type of
application.  I'm thinking that a user could press on a key to make a
sound or press on a different key that has a predefined chord
associated with it.  I've used SoundPool in the past and it works
great in my apps, however I found it to be a bit lagging if you add
more than a handful of simultaneous sounds (ie: a chord).  I'm
currently looking at JetPlayer and it has promise, but I'm finding
that it has some deficiencies of it's own.  AudioTrack seems very raw
and {*filter*}, but I have no idea where to begin making sounds with it
(at least sounds that sound good) and it seems it might have some
limitations as to how many notes could be played at once.

Does anyone have any general advice in this realm?

Thanks for anything!



3. Perjuangan seorang mahasuhu

4. Bajak Thread

5. ScrollView with EditText - Scrolling in EditText Issue

6. The application has stopped unexpectedly. Please try again.

7. Android Market woes... the press finally catch on