Create a tableview in Android: How to define the column-width in RecyclerView with fixed header?
11:14 26 May 2026

In android java I want to implement a table view using a RecyclerView

The table should have a fixed header (that is always visible) and there should be columns with different with-s.

My current prototyp looks like this:

enter image description here TableActivity Screenshot

As you can see the widht of column "ID" is too much(1) while the widht of column "greeting" is to small.

Also the header is not exactly over the column(2)

My question: is there a way to define seperate column width-s?


I am using this xml layout activity_table.xml

    
        

            
            

            
            

        
    

with this item_table_row.xml

    
    
    
    

With this TableActivity looks like this:

    public class TableActivity extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_table);

            recyclerView = findViewById(R.id.tableRecycler);
            headerRow = findViewById(R.id.headerRow);

            TableModelApi model = null;
            try {
                model = getTableModel();
            } catch (Exception e) {
                Toast.makeText(this, e.getLocalizedMessage(), Toast.LENGTH_LONG);
                model = createSampleModel();
            }

            setupHeader(model);
            setupRecycler(model);
        }

        private void setupHeader(TableModelApi model) {
            headerRow.removeAllViews();

            String[] columns = model.getColumnNames();

            for (int i = 0; i < columns.length; i++) {
                String text = columns[i];
                TextView tv = TableHelper.createTextView(this, text, model.getColumnWidth(i));
                // tv.setTypeface(Typeface.DEFAULT_BOLD);
                headerRow.addView(tv);
            }
        }

        private void setupRecycler(TableModelApi model) {
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
            recyclerView.setAdapter(new TableAdapter(model));
        }

        public TableModelApi getTableModel() throws IOException {       
            ...     
        }

    }

and this TableAdapter

    public class TableAdapter extends RecyclerView.Adapter {
        @Override
        public RowViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_table_row, parent, false);
            return new RowViewHolder(view);
        }

        @Override
        public void onBindViewHolder(RowViewHolder holder, int position) {
            holder.bind(model, position);
        }

        @Override
        public int getItemCount() {
            return model.getRowCount();
        }

        static class RowViewHolder extends RecyclerView.ViewHolder {

            LinearLayout rowContainer;

            RowViewHolder(View itemView) {
                super(itemView);
                rowContainer = itemView.findViewById(R.id.rowContainer);
            }

            void bind(TableModelApi model, int rowIndex) {
                rowContainer.removeAllViews();

                Object[] row = model.getRow(rowIndex);

                for (int i = 0; i < row.length; i++) {
                    Object cell = row[i];
                    TextView tv = TableHelper.createTextView(itemView.getContext(), cell, model.getColumnWidth(i));

                    rowContainer.addView(tv);
                }
            }
        }
    }

The complete sourcecode is available here : https://github.com/k3b/CsvViewer/tree/TableActivityDemo/app

java android android-recyclerview column-width