i'm developing an Android application i have to create a layout composed from a grid view with two columns with a fixed width and a certain number of row.
This gridview is composed from images, in each "square" of my grid there is a image, i need that the the dimensions are exactly the size of the screen, I do not want to be visulizzate by scrolling.
I was able to fix the length, but not the height. How can I make sure that my grill has the size that I specified?
this is my layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="am.MainActivity"
tools:showIn="@layout/activity_main">
<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="2"
android:verticalSpacing="4dp"
android:horizontalSpacing="8dp"
android:stretchMode="columnWidth"/>
</RelativeLayout>
This is the java code:
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// 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) {
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels / 2;
imageView = new ImageView(mContext);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setLayoutParams(new GridView.LayoutParams(width, width));
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.bacheca, R.drawable.turni,
R.drawable.utility, R.drawable.contatti,
R.drawable.invia, R.drawable.info
};
}
The output is perfectly for the width (two images that is contained in the screen size) but the height is wrong because it activates scrolling when I would rather have everything on the screen