2

i am making gallery but i am stuck with one problem here is a view of one device which is normal screen 320x480 link and device 2 with have higher screen resolution 720x1280 link i want to high resolution device have same view as normal screen. is there any option to make LayoutParams if is screen x-large to be like new imageView.setLayoutParams(GridView.LayoutParams(200, 200)); or more? how to do that?

public View getView(int position, View convertView, ViewGroup parent) {         
    ImageView imageView = new ImageView(mContext);
    imageView.setImageResource(mThumbIds[position]);
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
    return imageView;
}
user2672165
  • 2,986
  • 19
  • 27
BM84-M1
  • 111
  • 14

1 Answers1

1

you can do so directly using different values folder and put dimen.xml inside values folder and provide dimension as you want to apply on layout params.

for example for devices with hdpi resolution: make values-hdpi folder --> add dimen.xml inside dimen.xml add dimension like

<dimen name="gridview_size">100dp</dimen>

now yo just need to change only one line in your code and that is

int dimen = (int) (getResources().getDimensionPixelSize(R.dimen.gridview_size)/getResources().getDisplayMetrics().density);
imageView.setLayoutParams(new GridView.LayoutParams(dimen, dimen));

thats it you are done for 720x1280 device use values-xhdpi/values-sw360dp-xhdpi folder

Maulik.J
  • 636
  • 1
  • 6
  • 14
  • you mean like this? i am getting error :S public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView = new ImageView(mContext); imageView.setImageResource(mThumbIds[position]); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); int dimen = (int) (getResources().getDimensionPixelSize(R.dimen.gridview_size)/getResources().getDisplayMetrics().density); imageView.setLayoutParams(new GridView.LayoutParams(dimen, dimen)); return imageView; } } – BM84-M1 Jan 05 '14 at 15:15
  • you need to set layout params before setting image to imageview. can you tell me which error you getting? – Maulik.J Jan 05 '14 at 16:31
  • ok you need to use like this int dimen = (int) (context.getResources().getDimensionPixelSize(R.dimen.gridview_size)/context.getResources().getD‌​isplayMetrics().density); – Maulik.J Jan 05 '14 at 17:09
  • not work again here is what is the problem now :S [link](http://i.imgur.com/8kHbaXR.jpg?1) – BM84-M1 Jan 05 '14 at 17:20
  • i have checked its working. may be you have mistaken some where have you passed correct context check [link](http://imgur.com/CRzkvI7) – Maulik.J Jan 05 '14 at 17:39