How to fix gridview item sizes (width and height) when I put an image on view item in Gridview

10,678

You can set LayoutParams with fixed size to avoid resizing the view

LayoutParams params = new LayoutParams(width, height);
btn.setLayoutParams(params);

if you don't set LayoutParams the size will be affected by background resource size.

to get more information visit: https://stackoverflow.com/a/6170160/1891878

Share:
10,678
Mahmoud
Author by

Mahmoud

Updated on June 04, 2022

Comments

  • Mahmoud
    Mahmoud almost 2 years

    I have a gridview that has multi items (buttons) I bind background for each item in my adapter

    public View getView(int position, View convertView, ViewGroup parent) {
            Button btn;
            if (convertView == null) 
            { // if it's not recycled, initialize some attributes
                btn = new Button(mContext);
                KeypadButton keypadButton = mButtons[position];
                btn.setTag(keypadButton);
                btn.setBackgroundResource(R.drawable.button_image);
                btn.setTextColor(android.R.color.white);
            } 
            else 
            {
                btn = (Button) convertView;
            }
            btn.setText(mButtons[position].GetContent());
            return btn;
        }
    

    the imageview item change it's size when I setBackgroundResource , Is it way to avoid resizing for image view item .