grid view layout settings in android - android

I have created grid view for my wallpaper application but I am facing a problem with my layout. My grid view layout changes with different devices. I want the same view for all devices. Help me out.
This is my xml file.
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="30dp"
android:gravity="center"
android:horizontalSpacing="2dp"
android:numColumns="2"
android:stretchMode="columnWidth"
android:verticalSpacing="2dp"
android:orientation="vertical"
android:id="#+id/gridview"
And this is Imageadapter file in which I have set my grid view.
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imgGrid;
if (convertView == null)
{
imgGrid = new ImageView(current);
imgGrid.setLayoutParams(new AbsListView.LayoutParams(300,200));
imgGrid.setPadding(2, 2, 2, 2);
imgGrid.getScaleType();
imgGrid.setScaleType(ScaleType.FIT_XY);
}
else
{
imgGrid = (ImageView)convertView;
}
imgGrid.setImageResource(images[position]);
return imgGrid;
}}

replace this line
imgGrid.setLayoutParams(new AbsListView.LayoutParams(300,200));
with
imgGrid.setLayoutParams(new GridView.LayoutParams(70, 70));
feel change to pass layout params parameter. But don't give large values it won't look good on other devices.

Related

Android GridView not working properly on smaller and bigger screen devices

I have a GridView in my Android app, which is having images. I have 10 rows and 2 columns in the GridView. With that, I have same size images in every grid i.e. 20 images.
The grid code is,
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:columnWidth="140dp"
android:drawSelectorOnTop="true"
android:gravity="center"
android:numColumns="2"
android:stretchMode="columnWidth"
android:verticalSpacing="5dp"
android:focusable="true"
android:clickable="true"
/>
Meanwhile in the java code, I have used an Adapter. Here's the code, which sets the individual grid size and images, with the scaleType,
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(340, 400));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setPadding(10, 10, 10, 10);
It's working fine on device screen size 5'0, but on other devices such as 5'7, 4'5, it's not working in the same way. The grids (and their individual images) are somewhat hiding/ cropping. Even under 5'0, it is showing some extra space, which looks poor.
How GridView works correctly on different screen sizes.
Here's a sample, how is it looking like in 5'7,
Here's a sample, how is it looking like in 5'0.
This is how I want it to be visible on every screen size.
Here's a sample, how is it looking like in 4'0. Here the images are getting overlapped.
You can use item view for your GridView same as ListView in your adapter as below:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.item_grid, null);
holder = new ViewHolder();
holder.mImage = (ImageView) convertView.findViewById(R.id.img);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.mImage.setImageResource(imageIDs[position]);
return convertView;
}
public class ViewHolder {
private ImageView mImage;
}
I have created item_grid as below:
<?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="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/grid_image"
android:layout_width="match_parent"
android:layout_height="130dp" />
</LinearLayout>
Suggestion: You can use RecyclerView with GridLayoutManager with 2 columns
This code works like a charm for me in the Adapter:
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
//Calculation of ImageView Size - density independent.
//maybe you should do this calculation not exactly in this method but put is somewhere else.
Resources r = Resources.getSystem();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 60, r.getDisplayMetrics());
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams((int)px, (int)px));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
//imageView.setPadding(8, 8, 8, 8);
imageView.setBackgroundColor(Color.BLUE);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
Source:
https://stackoverflow.com/a/11485625/7639113
After adding the code above, I also create a different layout for bigger screen, something like this:
normal_layout.xml
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:numColumns="2"
android:verticalSpacing="5dp"
/>
bigscreen_layout.xml
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:numColumns="4"
android:verticalSpacing="5dp"
/>
Note that I set numColumns differently for those layouts above.
Here is how I created layout for different screen sizes:
Create new Android resource directory in res
Change resource type to layout, select smallest screen width for the available qualifiers, then click ">>"
Fill in 600 in the smallest screen width, Android Studio will automatically generate layout-sw600dp directory. Click OK.
Now all you have to do is place normal_layout.xml in the layout directory, and place bigscreen_layout.xml in the layout-sw600dp directory.
You can look at the different screen size detail in the official Android guide Supporting Multiple Screens:

Android GridView Item Selectors change position and duplicate

I have a GridView layout with 3 columns and several rows that contains pictures taken from Facebook. When I select an image, a selector appears for that item in the gridView as normal. (I don't use the android selector but a custom layout linked below.)
The problem is this: if I select one or more items, when I scroll down the screen to see more pictures I discover that other items have already been selected! Moreover, if I scroll up to see the items I've previously selected, the selectors change their position. It seems to me that the index of each selector update every time I scroll the screen.
I created a personal ImageAdapter to inflate the pictures from facebook. Here the getView:
public View getView(final int position, View convertView, ViewGroup parent) {
View v;
ImageView imageView = null;
if(convertView==null){
LayoutInflater li = getLayoutInflater();
v = li.inflate(R.layout.photos_item, null);
}else{
v = convertView;
}
imageView = (ImageView)v.findViewById(R.id.image);
v.setTag(imageView);
if (arrayPhotos.get(position).getPictureSource() != null){
Log.i(TAG, "Position in GridView = " + Integer.valueOf(position).toString());
imageLoader.DisplayImage(arrayPhotos.get(position).getPictureSource(), imageView);
}
return v;
}
Here the onItemClick:
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
View checkedItem;
checkedItem = (View) view.findViewById(R.id.check);
fbPicture = arrayFbPictures.get(position);
Log.i(TAG, "Position = " + arrayFbPictures.indexOf(fbPicture));
Log.i(TAG, "PicId = " + fbPicture.getPictureID());
if( fbPicture.isChoosed() ){
Log.i(TAG, "checkIcon DISACTIVATED");
fbPicture.setChoosed(false);
checkedItem.setVisibility(View.INVISIBLE);
parent.invalidate();
}
else{
Log.i(TAG, "checkIcon ACTIVATED");
fbPicture.setChoosed(true);
checkedItem.setVisibility(View.VISIBLE);
parent.invalidate();
}
}
That is the xml of the GridView:
<GridView
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:gravity="center"
android:numColumns="auto_fit"
android:columnWidth="96dp"
android:stretchMode="spacingWidth"
android:horizontalSpacing="8dp"
android:verticalSpacing="8dp"
android:paddingTop="8dp"
android:paddingLeft="8dp"
android:paddingRight="8dp" >
</GridView>
And this the xml of the custom selector:
<RelativeLayout
android:id="#+id/check"
android:layout_width="96dp"
android:layout_height="96dp"
android:background="#color/black_trasparency_light"
android:visibility="invisible" >
<ImageView
android:id="#+id/image_check"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:scaleType="center"
android:src="#drawable/ic_check_colored_48" />
</RelativeLayout>
Thank you.
The reason this is happening is because GridView uses view recycling. When you scroll down the GridView, instead of creating new views to put in the bottom, GridView reuses the ones that disappear from the top. What this means is that you have to remember to fully set the state of the view inside the call to getView().
That's a little hard to digest, so here's some code:
public View getView(final int position, View convertView, ViewGroup parent) {
...
View checkedItem = (View) v.findViewById(R.id.check);
fbPicture = arrayFbPictures.get(position);
if (fbPicture.isChoosed()) {
checkedItem.setVisibility(View.VISIBLE);
} else {
checkedItem.setVisibility(View.INVISIBLE);
}
return v;
}
The code above is setting the checked state of the convertView every time getView() is called, so the recycled view will not retain any of its previous state.
PS. The is done to conserve the memory and to improve performance of GridView and ListView. You can watch this awesome presentation for more information.

increase images sizes loads in carousel view android

I created app to display images from internet in carousel view. I loaded images into carousel view using imageview. just like following way.
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);
// RelativeLayout rlayout=(RelativeLayout)vi.findViewById(R.id.rlayouts);
image.setImageResource(R.drawable.black);
orgWidth = image.getDrawable().getIntrinsicWidth();
orgHeight = image.getDrawable().getIntrinsicHeight();
imageLoader.DisplayImage(data[position], image);
imageLoader.getDimension(widthScreen, heightScreen);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
widthScreen, heightScreen/3);
params.width=widthScreen;
image.setLayoutParams(params);
image.setScaleType(ImageView.ScaleType.FIT_XY);
//rlayout.addView(image);
return vi;
}
and my carousel layout xml file is,
<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>
then image show as following way in carousel,
![enter image description here][1]
Here i want to increase the image width & height. It means one images must load fill with in the screen
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.showimage, 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);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
widthScreen,heightScreen/3);
params.width=widthScreen/2;
image.setLayoutParams(params);
image.setScaleType(ImageView.ScaleType.FIT_XY);
//rlayout.addView(image);
return vi;
}
this way I add the imageview into carousel view.
how to do that. I asked question similer this one. But i didn't get answer. pls help me someone. pls.....
my imageview xml layout file..
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/rlayouts">
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:scaleType="fitXY"
android:layout_alignParentTop="true" />
</RelativeLayout>
I think you should use a Horizontal oriented Linear layout inside a Horizontal ScrollView then you should add ImageViews with FillParent Height and Width in this Linear layout.
It would be great if you elaborate your question a bit more.

CheckBox covers text in GridView

I'm pretty new to Android, and I'm looking for a little help.
I have a GridView with the following XML in a linear layout.
<GridView android:id="#+id/gridview"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:numColumns="2"
android:columnWidth="90dp"
android:stretchMode="columnWidth"/>
I'm adding a series of CheckBoxes to the view with...
public View getView(int position, View convertView, ViewGroup parent)
{
CheckBox checkView;
if (convertView == null) {
checkView = new CheckBox(context);
checkView.setPadding(5, 5, 5, 5);
} else {
checkView = (CheckBox) convertView;
}
checkView.setText(checkNames[position]);
checkView.setWidth(400);
return checkView;
}
No matter how many different widths I try, including fill_parent and
wrap_content, the text winds up behind the box instead of next to it.
Any hints or tricks?
Thanks,
Andrew
Try using only wrap content for both layout width and layout height. Because the fill_parent will fill the full layout, where as wrap_content will fill only to the size of your text entered.

to much space between images in gallery android

Hello i have an activity that loads lots of images from the drawable folder, and put them in a gallery view from an Adapter. The problem is that there's too much space between this images.
Here's the getView i use:
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
convertView = new ImageView(mContext);
convertView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
ImageView iv = (ImageView) convertView;
iv.setImageResource(mThumbIds[position]);
return convertView;
}
Here is my xml layout:
<Gallery android:id="#+id/gallery"
android:background="#000000"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
/>
Is there any way i can get the images to be next to each other without having to create a new view for every image?
Best Regards.
Try using android:spacing in the layout file, to decrease space between images.

Categories

Resources