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. Can't get the softinput to show up programmatically

We have a dialog box that includes an EditText field. We'd like the
softkeyboard to already appear in the dialog when the dialog opens
instead of the user first having to touch the EditText field. I've
read other posts and tried to incorporate the suggestions but there's
something that I'm missing.


Here's my code from onStart (you'll notice that I'm calling several
similar methods because from the documentation it's not clear :

  protected void onStart() {
      editText.setFocusable(true);    // which of these should I call?
      editText.setFocusableInTouchMode(true);
      editText.requestFocus();
      editText.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_MENU));
      InputMethodManager imm = (InputMethodManager)
this.context.getSystemService(Context.INPUT_METHOD_SERVICE);
      imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);

      //android.R.attr.windowSoftInputMode | stateAlwaysVisible); //
http://developer.android.com/reference/android/R.attr.html#windowSoftInputMode
      imm.showSoftInputFromInputMethod
(editText.getApplicationWindowToken(),
InputMethodManager.SHOW_FORCED);
      super.onStart();
   }
}


It seems like I might be able to use this type of  code
programatically (due to our project constraints, we build this dialog
programatically instead of via an xml file):   //
android.R.attr.windowSoftInputMode  stateAlwaysVisible); //
http://developer.android.com/reference/android/R.attr.html#windowSoftInputMode

Any pointers would be appreciated!

Jade





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

2. AppWidgetProviders and PackageManager.setComponentEnabledSetting

Hello all,

I am using PackageManager.setComponentEnabledSetting to turn off my
app widget provider receivers to limit what is shown on the widget
list. It seems my changes don't take into affect until I reboot my
phone though. Is there a way to somehow get the app widget list
refreshed or somehow tell Android to reload the package information?

Thank you!
-Brian
--~--~---------~--~----~------------~-------~--~----~

3. How to go back to my apllication's home screen

4. Retrieving data from a USSD command

5. Could NOT Upgrade App in Market!

6. Cannot find provider package when using ProviderTestCase2 to test

7. Problem while running application