How, using java, to bold certain parts of a Textview?

Asked

Viewed 782 times

1

I would like to know how to make a certain part of a text that will be displayed in a TEXTVIEW bold:

inserir a descrição da imagem aqui

I would like the text displayed to look like this: Total value => R$ 1100.

Activity:

package genesysgeneration.stackall;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView tvValor;
    private Button btnMais100;
    int valor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tvValor=(TextView)findViewById(R.id.tvValor);
        btnMais100=(Button)findViewById(R.id.btnMais100);
        valor=0;

        btnMais100.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                valor+=100;
                tvValor.setText(String.valueOf("Valor total => R$ " + valor + "."));

            }
        });

    }
}

I know I could create another Textview, but it doesn’t suit me. I also know I could do it in . xml from Textview, but it’s unfeasible because the text is dynamic (not so much in the example, but in my actual project where I want to use it).

1 answer

1


Use the class Spannablestring, it allows attaching objects from Markup to specific parts of a text.

TextView textView = (TextView)findViewById(R.id.textView);
String label = "Valor total => ";
String valor = "R$ 1100";

SpannableString textoNegrito = new SpannableString(label + valor);
textoNegrito.setSpan(new StyleSpan(Typeface.BOLD), label.length(), textoNegrito.length(), 0);
textView.setText(textoNegrito);
  • in the case of a text, such a thing becomes unviable

  • I don’t understand. Can you explain?

Browser other questions tagged

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