Grid View Auto fit is not showing full images - android

Why is my Grid View showing images like this?
My code:
<GridView
android:id="#+id/grid_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/sbsz"
android:numColumns="auto_fit"
android:columnWidth="90dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:gravity="center"
android:stretchMode="columnWidth" >
</GridView>
Image Adapter:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout linearlayout=new LinearLayout(mContext);
ImageView imageView = new ImageView(mContext);
text=new TextView(mContext);
linearlayout.setOrientation(LinearLayout.VERTICAL);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new GridView.LayoutParams(270, 270));
text.setText(mThumbTxt[position]);
linearlayout.addView(imageView);
linearlayout.addView(text);
return linearlayout;
}
}
What's wrong with this code?
How can I make it to show full images? Shouldn't autofit do it?

Autofit means that it will calculate how many 90dp wide columns it can fit. You should change the scale type to center crop, which looks better. And probably use another LayoutParams for the Image, like:
imageView.setLayoutParams(new GridView.LayoutParams(LayoutParams.MATCH_PARENT, 120));

Related

How can I reduce the gap between items of GridView?

Example Captcha:
Here are the codes of Adapter:
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView=new ImageView(context);
imageView.setImageBitmap(list.get(position).getBitmap());
imageView.setLayoutParams(new AbsListView.LayoutParams(
(int) (parent.getWidth() / DataUtils.num),//DataUtils.num=3
(int) (parent.getHeight() / DataUtils.num)));
return imageView;
}
And the XML of GridView:
<GridView
android:id="#+id/gv_paly"
android:layout_below="#id/ll_top"
android:layout_above="#id/ll_bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
></GridView>
Please, how do I reduce the gaps between CAPTCHA images so this looks more professional?
Use android:stretchMode and android:horizontalSpacing.
This will make a fix gap between view.You can set it to 0.
<GridView
android:stretchMode="columnWidth"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:id="#+id/gv_paly"
android:layout_below="#id/ll_top"
android:layout_above="#id/ll_bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</GridView>
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView=new ImageView(context);
imageView.setImageBitmap(list.get(position).getBitmap());
imageView.setLayoutParams(new AbsListView.LayoutParams(
(int) (parent.getWidth() / DataUtils.num),//DataUtils.num=3
(int) (parent.getWidth() / DataUtils.num)));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
return imageView;
}

Dimensions of the GridView cell is not changing

I dont know what is the problem in my code. I want to display some images in a GridView but the images being displayed are very small, and same is happening with the high resolutions images too. Please help. Thanks in Advance.
I dont know what to do. I am posting my code too.
activity_main.xml file
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<GridView
android:id="#+id/gridview_stickers"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center">
</GridView>
gridview_items.xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:orientation="vertical"
android:background="#000000">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/sticker"
android:scaleType="centerCrop"
/>
getView method of the AdapterClass
public View getView(int position, View convertView, ViewGroup parent) {
View imageView = convertView;
ImageView picture;
if(imageView == null) {
imageView = inflater.inflate(R.layout.gridview_items, parent, false);
imageView.setTag(R.id.sticker, imageView.findViewById(R.id.sticker));
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setPadding(8, 8, 8, 8);
}
picture = (ImageView)imageView.getTag(R.id.sticker);
picture.setImageBitmap(img.get(position).imageBitmap);
return imageView;
}
Finally I figured out myself. I changed the
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
to
imageView.setLayoutParams(new GridView.LayoutParams(300, 300));
and I got the desired results.

Scalable Gridview Layout

I've created a scaling, evenly-spaced gridview layout for a simple card-matching game I made for my nephew. It's behaving pretty much as I want, but it seems that there should be a less convoluted way to achieve this, so I was wondering if anyone could suggest a better/simpler way. I'm brand new to Android, so it's likely I over-engineered this.
I have a setting to control the # of cards (12-32), and it scales to any size screen (tablet or phone of various sizes and resolutions).
Here's my gridview XML:
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:background="#drawable/wood"
android:layout_below="#id/score_board"
android:layout_above="#+id/messageparent"
/>
I have an ImageAdapter subclass of BaseAdapter, with a getView that looks like this. The getCellWidth and getCellHeight functions return a value I set in the main activity on layout change (see below).
public View getView(int position, View convertView, ViewGroup parent) {
CardView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new CardView(mContext,position,mCardFaces[position]); //mThumbIds[position]
imageView.setLayoutParams(new GridView.LayoutParams(((MainActivity)mContext).getCellWidth(), ((MainActivity)mContext).getCellHeight()));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
//imageView.setPadding(R.dimen.card_padding, R.dimen.card_padding, R.dimen.card_padding, R.dimen.card_padding);
imageView.setPadding(8, 8, 8, 8);
imageView.setImageResource(CardView.CARDBACK);
} else {
imageView = (CardView) convertView;
//If we recycle an old view, make sure it resizes appropriately
if (imageView.getWidth() != ((MainActivity)mContext).getCellWidth()) {
imageView.setLayoutParams(new GridView.LayoutParams(((MainActivity)mContext).getCellWidth(), ((MainActivity)mContext).getCellHeight()));
}
if(!mCardFaces[position].equals(imageView.getResID()))
imageView.updateCard(mCardFaces[position]);
}
return imageView;
}
Here's the relevant code in my sizeCardsToFit method:
//... algorithm to calc cols, width and height to fit screen
gridview.setNumColumns(nCols);
gridview.setColumnWidth(width);
for (int i = 0; i < gridview.getChildCount(); i++) {
((CardView)gridview.getChildAt(i)).setLayoutParams(new GridView.LayoutParams(width,height));
}
mCellHeight=height;
mCellWidth=width;
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/MyGrid" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:padding="0dp"
android:verticalSpacing="2dp" android:horizontalSpacing="2dp"
android:scrollingCache="true" android:smoothScrollbar="true"
android:clipChildren="true" android:alwaysDrawnWithCache="true"
android:numColumns="auto_fit" android:columnWidth="100dp"
android:stretchMode="columnWidth" android:gravity="center_horizontal"
android:background="#000000">
</GridView>

Design looks bigger when using minsdkversion 3

I created an app, it's finely working as what i want on minsdkversion="3". But other activities look even bigger than usual. If i change the minsdkversion="8" or something, the other activities looks as normal, but i didn't get the output i want.
Following is the code for gridview and imageview layout files:
activity_main.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<GridView
android:id="#+id/grid_View"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:horizontalSpacing="0dp"
android:numColumns="4"
android:stretchMode="columnWidth"
android:verticalSpacing="0dp"
android:gravity="center_horizontal"
android:clipChildren="true" >
</GridView>
</LinearLayout>
grid.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/image_View"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.05"/>
</LinearLayout>
MainActivity.java:
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView imageView;
if (convertView == null)
{
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
i = i+1;
imageView.setId(i);
Log.d("===== i value =====", String.valueOf(i));
imageView.setAdjustViewBounds(true);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
}
else
{
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
What is the wrong with my code, what i have to change?
I have to show some toast when particular body part is pressed
Step #1: Display the body in an ImageView
Step #2: Override onTouchEvent() in your activity or fragment that is managing the ImageView
Step #3: Determine the body part based on the location of the touch event and the size of the ImageView, and display your Toast

Gridview displaying only 12 images in android

I am having a gridView with each item having an image to be loaded. I have used adapter to render the image. I am assigning images as gridView items using resource ID array created using images in drawable folder.
The problem is gridView is being displayed with only 12 images(which fits the view without scrolling). I have around 40 images resource ID in that array. the getView is called only 12-13 times.
Could you pleae help me how to display all 40 images on grid view? Do i need to explicitly add scrollpane in grid.xml?
Below is the grid.xml layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/wooden_background"
android:id="#+id/RootView">
<GridView
android:id="#+id/gridView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="3"
android:stretchMode="columnWidth"
android:padding="5dp"
android:verticalSpacing="5dp"
android:horizontalSpacing="5dp"
android:gravity="center"
android:maxWidth="150dip"
android:maxHeight="150dip" >
</GridView>
</LinearLayout>
Below is the getView function inside adapter
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi;
if (convertView == null){ // if it's not recycled, initialize some attributes
vi = inflater.inflate(R.layout.each_image,parent, false);
}
else{
vi=convertView;
}
/*imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(170,170));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);*/
System.out.println("Inside getView");
ImageView image = (ImageView)vi.findViewById(R.id.imageView);
image.setMaxHeight(150);
image.setMinimumHeight(150);
image.setScaleType(ImageView.ScaleType.FIT_XY );
image.setImageResource(imageIngredientsID[position]);
return vi;
}
How many items the gridview gets depends entirely on your getCount() method in your adapter, what does it return? 12?
Try adding ScrollView to the layout:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/wooden_background"
android:id="#+id/RootView">
<GridView
android:id="#+id/gridView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="3"
android:stretchMode="columnWidth"
android:padding="5dp"
android:verticalSpacing="5dp"
android:horizontalSpacing="5dp"
android:gravity="center"
android:maxWidth="150dip"
android:maxHeight="150dip" >
</GridView>
</LinearLayout>
</ScrollView>

Categories

Resources