How to change the color of a Text from a Listview?

Asked

Viewed 2,828 times

1

I need my Listview to identify the values of a Text item, for example: , "Complete"/"Pending" and make each one a different color within Listview. I tried to format by code, but I could not, I saw that in . VCL has the Customdrawsubitem that puts the loop there and does it, but I did not find in . FMX which is what I want. The one below gives Acess 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;

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 your list view and for each item in the list, search for 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 want to change the colors of the text only with the name "Meubtn". you’d do it this way:

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’ve helped! ;)

  • I did what you said, it gives the following error: [dcc32 Error] frmExames.pas(70): E2010 Incompatible types: 'Tlistitemtext' and 'Tlistitemdrawable'

  • 1

    Inside the for, replace "txt := ..." with: txt := Tlistitemtext(listView1.Items[i].Objects.Finddrawable('Meubtn'));

  • Now yes, that’s what I wanted. Thank you!

1

I’ll tell you how to fix it, I had the same problem with the color, the FMX works with TAlphaColor, with this he does not understand if we put a color for example: clRed it has the predefined colors:

If you want to use a certain color, you would have to do it as follows:

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

All preset 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 the FMX will take on the color that has been determined.

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;

  • Thank you! It worked right!

  • I just had a problem because I switched to Dynamicappearence and include new Text components. I named them as attributes of my Binding and tried to put Listview1.Items[i].(tribute name). Equals('Pending') then and it doesn’t work. I don’t know how to access the particular items.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.