How to set layout options in imageView inside GridView in Android

I am new to android, and I met this tutorial on the official android website on how to create GridView images. I use kotlin as the language of choice. In the adapter class that extends the BaseAdapter , I try to set the image layout options as indicated in the tutorial, but I get the GridView.LayoutParams error message. GridView.LayoutParams is an unresolved link. Here is the complete code for the class:

 class ImageAdapter(val context: Context) : BaseAdapter() { // references to our images val items: IntArray = intArrayOf(R.drawable.cat, R.drawable.daft_punk, R.drawable.gun, R.drawable.harvestors, R.drawable.intel, R.drawable.maserati, R.drawable.porsche, R.drawable.serveroom, R.drawable.spiderman, R.drawable.watch_dog) override fun getItem(p0: Int): Any { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } override fun getItemId(p0: Int): Long { return 0 } override fun getCount(): Int { return items.size } // create a new ImageView for each item referenced by the Adapter override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View { var imageView: ImageView if (p1 == null) { // if it not recycled, initialize some attributes imageView= ImageView(context) imageView.layoutParams= GridView.LayoutParams(85,85) imageView.scaleType = ImageView.ScaleType.CENTER_CROP imageView.setPadding(8, 8, 8, 8) } else { imageView=p1 as ImageView } imageView.setImageResource(items[p0]) return imageView } } 

The error is in the getView method in the line imageView.layoutParams=GridView.LayoutParams(85,85) . LayoutParams is in red, which means an error, and when I find it, I get a message:

Unauthorized link: LayoutParams

How do I install these LayoutParams ? Because without them, the images in the GridView displayed at their full height, and I need a small image just like the gallery application displays them on our phones.

Follow up question:

When I replace imageView.layoutParams=GridView.LayoutParams(85,85) with imageView.layoutParams=AbsListView.LayoutParams(85,85) , I get javaOutOfMemory error. The following is a complete stack trace:

 08-16 06:58:11.412 4543-4543/gallerie.classmite.com.imageviewer E/dalvikvm-heap: Out of memory on a 18662416-byte allocation. 08-16 06:58:11.431 4543-4543/gallerie.classmite.com.imageviewer E/AndroidRuntime: FATAL EXCEPTION: main Process: gallerie.classmite.com.imageviewer, PID: 4543 java.lang.OutOfMemoryError at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609 at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444) at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840) at android.content.res.Resources.loadDrawable(Resources.java:2166) at android.content.res.Resources.getDrawable(Resources.java:710) at android.widget.ImageView.resolveUri(ImageView.java:638) at android.widget.ImageView.setImageResource(ImageView.java:367) at gallerie.classmite.com.imageviewer.ImageAdapter.getView(ImageAdapter.kt:32) at android.widget.AbsListView.obtainView(AbsListView.java:2372) at android.widget.GridView.makeAndAddView(GridView.java:1345) at android.widget.GridView.makeRow(GridView.java:345) at android.widget.GridView.fillDown(GridView.java:287) at android.widget.GridView.fillFromTop(GridView.java:421) at android.widget.GridView.layoutChildren(GridView.java:1233) at android.widget.AbsListView.onLayout(AbsListView.java:2166) at android.view.View.layout(View.java:15127) at android.view.ViewGroup.layout(ViewGroup.java:4862) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1888) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1742) at android.widget.LinearLayout.onLayout(LinearLayout.java:1651) at android.view.View.layout(View.java:15127) at android.view.ViewGroup.layout(ViewGroup.java:4862) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515) at android.widget.FrameLayout.onLayout(FrameLayout.java:450) at android.view.View.layout(View.java:15127) at android.view.ViewGroup.layout(ViewGroup.java:4862) at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:374) at android.view.View.layout(View.java:15127) at android.view.ViewGroup.layout(ViewGroup.java:4862) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515) at android.widget.FrameLayout.onLayout(FrameLayout.java:450) at android.view.View.layout(View.java:15127) at android.view.ViewGroup.layout(ViewGroup.java:4862) at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2315) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2021) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1189) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6221) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:788) at android.view.Choreographer.doCallbacks(Choreographer.java:591) at android.view.Choreographer.doFrame(Choreographer.java:560) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:774) at android.os.Handler.handleCallback(Handler.java:808) at android.os.Handler.dispatchMessage(Handler.java:103) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:5315) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645) at dalvik.system.NativeStart.main(Native Method) 

The error occurs only when the application is launched on the phone, but when I launch the application in the emulator, it works flawlessly after I made the replacement above.

+3
source share
2 answers

You can try:

 imageView.layoutParams = AbsListView.LayoutParams(85, 85) 

AbsListView.LayoutParams used in the GridView when retrieving children of LayoutParams .

Since a static member can be inherited by a subclass in Java . When you call GridView.LayoutParams , it is actually AbsListView.LayoutParams . However, in Kotlin you cannot do the same. Outside of a subclass, you can only reference members of a companion superclass object using the name of the superclass.

Updates for the next question:

An OutOfMemory error indicates that you are loading a large image in an ImageView . To avoid this, you should reduce the image to display it, referring to this tutorial . You can also use some image upload frames, such as Glide , to display your image.

Recommended reading: Strange memory issue when loading image into Bitmap object

+3
source

Do not set layout options as you did

correct path imageParams = (GridView.LayoutParams) imageView.getLayoutParams ();

in the line above, "GridView" should be replaced by the parent view of your imageView

Happy coding .. :)

0
source

Source: https://habr.com/ru/post/1271299/


All Articles