How to set height of Gallery view cell equals Gallery height? - android

I have Gallery control with ImageView as cell View. I need to have ImageView size equals the Gallery height. How can I do that? I tried to set height like this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
FrameLayout retval = (FrameLayout) convertView;
ImageView imageView = (ImageView) retval.findViewById(R.id.image);
imageView.setLayoutParams(new FrameLayout.LayoutParams(parent.getHeight(), parent.getHeight()));
But first cell has height equals zero but next cells has right value.

Why not just set the imageView to fill the parent container through the params?
#Override
public View getView(int position, View convertView, ViewGroup parent) {
FrameLayout retval = (FrameLayout) convertView;
ImageView imageView = (ImageView) retval.findViewById(R.id.image);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
imageView.setLayoutParams(layoutParams);
That is, unless you want the width of the imageview to be equal to the height of the gallery. Then this would not work

I think you should set layoutParams on cell itself and let image view to scaleXY to the cell.
retval.setLayoutParams(new FrameLayout.LayoutParams(parent.getHeight(), parent.getHeight());
imageView.setScaleType(ScaleType.FIT_XY);

Related

Cannot center text when programmatically creating view from adaptor

I am using an adaptor to generate a view for each of several items. The view needs to show an image with centered text below it. I create a linear layout and add an image view and text view.
To show the problem I have changed the background colors to green for the linear layout and red for the text view...
Obviously the text is showing left aligned instead of centered. Here is the simple code in the adaptor GetView method...
public override View GetView(int position, View convertView, ViewGroup parent)
{
ImageView imageView = new ImageView(_context);
LinearLayout.LayoutParams imageViewParam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, LinearLayout.LayoutParams.WrapContent);
imageView.LayoutParameters = imageViewParam;
imageView.Id = 1000 + position;
imageView.SetImageResource(Resource.Drawable.Code);
TextView textView = new TextView(_context);
LinearLayout.LayoutParams textViewParam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, LinearLayout.LayoutParams.WrapContent);
textView.LayoutParameters = textViewParam;
textView.Text = OPTIONS_TEXT[position];
textView.SetBackgroundColor(Color.Red);
textView.SetForegroundGravity(GravityFlags.CenterHorizontal);
LinearLayout linear = new LinearLayout(_context);
linear.LayoutParameters = new GridView.LayoutParams(200, 172);
linear.SetBackgroundColor(Color.Green);
linear.Orientation = Orientation.Vertical;
linear.AddView(imageView);
linear.AddView(textView);
return linear;
}
As far as I can work out, you cannot set the layout_gravity setting when generating the view programmatically and so I tried setting the foreground gravity but has no effect at all.
I have tried everything I can think of but nothing gets that text centered!
Set gravity center to LinearLayout.LayoutParams of TextView i.e.
textViewParam.gravity = Gravity.CENTER;

Load image without cropping in imageview android

I created the app to display images in carousel view. I loaded images into imageview which is in another xml layout file & set them in carousel view contain in main xml layout file.
Here how i load imaged into image view
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.showsmain, null);
ImageView image=(ImageView)vi.findViewById(R.id.imageView1);
//LinearLayout rlayout=(LinearLayout)vi.findViewById(R.id.layout);
image.setImageResource(R.drawable.black);
orgWidth = image.getDrawable().getIntrinsicWidth();
orgHeight = image.getDrawable().getIntrinsicHeight();
imageLoader.DisplayImage(data[position], image);
imageLoader.getDimension(widthScreen, heightScreen);
LinearLayout lhome=(LinearLayout)((Activity) activity).findViewById(R.id.layouthome);
// LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
// widthScreen,heightScreen/3);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
widthScreen,heightScreen/3);
params.width=orgWidth;
image.setLayoutParams(params);
image.setScaleType(ImageView.ScaleType.FIT_XY);
return vi;
}
orgWidth is a width of imageview. widthscreen is a width of screen where app is run. below you can see how image is loaded in carousel view.
![enter image description here][1]
here you see image is crop. I want to load the images without crop....
I try to handle this during three days with three question in stackoverflow.
If anyone can know the answer .....help me
here I also give my carousel layout xml file....
<com.touchmenotapps.carousel.simple.HorizontalCarouselLayout
android:id="#+id/carousel_layout_event"
android:layout_width="600dp"
android:layout_height="500dp"
android:layout_above="#+id/relativeLayout1"
android:layout_alignTop="#+id/carousel_layout" >
</com.touchmenotapps.carousel.simple.HorizontalCarouselLayout>
After you make your ImageView, call this:
image.setScaleType(ScaleType.CENTER_INSIDE);
Then, instead of cropping, your image will scale to fit the View bounds with a maintained aspect ratio.

How to fix gridview item sizes (width and height) when I put an image on view item in Gridview

I have a gridview that has multi items (buttons) I bind background for each item in my adapter
public View getView(int position, View convertView, ViewGroup parent) {
Button btn;
if (convertView == null)
{ // if it's not recycled, initialize some attributes
btn = new Button(mContext);
KeypadButton keypadButton = mButtons[position];
btn.setTag(keypadButton);
btn.setBackgroundResource(R.drawable.button_image);
btn.setTextColor(android.R.color.white);
}
else
{
btn = (Button) convertView;
}
btn.setText(mButtons[position].GetContent());
return btn;
}
the imageview item change it's size when I setBackgroundResource , Is it way to avoid resizing for image view item .
You can set LayoutParams with fixed size to avoid resizing the view
LayoutParams params = new LayoutParams(width, height);
btn.setLayoutParams(params);
if you don't set LayoutParams the size will be affected by background resource size.
to get more information visit:
https://stackoverflow.com/a/6170160/1891878

Gallery View not filling complete screen when get created

I have created horizontal gallery view using this Tutorial
But when gallery is created it starts from half of the screen, below screen shot
I want that when gallery view starts it should start from left of screen not center.
Thanks
Better way to escape, mark the focus from second item.
Try to use in xml--
android:gravity="left"
in your getView function you can set the image that should be centered. So you can set the last image as below
imageView.setImageResource(mImageIds[position]);
Something I did
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mImageIds[position]);
imageView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setBackgroundResource(mGalleryItemBackground);
return imageView;
}

Gallery with image and text

I'm trying to build a gallery with text below the image but although I've followed every response founded in here I've yet to accomplish my objective.
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout ll = new LinearLayout(mContext);
ll.setOrientation(LinearLayout.VERTICAL);
ImageView i = new ImageView(mContext);
i.setImageResource(mImageIds[position]);
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setLayoutParams(new Gallery.LayoutParams(150, 150));
TextView tv = new TextView(ll.getContext());
tv.setTag(mText[position]);
tv.setText(mText[position]);
tv.setLayoutParams(new Gallery.LayoutParams(48, 48));
ll.addView(tv);
// The preferred Gallery item background
//i.setBackgroundResource(mGalleryItemBackground);
return ll;
//return i;
}
I don't know why (and maybe it's the dumbest thing) but my images don't appear:)
I believe you forgot to add the image view to the linear layout, simply add:
ll.addView(i);
Also you are not recycling the convertView which could cause problems unless you have very few images and little to no scrolling of the grid.
You should check if convertView is null and if it is not simply change the text and image of the existing convertView.
Here is a good example for a custom grid view:
http://vikaskanani.wordpress.com/2011/07/20/android-custom-image-gallery-with-checkbox-in-grid-to-select-multiple/

Categories

Resources