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. display thumbnail images from sdcard


i can not view any image in my emulator screen
can anyone please tell me where i am doing wrong in my code.

this is my code

<?xml version="1.0" encoding="utf-8"?>
< manifest xmlns:android=""
      package="image.Thumbnails" android:versionCode="1"
      < uses-permission
android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
      < application android:icon="@drawable/icon"
            < activity android:name=".ImageThumbnailsActivity"
                  < intent-filter>
                        < action
android:name="android.intent.action.MAIN" />
                        < category
            < activity android:name=".ViewImage">
                  < intent-filter>
                        < action
android:name="android.intent.action.VIEW" />
                        < category
android:name="android.intent.category.DEFAULT" />

package image.Thumbnails;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;

public class ImageThumbnailsActivity extends Activity {
      /** Called when the activity is first created. */
      private Cursor imagecursor, actualimagecursor;
      private int image_column_index, actual_image_column_index;
      GridView imagegrid;
      private int count;
      public void onCreate(Bundle savedInstanceState) {
      private void init_phone_image_grid() {
            String[] img = { MediaStore.Images.Thumbnails._ID };
            imagecursor = managedQuery(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, img, null,
null, MediaStore.Images.Thumbnails.IMAGE_ID + "");
            image_column_index = imagecursor
            count = imagecursor.getCount();
            imagegrid = (GridView) findViewById(;
            imagegrid.setAdapter(new ImageAdapter(getApplicationContext
            imagegrid.setOnItemClickListener(new OnItemClickListener()
                  public void onItemClick(AdapterView parent, View v,
int position, long id) {
                        String[] proj =
{ MediaStore.Images.Media.DATA };
                        actualimagecursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj,
null, null, null);
                        actual_image_column_index = actualimagecursor
                        String i = actualimagecursor.getString
                        Intent intent = new Intent
(getApplicationContext(), ViewImage.class);
                        intent.putExtra("filename", i);

      public class ImageAdapter extends BaseAdapter {
            private             Context mContext;
            public ImageAdapter(Context c) {
                  mContext = c;
            public int getCount() {
                  return count;
            public Object getItem(int position) {
                  return position;
            public long getItemId(int position) {
                  return position;
            public View getView(int position,View
convertView,ViewGroup parent) {
                  ImageView i = new ImageView
                  if (convertView == null) {
                        int id = imagecursor.getInt
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, ""
+ id));
                        i.setLayoutParams(new GridView.LayoutParams
(92, 92));
                  else {
                        i = (ImageView) convertView;
                  return i;

// By selecting the thumbnails user can view the actual image.
package image.Thumbnails;

import android.os.Bundle;
import android.widget.ImageView;
import android.content.Intent;

public class ViewImage extends Activity {
      private String filename;
      public void onCreate(Bundle savedInstanceState) {
            Intent i = getIntent();
            Bundle extras = i.getExtras();
            BitmapFactory.Options bfo = new BitmapFactory.Options();
            bfo.inSampleSize = 2;
            filename = extras.getString("filename");
            ImageView iv = new ImageView(getApplicationContext());
            Bitmap bm = BitmapFactory.decodeFile(filename, bfo);


2. How to exchange data between different users


I have developed an application which store data personal comments on
the phone but I'd like to create a V2 with would allow users to
exchange these data with their friends.

How could I store these data so that other users could have access to
them ?

Is there another way that to rent a space on a server ? Is google
providing some space like for my maps for example ?

Thanks for your help


3. All viewgroup redraw when an internal ImageView is changed

4. Change the color of ratingbar stars

5. ppp daemon not starting

6. Error on Google page [ReferenceError: Can't find variable: _gp_initSearch ]

7. Android data buffer pass by jni problem