Marwa Shams
Marwa Shams

Reputation: 179

How can I make the background-Color of gallery items transparent and change border style and size?

This is What I want to achieve


This is what appears "I want to modify"


1 - how can I change the background of the items in gallery to be transparent? 2 - How can I remove the borders around each item in gallery? 3 - How can I show them in normal size "without being stretched" ?


Here is the code used .java

public class Search extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.options);
          Gallery gallery = (Gallery) findViewById(R.id.gallery1);
            gallery.setAdapter(new ImageAdapter(this));

            gallery.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView parent, View v, int   position, long id) {
                    Toast.makeText(Search.this, "" + position, Toast.LENGTH_SHORT).show();
                }
            });
}
public class ImageAdapter extends BaseAdapter {
        int mGalleryItemBackground;
        private Context mContext;

        private Integer[] mImageIds = {
                R.drawable.animals,
                R.drawable.automotive,
                R.drawable.directory,
                R.drawable.homeoffice,
                R.drawable.hobbies,
                R.drawable.mother,
                R.drawable.jobs,
                R.drawable.realestate,
                R.drawable.technology
        };

        public ImageAdapter(Context c) {
            mContext = c;
            TypedArray attr =   mContext.obtainStyledAttributes(R.styleable.HelloGallery);
            mGalleryItemBackground = attr.getResourceId(
                    R.styleable.HelloGallery_android_galleryItemBackground,17170445 );
            attr.recycle();
        }

        public int getCount() {
            return mImageIds.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView = new ImageView(mContext);

            imageView.setImageResource(mImageIds[position]);
            imageView.setLayoutParams(new Gallery.LayoutParams(150, 100));
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setBackgroundResource(mGalleryItemBackground);

            return imageView;
        }
    }
}

Upvotes: 1

Views: 3577

Answers (1)

Tomik
Tomik

Reputation: 23977

You have to adjust parameters of the ImageView that's being created as a Gallery item. In this case you have to change contents of the getView() function.

  1. Set the proper background. Try imageView.setBackgroundColor(Color.TRANSPARENT). It sets the background transparent and also it removes the borders around the item (borders are described by the background).

  2. Solved in 1.

  3. You can affect the way the image is (or isn't) stretched by changing the scale type in imageView.setScaleType() function (see the documentation for ImageView.ScaleType for more details). Try imageView.setScaleType(ImageView.ScaleType.FIT_CENTER). Also you can set "better" layout params (try imageView.setLayoutParams(new Gallery.LayoutParams(Gallery.LayoutParams.WRAP_CONTENT, Gallery.LayoutParams.WRAP_CONTENT))).

To sum it up, the getView() functions should be something like this:

public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView = new ImageView(mContext);

    imageView.setImageResource(mImageIds[position]);
    imageView.setLayoutParams(new Gallery.LayoutParams(150, 100));
    imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
    imageView.setBackgroundColor(Color.TRANSPARENT);  

    return imageView;
}

Upvotes: 2

Related Questions