Android programmability is one of the fascinating features that attract developers. This customizability is endless when it comes to the Android app. It is so much that one can even use a different color in text view. But how to set TextView color in Android?
To add TextView color using Android studio, you will have to first define the command on the XML file and then use it on the Java file. For XML files, use the Android:textColor=”” and Android:background=”” for font and background respectively. And in the case of Java, use setTextColor() and setBackgroundColor().
Using the codes with their respective color will help you define your uniqueness. You can also change the font color on Galaxy S6 if you follow the proper steps.
Why Set Text Color Android?
The dynamic programmability of the Android application is the best. This is why most new developers aim to be Java or XML developers for the Android platform. But why does one need to change text colors on Android?
From a user’s perspective, the color might not be that significant. In fact, most Android application users might not even notice the color. They might see the background or icon, but the font color goes unnoticed.
Font Color vs Background
But if someone does thorough research, they will find that the font color is more significant than the background color. This is because our human psyche associates different functions with different features.
For instance, when someone sees a blue underlined text, they will immediately know it is a hyperlink. But if a black underlined text appears, they will regard it as a heading or important line instead of a hyperlink.
To be a proper Android developer, one needs to use Android studio to make the most eye-catching Android app. And to be successful in making the app eye-catching, one must learn how to set the color of TextView in Android.
Set Text Color Programmatically Android
If you want your Android application to stand out, you need to use code with the style attribute. And the Android textcolor codes are included in these attribute type codes.
Some of the best ways to set the color of TextView font and background are as follows.
Text View Color on XML File
Most coders and developers use the XML format for Android app development. This is why most want to know how to set the text color of TextView in Android XML. Here are the steps to do so perfectly.
Step 01: Open Android Studio.
Step 02: Create a new project.
Step 03: From the left panel, go to res.
Step 04: Next select layout.
Step 05: And finally, select activity_main.xml.
Step 06: Within the main activity XML file type in the TextView example code as follows.
<LinearLayout
xmlns:Android=”http://schemas.Android.com/apk/res/Android”
Android:layout_width=”match_parent”
Android:layout_height=”match_parent”>
<TextView
Android:id=”@+id/text_view_id”
Android:layout_height=”wrap_content”
Android:layout_width=”wrap_content”
Android:text=”@string/hello”
/>
</LinearLayout>
Step 07: After the Android:text=”@string/hello” insert an additional line of code. This is the main color code to add font color.
Android:textColor=”Color”
Step 08: To add background color to the text, you must use a code that responds to the background attribute. Here is the example code.
Android:background=”Color”
Step 09: If you followed the steps correctly, the entire code with custom font and background color should be somewhat as follows.
In place of “Color”, you will have to use the color you want. You can swap it with the #ARGB, #RRGGBB, #RGB, or #AARRGGBB color values.
If you want to use the same color for multiple text view segments, you can use the color’s name. But in this case, you will have to add their description beforehand to the colors.xml file.
Change Colors On Java
Java and XML go hand in hand when it comes to Android application creation. But the core code structure they follow is different.
To properly understand how to set the background color of TextView in Android, one also must learn the steps required for a Java file. For background and font color, follow these steps, and you will be good.
Step 01: After creating the Android textcolor XML file navigate to src from the left panel.
Step 02: Within this option select MainActivity.java.
Step 03: In the blank field type in the following example code.
import Android.graphics.Color;
import Android.support.v7.app.AppCompatActivity;
import Android.os.Bundle;
import Android.widget.TextView;
public class MainActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView1 = findViewById(R.id.textView1);
textView1.setText(“Hello World!!”);
}
}
Step 04: Now add the color code after the TextView textView1 = findViewById(R.id.textView1); similar to the following. This is for the font color.
textView1.setTextColor(Color.Name_Or_Code_Of_Color);
Step 05: If you want to add background color add the following code.
textView1.setBackgroundColor(Color.Name_Or_Code_Of_Color);
Step 06: The final code for both custom background and font color should be somewhat as follows.
Similar to before, replace the Name_Or_Code_Of_Color with the color code or name of the color that you already determined in the colors.xml file
For Special Texts
Not all texts used with text view are static. Some texts are unique and dynamic. So how to set TextView background color in Android dynamically?
The steps are easy if you follow the following ones.
Step 01: Use the simal code for your font and background texts as before.
Step 02: You will have to define the special text first. The example code we used is for determining a hyperlink.
Step 03: Go to the strings.xml file.
Step 04: Define the HTML link you want.
<string name=”link_text”>This is <a href=”Your_Link”>a hyperlink</a> example</string>
Step 05: After this go back to the activity_main.xml and add the following code.
Android:textColorLink=”@color/color_link”
Step 06: And within the MainActivity.java add this example.
textElement.setLinkTextColor(Color.Name_Or_Code_Of_Color);
Step 07: To set different colors for the normal and link text, you will have to use the following codes.
Step 07-a: In XML file.
Android:textColorHighlight=”@color/color_highlight “
Android:textColorHint=”@color/color_hint “
Step 07-b: In Java file.
textElement.setHighlightColor(Color.Name_Or_Code_Of_Color);
textElement.setHintTextColor(Color.Name_Or_Code_Of_Color);
Nickname Colors
As mentioned above, you can use the name of a color to use it in the code in place of code. For this, you will have to define the color first in the colors.xml file under values using the following code.
<resources>
<color name=”Custom_Name”>Color_Code</color>
</resources>
After this, call the defined color in your Java code. To refer to the color in code, use the following example.
getResources().getColor(R.color.Custom_Name);
Final Words
While using codes to customize your app, you need to be careful. A simple misplacement of a lion or symbol can cause you a serious loss. The code will not work correctly and will give you a severe headache.
No matter how good a developer you are, you can’t be too careful. This is why one must know how to set TextView color in Android. This will not only make the app unique but also flawless. Have fun tinkering with your Android customization options!
Recent Posts
Blog Introduction: If you're playing an app on your Android smartphone and it comes with an OBB file, you may wonder how to open it. OBB files are data files for certain Android apps, just like APK...
Qlink phone is a blessing for anyone who needs the smartphone service badly but can't afford the costly packages to use it. Through this free service phone or the Qlink phone, the user can choose any...