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. Two projects, One Service


I need to make a service usable from other applications, I'm trying
the following:

First eclipse Project:
- The class that extend the Service.
- Activity that only launch the service.
- MyInterface.aidl: The interface to comunicate with teh service.
- Manifest.xml: I've added the following:

       <service android:name="Service" android:exported="true"
                  class="Service" android:process=":remote">
                        <action android:name="Test.Service.MyInterface"/>
                        <action android:name="Test.Service.REMOTE_SERVICE"/>

Second eclipse Project:
- MyInterface.aidl: The same interface than in the first project.
- The activity which need use the service.

        private MyInterface mService = null;
        private int res = -2;

        private ServiceConnection mConnection;

        public void onCreate(Bundle savedInstanceState) {

                mConnection = new ServiceConnection() {
                        public void onServiceConnected(ComponentName className, 
service) {
                                mService = 
                        public void onServiceDisconnected(ComponentName name) {
                                mService = null;

                Intent i = new Intent(MyInterface.class.getName());
                Boolean b = bindService(i, mConnection, 

                try {
                        res = mService.requestAd();
                } catch (Exception e) {
                        Log.e("Log","RequestAd: "+e.getMessage());

My problem is that in the second project, when I run it, I obtain "b =
false", It's means that the bindService doesnt work properly.

Could anyone help me? I don't know what I have doing wrong.

Thank's in advance.


2. Audio on Android : \frameworks\base\media\tests\mediaframeworktest

Hii All,

I need to understand how an audio is being played by Android in its
native level. I started looking at its
'\frameworks\base\media\tests\MediaFrameworkTest' to track down the
call sequence into the native level.

I gave a LOG statement everywhere in all the functions that come under
"frameworks\base\media\jni\" thinking any Java Android API would call
its corresponding JNI function which would then call its native code.
But i did not see any of these LOG statements on my logcat.

Can anybody please give me a call sequence for MediaPlayer.start();
till its native code??

i.e, MediaPlayer.start() -> which JNI call(JNI File) in Android tree?
-> which Native function  ??

I am sure there is a lot of expertise with you people on this. Could
you please share it with me and help me to come out of my problem.

Would be really grateful for any help.



3. Problem in sending thisString url to local server

4. onReceive not triggering for SEND / image

5. get the path of a file

6. How to export data to excel?

7. helloworld dynamic implementation????