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. Write a file to sdcard...

Hi,

I'm trying to create a file on the sd card. Using this:

  OutputStream out = new FileOutputStream("/sdcard/tmp.jpg");

but that throws a FileNotFoundException. Isn't OutputStream supposed
to create the file, if not found on disk? I'm looking to write image
data there.

This is on a 1.6 emulator. I have an SD card installed, I'm looking at
the sdcard folder through DDMS, so it's there.

Thanks

-- 

2. How to display DATA SMS

HI all,


Can somebody please help me. In retrieving of DATA SMS.
I am unable to understand how to retrieve the DATA SMS.
I receive wild characters.

Here is the sample code, i am using.

---------------------------------
public void onReceive(Context context, Intent intent) {
                // TODO Auto-generated method stub

                if(!intent.getAction().equals
("android.provider.Telephony.SMS_RECEIVED"))
                {
                        return;
                }


                SmsMessage msg[] = getMessageFromIntent(intent);

                for(int i=0; i<msg.length; i++)
                {
                        String message = msg[i].getDisplayMessageBody();
                        if(message!= null && message.length() > 0)
                        {
                                if(message.startsWith("Something"))
                                {
                                        strMsg = 
msg[i].getDisplayMessageBody().toString();
                                        triggerAppLaunch(context);
                                }
                                else
                                {
                                        return;
                                }
                        }
                }

---------------------------------
private SmsMessage[] getMessageFromIntent(Intent intent) {
                SmsMessage retMsgs[] = null;
                Bundle bdl = intent.getExtras();

                try {
                        Object pdus[] = (Object [])bdl.get("pdus");
                        retMsgs = new SmsMessage[pdus.length];

                        for (int n=0;n<pdus.length;n++)
                        {
                                byte[] byteData = (byte[])pdus[n];
                                retMsgs[n] = SmsMessage.createFromPdu(byteData);
                        }
                } catch(Exception e)
                {
                        Log.e("Get Messages", "Fail", e);
                }
                return retMsgs;
        }


----------------------------------

I have sent DATA SMS by this method.
// Where message is String.
SmsManager sms = SmsManager.getDefault();
 sms.sendDataMessage(phoneNumber, null, DATA_SMS_PORT, message.getBytes
(), sentPI, deliveredPI);

--------------------------

Here is my Manifest file

<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
<uses-permission android:name="android.permission.SEND_SMS"></uses-
permission>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-
permission>

------------------------------------

-- 

3. make call from app & go directly back to app after call hangup?

4. How to get the gallery app to recognize a new extension

5. Query regarding Email application

6. Connection to Java server program

7. Why are onCreate() onStart() etc. called when handset orientation is changed?