Text in bold from a json

3

There is some way to make a fraction of the text in a json in Android Studio 2.3 go bold, just like line breaks are done.

I have this json:

{
"array_texto":[
    {
    "texto"":"Hola mundo",
    "texto":"Esto es un hola mundo"
    }
]
}

I read the json from the raw folder with this code:

 public String LeerFicheroRAW (InputStream fraw){
        String linea = null;
        try {
            BufferedReader brin = new BufferedReader(new InputStreamReader(fraw));

            StringBuilder sBuilder = new StringBuilder();
            String line = null;
            while ((line = brin.readLine()) != null) {
                sBuilder.append(line + "\n");
            }
            linea = sBuilder.toString();
            fraw.close();
            Log.i("Ficheros", "Fichero RAW leido!");
            Log.i("Ficheros", "Texto: " + linea);
        } catch (Exception ex) {
            Log.e("Ficheros", "Error al leer fichero desde recurso raw");
        }
        return linea;
    }

in the main activity I deserialize the json and I show it in the texview like this:

final InputStream fraw = getResources().openRawResource(R.raw.consti);
        String text = aux.LeerFicheroRAW(fraw);
        Gson gson = new Gson();
        json = gson.fromJson(text, Clase_generica.class);
 String texto = "";
        for (int i = 0; i < array_texto.size(); i++) {
            texto = texto.concat(getArraytexto.get(i).getTexto());
            texto = texto.concat("\n\n");
        }
 textview.setText(texto);

What I want to know is if from the json file you can make the text come in bold by default and it looks like this:

Hello world: This is a hello world

because for example if in the json file I have \ n like this:

 {
    "array_texto":[
        {
        "texto"":"Hola mundo",
        "texto":"Esto es\n un hola mundo"
        }
    ]
    }

then it appears like this when it is shown in the texview:

Hello world

This is

a hello world

Just as the IDE recognizes that "\ n" is a line break, there is something with which you can specify which text to quiro in bold. Any ideas?

    
asked by Felix A Marrero Pentón 13.10.2017 в 17:30
source

1 answer

1

If you want to do this:

Hello world : This is a hello world

From the .json that you propose, if you can edit the answer you get you can add the html tags for bold letter: <b>

 {
    "array_texto":[
        {
        "texto"":"<b>Hola mundo</b>",
        "texto":"Esto es\n un hola mundo"
        }
    ]
 }

and when you represent it in your TextView use the Html.fromHtml(...) method to get the html representation and display in bold the content between <b> and </b> :

textView.setText(Html.fromHtml(respuesta));

If you do not have the possibility to modify the answer then use a SpannableString defining a < a href="https://developer.android.com/reference/android/text/style/StyleSpan.html"> StyleSpan :

respuesta = "Hola mundo Esto es un hola mundo";
SpannableString miTexto = new SpannableString(strTexto);
    StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
    miTexto.setSpan(boldSpan, 0, 10, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
Spannable.SPAN_INCLUSIVE_INCLUSIVE);
    textView.setText(miTexto);

in this way you would get:

Hello world : This is a hello world

Related question:

Bold in a part of a TextView

    
answered by 13.10.2017 / 21:11
source