GridView isn't creating Squares












1















I want to create a simple android wallpaper app just for practice.
So I added pictures to an assets folder, and I want to show them in a gridview.
I created it, but for some reason the pictures in the gridview doesn't appear in squares rather in a large height format.
I'm attaching my code if anyone can see where the problem is:



this is how it looks like:



enter image description here



you can see that it creates rectangles instead of equal squares.



MainActivity:



@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridView = (GridView)findViewById(R.id.gridview);
gridView.setAdapter(new ImageAdapter(this));
}


ImageAdapter:



public class ImageAdapter extends BaseAdapter {
private Context context;
private String list;

public ImageAdapter(Context c) {
context = c;
try {
list = context.getAssets().list("imgs");
} catch (IOException e) {
e.printStackTrace();
}
}

public int getCount() {return list.length;}
public Object getItem(int position) {return null;}
public long getItemId(int position) {return 0;}

public View getView(int position, View convertView, ViewGroup parent) {
ImageView img;
if (convertView == null) {
img = new ImageView(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
img.setLayoutParams(new GridView.LayoutParams(params));
img.setScaleType(ImageView.ScaleType.CENTER);
img.setPadding(8, 8, 8, 8);
} else {
img = (ImageView) convertView;
}
try {
InputStream ims = context.getAssets().open("imgs/" + list[position]);
Bitmap bitmap = BitmapFactory.decodeStream(ims);
img.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
return img;
}
}


and this is the xml layout:



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

<GridView
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
android:stretchMode="columnWidth"
android:numColumns="2"/>

</android.support.constraint.ConstraintLayout>









share|improve this question

























  • Your image size is not fixed even GridView is not the best approach try using RecyclerView with GridLayoutManager refer this stackoverflow.com/questions/40587168/…

    – Akshay Katariya
    Dec 31 '18 at 9:49













  • Change LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); to LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(item_size, item_size);

    – Brian Hoang
    Dec 31 '18 at 9:57











  • @AkshayKatariya it is not about RecycleView or GridView

    – Brian Hoang
    Dec 31 '18 at 9:58
















1















I want to create a simple android wallpaper app just for practice.
So I added pictures to an assets folder, and I want to show them in a gridview.
I created it, but for some reason the pictures in the gridview doesn't appear in squares rather in a large height format.
I'm attaching my code if anyone can see where the problem is:



this is how it looks like:



enter image description here



you can see that it creates rectangles instead of equal squares.



MainActivity:



@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridView = (GridView)findViewById(R.id.gridview);
gridView.setAdapter(new ImageAdapter(this));
}


ImageAdapter:



public class ImageAdapter extends BaseAdapter {
private Context context;
private String list;

public ImageAdapter(Context c) {
context = c;
try {
list = context.getAssets().list("imgs");
} catch (IOException e) {
e.printStackTrace();
}
}

public int getCount() {return list.length;}
public Object getItem(int position) {return null;}
public long getItemId(int position) {return 0;}

public View getView(int position, View convertView, ViewGroup parent) {
ImageView img;
if (convertView == null) {
img = new ImageView(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
img.setLayoutParams(new GridView.LayoutParams(params));
img.setScaleType(ImageView.ScaleType.CENTER);
img.setPadding(8, 8, 8, 8);
} else {
img = (ImageView) convertView;
}
try {
InputStream ims = context.getAssets().open("imgs/" + list[position]);
Bitmap bitmap = BitmapFactory.decodeStream(ims);
img.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
return img;
}
}


and this is the xml layout:



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

<GridView
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
android:stretchMode="columnWidth"
android:numColumns="2"/>

</android.support.constraint.ConstraintLayout>









share|improve this question

























  • Your image size is not fixed even GridView is not the best approach try using RecyclerView with GridLayoutManager refer this stackoverflow.com/questions/40587168/…

    – Akshay Katariya
    Dec 31 '18 at 9:49













  • Change LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); to LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(item_size, item_size);

    – Brian Hoang
    Dec 31 '18 at 9:57











  • @AkshayKatariya it is not about RecycleView or GridView

    – Brian Hoang
    Dec 31 '18 at 9:58














1












1








1








I want to create a simple android wallpaper app just for practice.
So I added pictures to an assets folder, and I want to show them in a gridview.
I created it, but for some reason the pictures in the gridview doesn't appear in squares rather in a large height format.
I'm attaching my code if anyone can see where the problem is:



this is how it looks like:



enter image description here



you can see that it creates rectangles instead of equal squares.



MainActivity:



@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridView = (GridView)findViewById(R.id.gridview);
gridView.setAdapter(new ImageAdapter(this));
}


ImageAdapter:



public class ImageAdapter extends BaseAdapter {
private Context context;
private String list;

public ImageAdapter(Context c) {
context = c;
try {
list = context.getAssets().list("imgs");
} catch (IOException e) {
e.printStackTrace();
}
}

public int getCount() {return list.length;}
public Object getItem(int position) {return null;}
public long getItemId(int position) {return 0;}

public View getView(int position, View convertView, ViewGroup parent) {
ImageView img;
if (convertView == null) {
img = new ImageView(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
img.setLayoutParams(new GridView.LayoutParams(params));
img.setScaleType(ImageView.ScaleType.CENTER);
img.setPadding(8, 8, 8, 8);
} else {
img = (ImageView) convertView;
}
try {
InputStream ims = context.getAssets().open("imgs/" + list[position]);
Bitmap bitmap = BitmapFactory.decodeStream(ims);
img.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
return img;
}
}


and this is the xml layout:



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

<GridView
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
android:stretchMode="columnWidth"
android:numColumns="2"/>

</android.support.constraint.ConstraintLayout>









share|improve this question
















I want to create a simple android wallpaper app just for practice.
So I added pictures to an assets folder, and I want to show them in a gridview.
I created it, but for some reason the pictures in the gridview doesn't appear in squares rather in a large height format.
I'm attaching my code if anyone can see where the problem is:



this is how it looks like:



enter image description here



you can see that it creates rectangles instead of equal squares.



MainActivity:



@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridView = (GridView)findViewById(R.id.gridview);
gridView.setAdapter(new ImageAdapter(this));
}


ImageAdapter:



public class ImageAdapter extends BaseAdapter {
private Context context;
private String list;

public ImageAdapter(Context c) {
context = c;
try {
list = context.getAssets().list("imgs");
} catch (IOException e) {
e.printStackTrace();
}
}

public int getCount() {return list.length;}
public Object getItem(int position) {return null;}
public long getItemId(int position) {return 0;}

public View getView(int position, View convertView, ViewGroup parent) {
ImageView img;
if (convertView == null) {
img = new ImageView(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
img.setLayoutParams(new GridView.LayoutParams(params));
img.setScaleType(ImageView.ScaleType.CENTER);
img.setPadding(8, 8, 8, 8);
} else {
img = (ImageView) convertView;
}
try {
InputStream ims = context.getAssets().open("imgs/" + list[position]);
Bitmap bitmap = BitmapFactory.decodeStream(ims);
img.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
return img;
}
}


and this is the xml layout:



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

<GridView
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
android:stretchMode="columnWidth"
android:numColumns="2"/>

</android.support.constraint.ConstraintLayout>






android gridview






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 31 '18 at 12:49









Fantômas

32.6k156389




32.6k156389










asked Dec 31 '18 at 9:44









TheDragonerTheDragoner

63210




63210













  • Your image size is not fixed even GridView is not the best approach try using RecyclerView with GridLayoutManager refer this stackoverflow.com/questions/40587168/…

    – Akshay Katariya
    Dec 31 '18 at 9:49













  • Change LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); to LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(item_size, item_size);

    – Brian Hoang
    Dec 31 '18 at 9:57











  • @AkshayKatariya it is not about RecycleView or GridView

    – Brian Hoang
    Dec 31 '18 at 9:58



















  • Your image size is not fixed even GridView is not the best approach try using RecyclerView with GridLayoutManager refer this stackoverflow.com/questions/40587168/…

    – Akshay Katariya
    Dec 31 '18 at 9:49













  • Change LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); to LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(item_size, item_size);

    – Brian Hoang
    Dec 31 '18 at 9:57











  • @AkshayKatariya it is not about RecycleView or GridView

    – Brian Hoang
    Dec 31 '18 at 9:58

















Your image size is not fixed even GridView is not the best approach try using RecyclerView with GridLayoutManager refer this stackoverflow.com/questions/40587168/…

– Akshay Katariya
Dec 31 '18 at 9:49







Your image size is not fixed even GridView is not the best approach try using RecyclerView with GridLayoutManager refer this stackoverflow.com/questions/40587168/…

– Akshay Katariya
Dec 31 '18 at 9:49















Change LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); to LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(item_size, item_size);

– Brian Hoang
Dec 31 '18 at 9:57





Change LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); to LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(item_size, item_size);

– Brian Hoang
Dec 31 '18 at 9:57













@AkshayKatariya it is not about RecycleView or GridView

– Brian Hoang
Dec 31 '18 at 9:58





@AkshayKatariya it is not about RecycleView or GridView

– Brian Hoang
Dec 31 '18 at 9:58












1 Answer
1






active

oldest

votes


















0














@TheDragoner Your gridview is using Wrap Content as its Layout Params i.e. LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);



As a result it the grid takes the actual image's height and width.
Change LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(getResources().getDimensionPixelOffset(R.dimen.dp_100), getResources().getDimensionPixelOffset(R.dimen.dp_100));






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53985937%2fgridview-isnt-creating-squares%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    @TheDragoner Your gridview is using Wrap Content as its Layout Params i.e. LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);



    As a result it the grid takes the actual image's height and width.
    Change LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(getResources().getDimensionPixelOffset(R.dimen.dp_100), getResources().getDimensionPixelOffset(R.dimen.dp_100));






    share|improve this answer




























      0














      @TheDragoner Your gridview is using Wrap Content as its Layout Params i.e. LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);



      As a result it the grid takes the actual image's height and width.
      Change LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(getResources().getDimensionPixelOffset(R.dimen.dp_100), getResources().getDimensionPixelOffset(R.dimen.dp_100));






      share|improve this answer


























        0












        0








        0







        @TheDragoner Your gridview is using Wrap Content as its Layout Params i.e. LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);



        As a result it the grid takes the actual image's height and width.
        Change LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(getResources().getDimensionPixelOffset(R.dimen.dp_100), getResources().getDimensionPixelOffset(R.dimen.dp_100));






        share|improve this answer













        @TheDragoner Your gridview is using Wrap Content as its Layout Params i.e. LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);



        As a result it the grid takes the actual image's height and width.
        Change LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(getResources().getDimensionPixelOffset(R.dimen.dp_100), getResources().getDimensionPixelOffset(R.dimen.dp_100));







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 31 '18 at 10:54









        Aman RawatAman Rawat

        1678




        1678






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53985937%2fgridview-isnt-creating-squares%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas