How to change color of a Text in a ListView?

1

I need my ListView to identify the values of a Text item, for example:, "Full" / "Pending" and make each one have a different color inside the ListView. I tried to format by code, but I could not, I saw that in .VCL there is the CustomDrawSubItem that puts the loop there and does it, however I did not find it in .FMX which is what I want. The one below gives Access Violation, how should I do this?

procedure TformExames.listView1ApplyStyleLookup(Sender: TObject);

var
  i : integer;
begin 

  for i := 0 to listView1.Items.Count -1 do

    if listView1.Items[i].Text = 'Cancel' then
    listView1.BeginUpdate;
    listView1.Items[i].Objects.TextObject.TextColor := 444444;
    listView1.EndUpdate;
end;
    
asked by anonymous 23.03.2017 / 19:54

3 answers

2

Philip,

There is a way to change the properties of each text component (or any other) within a listview. To do this, you loop in your list view and for each item in the list, look up the corresponding text (s) and change the properties you want.

In the example below, imagine that you have two text components in the listview item and you want to change the colors of the text only with the name "MeuBtn". you would do the following:

uses FMX.ListView.Types;

var
  i : integer;
  txt : TListItemText;
begin 

listView1.BeginUpdate;

for i := 0 to listView1.Items.Count -1 do
begin
    txt := TListItemText(listView1.Items[i].Objects.FindDrawable('MeuBtn'));

    if txt <> nil then
    begin
        txt.TextColor := $FF434A52;
        txt.Font.Size := 13;
        ...
    end;    
end;

listView1.EndUpdate;

end;

I hope I have helped! ;)

    
23.03.2017 / 21:22
1

I'm going to give you how to solve, I had the same problem with color, FMX works with TAlphaColor , so it does not understand if we put a color for example: clRed it has the pre-defined colors:

If you want to use a certain color, it would have to do the following:

ListView1.Items[i].Objects.TextObject.TextColor := TAlphaColorRec.Green

All pre-defined colors are in TAlphaColorRec , now if you want a custom color. You should develop a function.

function ColorToAlphaColor(Value: TColor): TAlphaColor;
var
  oCRec: TColorRec;
  oARec: TAlphaColorRec;
begin
  oCRec.Color := Value;
  oARec.A := 255;
  oARec.B := oCRec.B;
  oARec.G := oCRec.G;
  oARec.R := oCRec.R;
  Result  := oARec.Color;
end;

For the color you chose to start running on FMX, you should do the following:

ListView1.Items[i].Objects.TextObject.TextColor := ColorToAlphaColor(StringToColor('$0080FFFF'));

With this FMX will assume the color that was determined.

    
23.03.2017 / 21:08
0

procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
begin
  ListView1.BeginUpdate;
  for i := 0 to ListView1.ItemCount - 1 do
  begin
    if ListView1.Items[i].Text.Equals('Pendente') then
      ListView1.Items[i].Objects.TextObject.TextColor := TAlphaColorRec.Red
    else
      ListView1.Items[i].Objects.TextObject.TextColor :=
        TAlphaColorRec.Yellow;
  end;
  ListView1.EndUpdate;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  item: TListViewItem;
begin
  item := ListView1.Items.Add;
  item.Text := 'Pendente';
  item := ListView1.Items.Add;
  item.Text := 'Completo';
  item := ListView1.Items.Add;
  item.Text := 'Pendente';
  item := ListView1.Items.Add;
  item.Text := 'Cancelado';
end;
    
23.03.2017 / 20:43