I have gone through the same problem a long time ago & get solved by
this:
You can do some change attributes as below in
Gridview:
<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_below="@+id/rltop"
android:fadingEdge="none"
android:fitsSystemWindows="true"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="3"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:soundEffectsEnabled="true"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" >
</GridView>
Get your Screen Size & Width by:
Display display= ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
myapp.screen_width = display.getWidth();
myapp.screen_height = display.getHeight();
And The Most Important thing:
In your GridAdapter class -> in getView() method:
call below method to target devices with different resolution:
public void screenResolutions(int screen_width,int screen_height, RelativeLayout relativeLayout)
{
if((myapp.screen_width == 240)&&(myapp.screen_height==320))
{
// relativeLayout.getLayoutParams().height=(myapp.screen_height-(35+General.getStatusBarHeight(cntxt)))/3;
relativeLayout.getLayoutParams().width=(myapp.screen_width)/3;
}
else if((myapp.screen_width== 720)&&(myapp.screen_height==1280))
{
// relativeLayout.getLayoutParams().height=(myapp.screen_height-(107+General.getStatusBarHeight(cntxt)))/3;
relativeLayout.getLayoutParams().width=(myapp.screen_width)/3;
}
else if((myapp.screen_width== 320)&&(myapp.screen_height==480))
{
// relativeLayout.getLayoutParams().height=(myapp.screen_height-(47+General.getStatusBarHeight(cntxt)))/3;
relativeLayout.getLayoutParams().width=(myapp.screen_width)/3;
}
else if((myapp.screen_width == 480)&&(myapp.screen_height==800))
{
// relativeLayout.getLayoutParams().height=(myapp.screen_height-(71+General.getStatusBarHeight(cntxt)))/3;
relativeLayout.getLayoutParams().width=(myapp.screen_width)/3;
}
else if((myapp.screen_width == 768)||(myapp.screen_height==1280))
{
// relativeLayout.getLayoutParams().height=(myapp.screen_height-(114+General.getStatusBarHeight(cntxt)))/3;
relativeLayout.getLayoutParams().width=(myapp.screen_width)/3;
}
else if((myapp.screen_width == 540)&&(myapp.screen_height==960))
{
// relativeLayout.getLayoutParams().height=(myapp.screen_height-(80+General.getStatusBarHeight(cntxt)))/3;
relativeLayout.getLayoutParams().width=(myapp.screen_width)/3;
}
else if((myapp.screen_width == 1080)||(myapp.screen_height==1920))
{
// relativeLayout.getLayoutParams().height=(myapp.screen_height-(160+General.getStatusBarHeight(cntxt)))/3;
relativeLayout.getLayoutParams().width=(myapp.screen_width)/3;
}
}
Where relativelayout = layout which consists ImageView.
You also have to change your height & width of imageview to target
multiple resolutions as below:
put this in your imageview:
android:layout_width="@dimen/image_width"
android:layout_height="@dimen/image_height"
Put this:
In values folder values-hdpi => dimens.xml(480x800)
<dimen name="image_width">92dp</dimen>
<dimen name="image_height">92dp</dimen>
In values folder values-mdpi => dimens.xml(320x480)
<dimen name="image_width">64dp</dimen>
<dimen name="image_height">64dp</dimen>
In values folder values-sw360dp-notlong-hdpi => dimens.xml(540x960)
<dimen name="image_width">114dp</dimen>
<dimen name="image_height">114dp</dimen>
In values folder values-sw360dp-xhdpi => dimens.xml(720x1280)
<dimen name="image_width">138dp</dimen>
<dimen name="image_height">138dp</dimen>
In values folder values-sw360dp-notlong-xhdpi => dimens.xml(768x1280)
<dimen name="image_width">184dp</dimen>
<dimen name="image_height">184dp</dimen>