'Need list with column headers and different column widths on Android Studio

Like this:

Code     Product Description                 Stock    Tel  
P1234    Portland Cement 25kg                70       0208 1234567  
P4321    MGM Sharp Sand Bulk Bag 800kg       3        0799 98765433  

The variable-length product description may be up to 45 characters.

Will use the wider horizontal display on the mobile for this.

I checked and found gridview does not support different-width columns. From what I understand it may be the same for listview.

Is there a way without using x,y for positioning the columns?



Solution 1:[1]

You can try this with a GridLayout or (possibly) with a TableLayout.

There is also GridView, but as far as I know every cell in a GridView must have the same size with every other. I don't know if a cell could span multiple rows/columns in a GridView though, but even if it can and since every cell must be the same size, then you would have to calculate how many cells your data would require for each entry. So I would suggest going with the previous 2 options.

Finally, RecyclerViews can arrange Views in a grid (via GridLayoutManager), but I am not aware if it can fit your requirements (such as variable size for each cell), so I am just leaving it here in case you want to look at it as another possible option.

Here is an example implementation with a GridLayout:

import android.content.Context;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.GridLayout;
import android.widget.HorizontalScrollView;
import android.widget.ScrollView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private static void addTextViews(final ViewGroup viewGroup,
                                     final String... text) {
        final Context ctx = viewGroup.getContext();
        for (final String t: text) {
            final TextView view = new TextView(ctx);
            view.setText(t);
            viewGroup.addView(view);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final GridLayout viewGroup = new GridLayout(this);

        //Configure grid:
        viewGroup.setColumnCount(4); //Wrap every 4 columns.
        viewGroup.setUseDefaultMargins(true); //Insert default space between cells.
        viewGroup.setAlignmentMode(GridLayout.ALIGN_BOUNDS); //Required by 'setUseDefaultMargins(true)' documentation.

        //Populate the grid:
        addTextViews(viewGroup, "Code", "Product Description", "Stock", "Tel");
        addTextViews(viewGroup, "P1234", "Portland Cement 25kg", "70", "0208 1234567");
        addTextViews(viewGroup, "P4321", "MGM Sharp Sand Bulk Bag 800kg", "3", "0799 98765433");
        for (int i = 0; i < 40; ++i)
            addTextViews(viewGroup, Integer.toString(i), "MGM Sharp Sand Bulk Bag 800kg......", Integer.toOctalString(5 * i), "0799 98765433");

        //Add the grid to scroll views:
        final HorizontalScrollView horizontalScrollView = new HorizontalScrollView(this);
        horizontalScrollView.addView(viewGroup);
        final ScrollView verticalScrollView = new ScrollView(this);
        verticalScrollView.addView(horizontalScrollView);

        setContentView(verticalScrollView);
    }
}

The TextViews are added to the grid starting from the top-left cell and going to the right until 4 columns are filled, in which case the next TextViews will go in subsequent rows following the same pattern. This behaviour is because of the combination of horizontal orientation (which is the default value for the GridLayout), together with setting the column count to 4. By default, each cell spans a single row and a single column.

The GridLayout does not have built in support for scrolling, but you can add it to both a HorizontalScrollView and a vertical ScrollView, as demonstrated in the above sample code, in order to enable scrolling.

You can always look more information about those ViewGroups in their documentation.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 gthanop