With setTextColor() method we can set the color for Textview. For this method we need to pass color vallu
|
|
Change Text Color of TextView via XML Layout File
Textview has property 'textColor' attribute which is set a color of our choice. We can provide the color as hexa 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
|
... android:textColor="#92B" /> ... android:textColor="#A92B" /> ... android:textColor="#9C27B0" /> ... android:textColor="#CC9C27B0" /> |
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
|
xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linear_layout_id" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> android:id="@+id/text_view_id" android:layout_height="wrap_content" android:layout_width="match_parent" android:gravity="center" android:textSize="40dp" android:padding="50dp" android:textColor="#9C27B0" android:text="@string/hello" />
|
We have used a string resource, and the contents of strings.xml is
|
|
There is no need to change the MainActivity.kt file. The default code would do.
MainActivity.
|
import androidx.appcompat.app.AppCompatActivity import android.os.Bundle class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) } } |