Android studio как изменить цвет текста

от admin

How to change font color of textView in android?

This example demonstrates how do I change font color of TextView in android.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml

Step 3 − Add the following code to src/MainActivity.java

Step 4 − Add the following code to androidManifest.xml

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen

Android studio как изменить цвет текста

One of the most often used User Interface elements is TextView. The user experience is improved, and the application looks better, due to the clear and organized text. It doesn’t matter what kind of information is being shown, it should always be done well. In an android app, TextView allows us to do this, thus understanding its properties is crucial.

There are two methods of changing the color of a TextView and the code for that has been given in both Java and Kotlin Programming Language for Android, so it can be done by directly adding a color attribute in the XML code or we can change it through the MainActivity File. By default, the color of the text will be black until it is changed.

How to change Text Color of TextView in Android?

TextView Text Color – To change the color of text in TextView, you can set the color in layout XML file using textColor attribute or change the color dynamically in Kotlin file using setTextColor() method.

In this tutorial, we will learn both the layout file approach and Kotlin line approach to change the text color of TextView.

Change Text Color of TextView via XML Layout File

textColor attribute of TextView widget lets you set a color of your choice. You can provide the color as hex value in one of the four formats: rgb, argb, rrggbb, or aarrggbb.

The syntax to set textColor attribute for TextView using different color formats is

Example 1 – TextView Color

Let us create an Android application with Kotlin support in Android Studio and change the text color of TextView in XML layout file.

activity_main.xml

We have used a string resource, and the contents of strings.xml is

Читать:
Сайт torch что это

There is no need to change the MainActivity.kt file. The default code would do.

MainActivity.kt

Run this application, and you would get the following screenshot.

Android TextView - Text Color

Change Text Color of TextView in Kotlin File

We can get the reference to TextView widget present in layout file and change the color dynamically with Kotlin code.

To set the color to the TextView widget, call setTextColor() method on the TextView widget reference with specific color passed as argument.

setTextColor() method takes int as argument. Use android.graphics.Color class to get an integer for a given color. You can provide the color as hex value in one of the four formats: rgb, argb, rrggbb, or aarrggbb.

The syntax to set text color using setTextColor() method of TextView in Kotlin Activity file is

Example 2 – TextView Color

Let us create an Android application with Kotlin support in Android Studio and change the text color of TextView dynamically/programmatically in Kotlin file.

The layout file contains a TextView. Since we are about to change the text color in Kotlin file, no text color is specified in layout XML file.

activity_main.xml

We have used a string resource, and the contents of strings.xml is

We have got the reference to the TextView in layout file using findViewById() method. Call to setTextColor() method on the TextView reference would set the text color of TextView.

MainActivity.kt

Run this application, and you would get the following screenshot.

Change Android TextView - Text Color dynamically

Conclusion

In this Kotlin Android Tutorial, we learned how to set or change the text/font color of TextView widget in Android application.

Android studio как изменить цвет текста

Для простого вывода текста на экран предназначен элемент TextView . Он просто отображает текст без возможности его редактирования. Некоторые его основные атрибуты:

android:text : устанавливает текст элемента

android:textSize : устанавливает высоту текста, в качестве единиц измерения для указания высоты используются sp

android:background : задает фоновый цвет элемента в виде цвета в шестнадцатиричной записи или в виде цветового ресурса

android:textColor : задает цвет текста

android:textAllCaps : при значении true делает все символы в тексте заглавными

android:textDirection : устанавливает направление текста. По умолчанию используется направление слева направо, но с помощью значения rtl можно установить направление справо налево

android:textAlignment : задает выравнивание текста. Может принимать следующие значения:

center : выравнивание по центру

textStart : по левому краю

textEnd : по правому краю

viewStart : при направлении текста слева направо выравнивание по левому краю, при направлении справа налево — по правому

viewEnd : при направлении текста слева направо выравнивание по правому краю, при направлении справа налево — по левому

android:fontFamily : устанавливает тип шрифта. Может принимать следующие значения:

Похожие статьи