Android Button Resize Issue

I have 4 buttons that I want to show in gridview. I want them to fill the screen as much as possible, but I never did. I have tried all kinds of ScaleTypes and it will not work. Right now I have a scale type in FIT_CENTER, but I get an unexpected result. Here is the code that I still have:

Main.java

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Display screenSize = getWindowManager().getDefaultDisplay();
        int screenWidth = screenSize.getWidth();
        int screenHeight = screenSize.getHeight();

        int iconWidth =  screenWidth/4;
        int iconHeight=  screenHeight/2;

        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new ImageAdapter(this, iconWidth, iconHeight));

        }

ImageAdapter.java

public View getView(final int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            imageView = new ImageView(m_Context);
            imageView.setLayoutParams(new GridView.LayoutParams(m_width, m_height));
            imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            imageView.setPadding(8, 8, 8, 8);

        } else {
            imageView = (ImageView) convertView;
        }
        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }

GridViewLayout.xml

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:columnWidth="90dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
    android:numColumns="2"
    android:background="#FFFFFF"/>

The result of the code is as follows:

wrong result

Here is what I would like to look like this:

desired result

Please note that the last screenshot is hardcoded, which is obviously not my desire. I want this to work in every screen size without hard coding.

+3
source share
1 answer

, GridLayoutManager. , . .

0

Source: https://habr.com/ru/post/1789198/


All Articles