Search code examples
androidhorizontalscrollview

HorizontalScrollView adds space between views


I have HorizontalScrollView and the problem is that after I add views, I have blank spaces between these views.. Here is a pic:
enter image description here

Here is the code of the class:

public class HomeFeatureLayout extends HorizontalScrollView {
    public HomeFeatureLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public HomeFeatureLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public HomeFeatureLayout(Context context) {
        super(context);
    }

    public void setFeatureItems() {
        LinearLayout internalWrapper = new LinearLayout(getContext());
        internalWrapper.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        internalWrapper.setOrientation(LinearLayout.HORIZONTAL);
        addView(internalWrapper);
        // this.mItems = items;
        for (int i = 0; i < 10; i++) {
            ImageView im = new ImageView(getContext());
            im.setImageResource(R.drawable.aaaa);
            im.setScaleType(ScaleType.CENTER_INSIDE);
            im.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));


            internalWrapper.addView(im);
        }
    }
}

And MainActivity:

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        HomeFeatureLayout home = new HomeFeatureLayout(this);
        home.setFeatureItems();
        home.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

        setContentView(home);

    }

Why are there blank spaces between each 2 images?


Solution

  • I had to create my own view and extend ImageView, then override OnMeasure method, with the help of THIS question:

    public class myImageView extends ImageView {
    
        public myImageView(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            MeasureSpec.getSize(widthMeasureSpec);
            MeasureSpec.getSize(heightMeasureSpec);
            setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec));
    
            setLayoutParams(new LinearLayout.LayoutParams(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec)));
    
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    
        public int measureWidth(int measureSpec) {
            int result = 0;
            int specMode = MeasureSpec.getMode(measureSpec);
            int specSize = MeasureSpec.getSize(measureSpec);
            Display display = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
            int screenWidth = display.getWidth();
    
            if (specMode == MeasureSpec.EXACTLY)
                // We were told how big to be
                result = specSize;
            else {
                // Measure the view
                result = screenWidth;
                if (specMode == MeasureSpec.AT_MOST)
                    // Respect AT_MOST value if that was what is called for by measureSpec
                    result = Math.min(result, specSize);
            }
    
            return result;
        }
    
        public int measureHeight(int measureSpec) {
            int result = 0;
            int specMode = MeasureSpec.getMode(measureSpec);
            int specSize = MeasureSpec.getSize(measureSpec);
            Display display = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
            int screenHeight = display.getHeight();
    
            if (specMode == MeasureSpec.EXACTLY)
                // We were told how big to be
                result = specSize;
            else {
                // Measure the view
                result = screenHeight;
                if (specMode == MeasureSpec.AT_MOST)
                    // Respect AT_MOST value if that was what is called for by measureSpec
                    result = Math.min(result, specSize);
            }
    
            return result;
        }
    
    }