Search code examples
javaandroidtextviewtextcolor

How to change Text Color and Text Size with Java in Android Studio?


package com.example.androidx;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

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

    public void setXiaomiPhone(View view) {
        TextView helloTextView = findViewById(R.id.textView);
        helloTextView.setText("Hello Xiaomi Phone");
   }
}

I need to change the text in Text View, and I also need to change the color and the size of the text. How can I do it?


Solution

  • To change the text color:

    textView.setTextColor(ContextCompat.getColor(context, R.color.<name_of_color>));
    

    To change the text size:

    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,14);