2

I'm developing a android app to show some movies in a scrollable view with two columns.

Everything is okay except that I have huge space between rows.

I have tried many things that I found in StackOverflow, like: android:adjustViewBounds="true", set padding to zero and etc.

Can anyone give enlighten me?

Here is how the app is looking like

As you can see there is a huge space between rows

I'm adding the imageViews dynamically through inflate, and I think thats the issue.

Here is my code:

package com.example.androidvidly

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.squareup.picasso.Picasso
import org.json.JSONArray
import org.json.JSONObject

const val MOVIE_OBJECT_ID = "tokenlab.com.MOVIE_OBJECT_ID"
const val MOVIE_OBJECT_VOTES = "tokenlab.com.MOVIE_OBJECT_VOTES"
const val MOVIE_OBJECT_TITLE = "tokenlab.com.MOVIE_OBJECT_TITLE"
const val MOVIE_OBJECT_POSTER_URL = "tokenlab.com.MOVIE_OBJECT_POSTER_URL"
const val MOVIE_OBJECT_RELEASE_DATE = "tokenlab.com.MOVIE_OBJECT_RELEASE_DATE"
const val MOVIE_OBJECT_GENRES = "tokenlab.com.MOVIE_OBJECT_GENRES"

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val mQueue = Volley.newRequestQueue(this)
        val url = "https://desafio-mobile.nyc3.digitaloceanspaces.com/movies" //API provided
        //val url = "http://10.0.2.2:3000"// Node.js Backend
        val gallery = findViewById<LinearLayout>(R.id.gallery)
        val inflater = LayoutInflater.from(this)
        var mAppName = findViewById<TextView>(R.id.appName)
        mAppName.text="Vidly"
        val gson = Gson()
        var request = StringRequest(Request.Method.GET, url, Response.Listener { response ->
            var moviesJSONArray = JSONArray(response)
            for (i in 0 until moviesJSONArray.length()) {
                var e : JSONObject = moviesJSONArray.getJSONObject(i)
                //Initializing the object
                val arrayStringType = object : TypeToken<ArrayList<String>>() {}.type
                var genreListAux:ArrayList<String> = gson.fromJson(e.getString("genres"), arrayStringType)
                var movieObject: Movie = Movie(e.getString("id").toInt(), e.getString("vote_average").toDouble(), e.getString("title"), e.getString("poster_url"), genreListAux, e.getString("release_date"))
                if (i==0) {
                    var mImageView = findViewById<ImageView>(R.id.imageDisplay)
                    mImageView.setTag(movieObject)
                    loadImageFromUrl(movieObject.poster_url, mImageView)
                    mImageView.setPadding(0, 0, 0, 0);
                }
                else if (i==1) {
                    var mImageView = findViewById<ImageView>(R.id.imageDisplay2)
                    mImageView.setTag(movieObject)
                    loadImageFromUrl(movieObject.poster_url, mImageView)
                    mImageView.setPadding(0, 0, 0, 0);
                }
                else{
                    if(i.rem(2)==0) {
                        var view = inflater.inflate(R.layout.activity_main, gallery, false)
                        var mImageView = view.findViewById<ImageView>(R.id.imageDisplay)
                        mImageView.setTag(movieObject)
                        var height = gallery.height
                        view.minimumHeight=height
                        gallery.addView(view)
                        loadImageFromUrl(movieObject.poster_url, mImageView)
                        mImageView.setPadding(0, 0, 0, 0);
                    }
                    else {
                        var view = gallery.getChildAt(gallery.childCount-1)
                        var mImageView = view.findViewById<ImageView>(R.id.imageDisplay2)
                        mImageView.setTag(movieObject)
                        loadImageFromUrl(movieObject.poster_url, mImageView)
                        mImageView.setPadding(0, 0, 0, 0);
                    }
                }

            }

        }, Response.ErrorListener { error ->
            error.printStackTrace()
        })
        mQueue.add(request)
    }

    fun showMore(view: View) {
        val intent = Intent(this, MovieSelectedActivity::class.java).apply {
            var movieObj : Movie = view.getTag() as Movie
            putExtra(MOVIE_OBJECT_ID, movieObj.id)
            putExtra(MOVIE_OBJECT_GENRES, movieObj.genres)
            putExtra(MOVIE_OBJECT_POSTER_URL, movieObj.poster_url)
            putExtra(MOVIE_OBJECT_RELEASE_DATE, movieObj.release_date)
            putExtra(MOVIE_OBJECT_TITLE, movieObj.title)
            putExtra(MOVIE_OBJECT_VOTES, movieObj.vote_average)
        }
        startActivity(intent)
    }
    fun loadImageFromUrl(url: String, mImageView: ImageView) {
        Picasso.with(this).load(url).placeholder(R.mipmap.ic_launcher)
            .error(R.mipmap.ic_launcher)
            .into(
                mImageView, null
            )
    }
}

Here is the xml for the views:

    <?xml version="1.0" encoding="utf-8"?>
<ScrollView 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="wrap_content"
    android:layout_height="wrap_content"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/gallery"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/background"
        android:orientation="vertical"
        android:weightSum="10"
        android:adjustViewBounds="true">

        <RelativeLayout
            android:layout_weight="2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:adjustViewBounds="true">


            <TextView
                android:id="@+id/appName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:textAppearance="@style/TextAppearance.AppCompat.Body2"
                android:textColor="#000000"
                android:textSize="60dp"
                android:adjustViewBounds="true"/>
        </RelativeLayout>
        <RelativeLayout
            android:layout_weight="8"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:adjustViewBounds="true">
                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:layout_weight="1"
                    android:layout_alignParentLeft="true"
                    android:adjustViewBounds="true">

                    <ImageView
                        android:id="@+id/imageDisplay"
                        android:layout_width="170dp"
                        android:layout_height="280dp"
                        android:clickable="true"
                        android:onClick="showMore"
                        android:adjustViewBounds="true"/>
                </LinearLayout>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:layout_weight="1"
                    android:layout_alignParentRight="true"
                    android:adjustViewBounds="true">

                    <ImageView
                        android:id="@+id/imageDisplay2"
                        android:layout_width="170dp"
                        android:layout_height="280dp"
                        android:clickable="true"
                        android:onClick="showMore"
                        android:adjustViewBounds="true"/>
                </LinearLayout>

        </RelativeLayout>

    </LinearLayout>




</ScrollView>
Acroyear
  • 1,354
  • 3
  • 22
  • 37
  • Your code seems to be very complicated for not much. You should try with a recyclerview instead of this; Have a look at this : https://stackoverflow.com/questions/40587168/simple-android-grid-example-using-recyclerview-with-gridlayoutmanager-like-the – Kulbuto Jan 07 '20 at 13:22
  • Thank you very much , I'll definitely look into that. – Daniel De Marco Fucci Jan 07 '20 at 22:07

1 Answers1

0

I Found out that the issue was with the background I was setting in the main linear layout. After removing it the huge spaces disappeared.

android:background="@drawable/background"