How can i change Been programmatically Gridview 'android:numColumns="2"'
when device size larger than 5 inch Or How can i detect Activity create on tablet and set 'numColumns=3'
.
thanks.
How can i change Been programmatically Gridview 'android:numColumns="2"'
when device size larger than 5 inch Or How can i detect Activity create on tablet and set 'numColumns=3'
.
thanks.
Set numColums as auto_fit
to Display as many columns as possible to fill the available space.
what you can do in this situation is create two different layouts for devices bigger than 5 inches and devices having screen space less than 5 inches in those layouts you can set the number of columns in XML as you wish.
Try this:
<GridView
android:id="@+id/gridView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="auto_fit"
>
</GridView>
And In .java file
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int density = metrics.densityDpi;
int width = 0, height = 0;
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
height = metrics.heightPixels;
width = metrics.widthPixels;
if(width >480 && width <780)
{
setContentView(R.layout.home_student);
gridView = (GridView) findViewById(R.id.gridView1);
gridView.setNumColumns(3);
if(density <200)
{
h=200;
w=200;
CustomAdapterStudent mAdapter;
mAdapter = new CustomAdapterStudent(this,prgmNameList, prgmImages,h,w);
gridView.setAdapter(mAdapter);
}
else
{
h=200;
w=200;
CustomAdapterStudent mAdapter;
mAdapter = new CustomAdapterStudent(this,prgmNameList, prgmImages,h,w);
gridView.setAdapter(mAdapter);
}
}
else if(width<=480)
{
setContentView(R.layout.home_student);
gridView = (GridView) findViewById(R.id.gridView1);
gridView.setNumColumns(3);
h=150;
w=150;
CustomAdapterStudent mAdapter;
mAdapter = new CustomAdapterStudent(this,prgmNameList, prgmImages,h,w);
gridView.setAdapter(mAdapter);
}
else if(width >= 780)
{
setContentView(R.layout.home_student);
gridView = (GridView) findViewById(R.id.gridView1);
gridView.setNumColumns(4);
if(density <200)
{
h=200;
w=200;
CustomAdapterStudent mAdapter;
mAdapter = new CustomAdapterStudent(this,prgmNameList, prgmImages,h,w);
gridView.setAdapter(mAdapter);
}
else
{
h=200;
w=200;
CustomAdapterStudent mAdapter;
mAdapter = new CustomAdapterStudent(this,prgmNameList, prgmImages,h,w);
gridView.setAdapter(mAdapter);
}
}
Hope This may help you!