How to concatenate a MySQL query with html tags to send color to text textView on Android?

1

The following code in php

$result = mysql_query("SELECT name, concat('<div style="color:Red">',designation,'</div>') as designation FROM test.table_test;") or die(mysql_error());

if (mysql_num_rows($result) > 0) {

    while ($row = mysql_fetch_array($result)) {

        $product = array();
        $product["name"] = $row["name"];
        $product["designation"] = $row["designation"];

        $var_designation = $row["designation"];

    }
    echo "CON COLOR= ".$var_designation;

If I run it in a browser, it results in this:

If I send it to a textView it literally shows this:

<div style="color:Red">',designation,'</div>

Modify the MySQL query line by this:

SELECT name, CONCAT('<font color="#FF0000">',designation,'</font>') as designation FROM test.table_test limit 1;

But it gives me the same result ...

Is it possible what I try to do?

    
asked by El Cóndor 03.03.2017 в 21:59
source

2 answers

1

The most common thing seems to me that it would be to store the value of the color as Hexadecimal and in this way when obtaining it the text could be configured inside the TextView:

String colorDB = "#00FF00";  //color verde.

textView.setTextColor(Color.parseColor(colorDB));

If you have this in your database:

<font color="#FF0000">

You could do this:

  String colorDB = "<font color="#FF0000">";  //color rojo
  String endFont = "</font>";
  String mensaje = "StackOverflow is cool!";

  textView.setText(Html.fromHtml(colorDB 
+ mensaje + endFont));
    
answered by 03.03.2017 в 22:36
1

Why assign the color in the query? Better make a common and current query and at the time of showing the value you already assign the color, example:

$Color = "red";
$result = mysql_query("SELECT name, designation, as designation FROM test.table_test;") or die(mysql_error());

if (mysql_num_rows($result) > 0) {

    while ($row = mysql_fetch_array($result)) {

        $product = array();
        $product["name"] = $row["name"];
        $product["designation"] = $row["designation"];

        $var_designation = $row["designation"];

    }

 echo '<div style="Color:'.$Color.'">'.$var_designation.'</div>';

Then in Android you receive the value and you contain it in a textview which you will customize it by giving it a color:

   holder.tuTextView.setTextColor(Color.RED); 

or

tuTextView.setTextColor(Color.parseColor("##DF0101"))
    
answered by 04.03.2017 в 00:17