Search code examples
androidandroid-imageviewandroid-image

Handling Images In Custom ListView


I Have A Custom listView, and i get the data for list from JSON, The data Contains Username, PostText , PostPicture

i am having the issues with the image size as the PostPicture Has different Sizes For Different Users,

what i want is, fixed Or ( Maximum Width ) and height should be taken accordingly

My Code Is

 @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View itemView = convertView;
                if ( itemView == null)
                {
                    itemView = getLayoutInflater().inflate(R.layout.postlistview, parent,false);
                }
                Posts CurrentPost = myPosts.get(position);
                // myPosts Is the List Of Posts, Usermane, Posttext, PostImage


                ImageView imgView =(ImageView) itemView.findViewById(R.id.post_photo);

                URL url = null;
                Bitmap bmp = null;
                try {
                           url = new URL(CurrentPost.getPostPhoto());
                            bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
                        } catch (MalformedURLException e) {

                        }catch (IOException e) {

                        }
                        imgView.setImageBitmap(bmp); 

    }

Solution

  • use this and set height and width as per your requirement

    <ImageView
        android:id="@+id/image"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:scaleType="fitXY" />