Giter Site home page Giter Site logo

Change Cell Text Color about tableview HOT 6 OPEN

evrencoskun avatar evrencoskun commented on May 21, 2024
Change Cell Text Color

from tableview.

Comments (6)

evrencoskun avatar evrencoskun commented on May 21, 2024 1

Hi @schlaincher @azisoli,

I reopened this issue. Becuase, both of you has the same need.

In your TableView, if Each cell item can have a different background color, then you need to create an instance for background color on your Cell Model class.

For example;

public class Cell {
   private int mBackground = -1;
   ...

  public void setBackgroundColor(int backgroundColor) {
        mBackground = backgroundColor;
    }

    public int getBackgroundColor() {
        return mBackground;
    }
}

Each cell view holder needs to take its cell model on onBindCellViewHolder of your TableViewAdapter.

@Override
    public void onBindCellViewHolder(AbstractViewHolder holder, Object cellItemModel, int
            columnPosition, int rowPosition) {
           
            // Get the holder to update cell item
            CellViewHolder viewHolder = (CellViewHolder) holder;
            // bind the cell view holder
            viewHolder.setCell(cell);
}

However, even if you change the color of the item, TableView also changes the color considering to Selection state. So How can we handle this situation?

public class CellViewHolder extends AbstractViewHolder {

    public final TextView cell_textview;
    public final LinearLayout cell_container;
    private Cell cell;

    public CellViewHolder(View itemView) {
        super(itemView);
        cell_textview = (TextView) itemView.findViewById(R.id.cell_data);
        cell_container = (LinearLayout) itemView.findViewById(R.id.cell_container);
    }

    public void setCell(Cell cell) {
        this.cell = cell;
        // Change the text of the item
        cell_textview.setText(String.valueOf(cell.getData()));

        // If your TableView should have auto resize for cells & columns.
        // Then you should consider the below lines. Otherwise, you can ignore them.

        // It is necessary to remeasure itself.
        cell_container.getLayoutParams().width = LinearLayout.LayoutParams.WRAP_CONTENT;
        cell_textview.requestLayout();
    }

    @Override
    public void setSelected(SelectionState p_nSelectionState) {
        super.setSelected(p_nSelectionState);

        if (p_nSelectionState != SelectionState.SELECTED) {
            // Change the background color
            setBackgroundColor(cell.getBackgroundColor());
        }
    }
}

As you can see the above code line we have an override method setSelected(SelectionState p_nSelectionState) of the AbstractViewHolderclass.

Thanks to setSelected method, It can be control considering to it selection state. So, even if TableView wants to set the default background color, you can set the desired color like below.

In my case, I set the color on long press action like this.

@Override
    public void onCellLongPressed(@NonNull RecyclerView.ViewHolder cellView, final int column,
                                  int row) {
        // Get the cell item model
        Cell cell = (Cell) mTableView.getAdapter().getCellItem(column, row);
        // Change the color;
        cell.setBackgroundColor(ContextCompat.getColor(mContext, R.color.colorAccent));
        // Change the cell item model
        mTableView.getAdapter().changeCellItem(column, row, cell);
    }

And this is the result;

69_issue

How can I have the zebra pattern on my table?

You can set any zebra pattern that you want using the below way.

For example; I have set the background color for my dummy list like this;

private List<List<Cell>> getSimpleCellList() {
       int bgColor;
       List<List<Cell>> list = new ArrayList<>();
       for (int i = 0; i < ROW_SIZE; i++) {
           List<Cell> cellList = new ArrayList<>();
           
           // Use the zebra background color for even numbers
           if (i % 2 == 0) {
               bgColor = ContextCompat.getColor(mContext, R.color.cell_zebra_background_color);
           } else {
               bgColor = ContextCompat.getColor(mContext, R.color.cell_background_color);
           }

           for (int j = 0; j < COLUMN_SIZE; j++) {
               String text = "cell " + j + " " + i;
               if (j % 4 == 0 && i % 5 == 0) {
                   text = "large cell " + j + " " + i + ".";
               }
               String id = j + "-" + i;

               Cell cell = new Cell(id, text, bgColor);
               cellList.add(cell);
           }
           list.add(cellList);
       }

       return list;
   }

69_2_fix

from tableview.

evrencoskun avatar evrencoskun commented on May 21, 2024

Hi @schlaincher,

If you need to change text color considering the selection state, there is a simple way that I can show you.

AbstractViewHolder that is the superclass of your cell holder has a protected function that is setSelected. You can just override it like this.

@Override
    public void setSelected(SelectionState selectionState) {
        super.setSelected(selectionState);
        if (selectionState == SelectionState.SELECTED) {
            cell_textview.setTextColor( mySelectedTextColor);
        } else {
            cell_textview.setTextColor(ContextCompat.getColor(cell_textview.getContext(), R.color
                    .unselected_text_color));
        }
    }

You can also check the sample app that has the same control.

Have a nice day!

from tableview.

skacuk avatar skacuk commented on May 21, 2024

Thanks. It works perfect. But now, I have a new question. How can I have zebra pattern with my table? Do you have a solution for this?

from tableview.

thepoojam avatar thepoojam commented on May 21, 2024

I have problem with Zebra pattern with my table, its not working.
Please provide source code of zebra Pattern.

Thanks..

from tableview.

evrencoskun avatar evrencoskun commented on May 21, 2024

Hi @poojamantri93 please check #106

from tableview.

priyadharsinipri avatar priyadharsinipri commented on May 21, 2024

@evrencoskun i want to add propertychangelistner for each cell that should be called everytime when the value of the cell gets changed

from tableview.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.