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. taking a screenshot...of whatever is drawn on the view

I am trying to take a screen shot of whatever is drawn on my custom
view's canvas. I use the following lines for the same:

//enable drawing cache
this.setDrawingCacheEnabled(true);
// use the drawing on the cache to create a Bitmap Object
Bitmap screenshot = Bitmap.createBitmap(this.getDrawingCache());
// disable the cache
this.setDrawingCacheEnabled(false);

Problem:

The getDrawingCache() call returns null sometimes.
Any pointers to what I am doing wrong here?

Thanks.
--~--~---------~--~----~------------~-------~--~----~

2. ADB shell exiting abnormally

I find that when I am running 2 android devices connected to 1 linux
machine, I find I can get ADB shell to exit abnormally.

If I start a shell on Device 1, then on Device 2 start a shell and
type reboot.   Device 2 resets like expected, but the shell on device
1 will exit.

I find that functionality of running controlling multiple devices with
ADB very useful for testing.   My question is can anyone reproduce
this?   Has this been fixed upstream?   Is ADB planning on first class
support for multiple devices, or was this an after thought feature?

Any help debugging this, or possible just a little information in the
right direction so I can make a patch myself would be helpful.

Keith Preston


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

3. I get the "unable to start wifi"

4. OpenGL ES 2.0?

5. Any suggestions on how to save and XML DOM back to a file?

6. Real time kernel support in Android

7. emulator: could not find virtual device named...