Settextcolor android studio kotlin

How to use «setTextColor(hexaValue)» on Kotlin for Android

Question: Background In Java, I could directly change the text color of a TextView, using the standard hexa-decimal value of it: Very easy. Solution 4: for changing the text color, you can follow this — Question: I need to change text color when state change(pressed, focus). How to set the text color of a using ?

How to use «setTextColor(hexaValue)» on Kotlin for Android

Background

In Java, I could directly change the text color of a TextView, using the standard hexa-decimal value of it:

 textView.setTextColor(0xffffffff); //white textView.setTextColor(0x00000000); //transparent textView.setTextColor(0xff000000); //black textView.setTextColor(0xff0000ff); //blue //etc. 

The problem

On Kotlin, if I try to write such a thing, I get with a weird build error:

Error:(15, 18) None of the following functions can be called with the arguments supplied : public open fun setTextColor(p0: colorstatelist !): Unit defined in android.widget.TextView public open fun setTextColor(p0: Int): Unit defined in android.widget.TextView

What I’ve tried

I tried to search about this over the Internet, and I couldn’t see anything special about hexa-decimal values. Seems the same like on Java:

Then I decided to just write in Java, and convert to Kotlin. The result is very unreadable in terms of the color value:

 textView.setTextColor(-0x1) //white textView.setTextColor(0x00000000) //transparent textView.setTextColor(-0x1000000) //black textView.setTextColor(-0xffff01) //blue 

To me it seem that the hexadecimal value of Integer that is used for Kotlin is signed, while on Java it’s converted to signed one automatically, so this causes flipping of values and the need to set a minus sign when needed.

The only thing I can think of, that still allows to read it well, is something like this:

textView.setTextColor(Integer.parseUnsignedInt("ffff0000",16)); 

However, this has multiple disadvantages:

  1. It is way longer.
  2. It converts a String, so it’s much less efficient
  3. Most importantly: it works only from API 26 (Android O) , which currently is active on about 1% of Android devices worldwide.

The questions

What exactly can I do to make it the most readable, without string conversions, and work on all Android versions (minSdkVersion 14 in my case) ?

textView.setTextColor(Color.parseColor("#0aad3f")) 

Oxff000000 is resolved to Long in Kotlin so right now there is no way to use this literal as is, however 0xff000000.toInt() will give you exactly the same result as -0x1000000 so you can use .toInt() approach. Under the hood, it’s the equivalent of (int)4278190080L Java cast .

Also, with Kotlin extensions you can write a simple property like that

var TextView.textColor: Long get() < //. not important >set(value: Long)

and you’ll be able to use a more concise syntax textView.textColor = 0xff000000

UPDATE : As of Kotlin 1.3 it will be possible to use concise syntax like that 0xff000000u See: Jetbrains blog and the original proposal

You can try this to set color of your text programmatically.

textview.textColor=Color.parseColor("#22aadd") 

for changing the text color, you can follow this —

textView.setTextColor(ContextCompat.getColor(applicationContext,R.color.colorAccent)) 

C — Set static text color Win32, Set static text color Win32. I am making a dll that controls a dialogue box. I like to get a certain area to have red text. This code does compile, but the …

Читайте также:  Windows sass to css

How to set TextColor using setTextColor(ColorsStateList colors)?

I need to change text color when state change(pressed, focus).

How to set the text color of a TextView using ColorsStateList ?

If you need to set the colors in code (using ColorStateList), but still want to keep the color states in an XML, you might want to use this:

You have to use getColorStateList()

I was also struggling with this problem, if you want to use a state list , you need to declared it in the color resources folder, instead of the drawable folder, and use the setTextColor(getResources().getColorStateList(R.color.your_colors)) .

You can also use contextcompat to load a color state list

ColorStateList colors = ContextCompat.getColorStateList(this,R.color.my_color_list);

Android — TextView setTextColor() not working, The documentation is not very verbose about this, but you cannot use just the R.color integer when calling setTextColor.You need to call …

SetTextColor not changing command line text color

Since BGI is obsolete, and a lot of its source code seems to be missing from the original website, I’ve been meaning to design my own color engine that will affect lines individually. So far, the 16 colors that » setconsoletextattribute ()» from windows.h can accept have been doing fine, but I’ve been meaning to use more colors (by using RGB instead of 0xbf) to upgrade the look of it and color my own ASCII art.

«SetTextColor ()» seems to be the route I want to go. I’ve set up a testing function to see if it works. Here’s the snippet of code with the setup.

HDC hType; // Handle DC, save some work to reduce repetition int initColor () // Initializes engine < hType = GetDC (GetConsoleWindow ()); printf ("String Hexadecimal\n"); testcolorR (RGB(255, 0, 0)); // Red testcolorR (RGB(0, 255, 0)); // Green testcolorR (RGB(0, 0, 255)); // Blue getch (); // Pause to see results return 0; // Exit success >// Take in RGB void colortextR (COLORREF rgbcolor) < SetTextColor (hType, rgbcolor); >// Test RGB colors int testcolorR (COLORREF color)

However, on the command line, the color did not change and remained as the default light-gray, but this is the result.

Which means that the RGB color is being passed, but something else is causing this problem. I suspect the culprit is the GetConsoleWindow () function.

SetTextColor is a GUI function; it will not have the effect you want in a standard Windows console.

If your application will only be run on Windows 10 build 14392 or later, or on (most) non-Windows platforms such as Linux, then you can generally use virtual terminal sequences. Please note that even on supported versions of Windows, VT functionality must be explicitly enabled:

// error handling omitted for brevity; see GetLastError HANDLE hOut = GetStdHandle( STD_OUTPUT_HANDLE ); if( hOut == INVALID_HANDLE_VALUE ) < return; >DWORD dwMode = 0; if( !GetConsoleMode( hOut, &dwMode ) ) < return; >dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING; if( !SetConsoleMode( hOut, dwMode ) ) < return; >std::cout  

If your application needs to run on older versions of Windows, and the standard 16-color palette is sufficient, then something similar to the following will work (see SetConsoleTextAttribute and console screen buffer text attributes):

// error handling omitted for brevity; see GetLastError HANDLE hOut = GetStdHandle( STD_OUTPUT_HANDLE ); if( hOut == INVALID_HANDLE_VALUE ) < return; >// save current buffer information CONSOLE_SCREEN_BUFFER_INFO sbInfo< sizeof CONSOLE_SCREEN_BUFFER_INFO >; GetConsoleScreenBufferInfo( hOut, &sbInfo ); SetConsoleTextAttribute( hOut, FOREGROUND_RED | FOREGROUND_INTENSITY ); std::cout  

Qt - QLabel: set color of text and background, The best and recommended way is to use Qt Style Sheet. Docs: Qt 5 Style Sheet, Qt 6 Style Sheet. To change the text color and background color of a …

Set static text color Win32

I am making a dll that controls a dialogue box. I like to get a certain area to have red text. This code does compile, but the effect is not seen. Here is the area where the dialogProc is done:

LRESULT CALLBACK DialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) < switch(message) < case WM_INITDIALOG: CheckDlgButton(hDlg, IDC_CHECK, FALSE); EnableWindow(GetDlgItem(hDlg, IDOK), FALSE); return TRUE; case WM_COMMAND: switch (LOWORD(wParam)) < case IDC_CHECK: if (IsDlgButtonChecked(hDlg, IDC_CHECK)) < EnableWindow(GetDlgItem(hDlg, IDOK), TRUE); EnableWindow(GetDlgItem(hDlg, IDCANCEL), FALSE); >else < EnableWindow(GetDlgItem(hDlg, IDOK), FALSE); EnableWindow(GetDlgItem(hDlg, IDCANCEL), TRUE); >break; case IDOK: < EndDialog(hDlg, TRUE); return FALSE; >case IDCANCEL: < EndDialog(hDlg, FALSE); return FALSE; >case WM_CTLCOLORSTATIC: // Set the colour of the text for our URL if ((HWND)lParam == GetDlgItem(hDlg,IDC_WARNING)) < // we're about to draw the static // set the text colour in (HDC)lParam SetBkMode((HDC)wParam,TRANSPARENT); SetTextColor((HDC)wParam, RGB(255,0,0)); return (BOOL)CreateSolidBrush (GetSysColor(COLOR_MENU)); >return TRUE; > > return FALSE; > 

WM_CTLCOLORSTATIC is a separate message from wm_command . Your desired handling of the message appears to be correct except that the check for the message is inside your check for WM_COMMAND specific items. Try reorganizing your outer switch statement. Perhaps something like the following:

LRESULT CALLBACK DialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) < switch(message) < case WM_INITDIALOG: // . break; case WM_COMMAND: switch (LOWORD(wParam)) < case IDC_CHECK: // . break; case IDOK: // . break; case IDCANCEL: // . break; >break; case WM_CTLCOLORSTATIC: // Set the colour of the text for our URL if ((HWND)lParam == GetDlgItem(hDlg, IDC_WARNING)) < // we're about to draw the static // set the text colour in (HDC)lParam SetBkMode((HDC)wParam,TRANSPARENT); SetTextColor((HDC)wParam, RGB(255,0,0)); // NOTE: per documentation as pointed out by selbie, GetSolidBrush would leak a GDI handle. return (BOOL)GetSysColorBrush(COLOR_MENU); >break; > return FALSE; > 

Also note that it would be kinda weird to filter WM_COMMAND's wParam argument when wParam is supposed to provide the HDC for WM_CTLCOLORSTATIC.

WM_CTLCOLORSTATIC Notification at MSDN

How to change text color programmatically in kotlin, Stack Overflow Public questions & answers; Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers; Talent Build …

Источник

The Ultimate Guide to Changing Text Color in Kotlin for Android Development

Learn how to change text color in Kotlin for Android development with our comprehensive guide. Discover best practices and helpful tips for TextView, Button, Menu item, and more.

  • Changing Text Color in TextView
  • Changing Text Color in Button
  • Change the Text Color of a Substring
  • Changing Text Color of Menu Item
  • Using Android Resource or RGB to Change Text Color
  • Automatically Change Text Color Based on Background Color
  • Other examples of code for changing text color in Kotlin
  • Conclusion
  • How do I change the text color in Kotlin?
  • How do I change text color programmatically in Kotlin?
  • How do I change text on Kotlin?
  • How to change text color in XML?

Kotlin is a popular programming language used for developing Android applications. One common task in Android development is changing the color of text in various UI elements. This blog post will provide a comprehensive guide on how to change the color of text in Kotlin programming language for Android, including best practices and helpful tips.

Changing Text Color in TextView

TextView is one of the most commonly used UI elements in Android applications. It is used to display text on the screen. One can change the color of text in a TextView by setting the color in layout XML file using the “textColor” attribute. Alternatively, one can change the color programmatically using Kotlin code.

To change the text color of a TextView in Kotlin, one can use the standard hexadecimal value by using textView.setTextColor(0xffffffff) . Another way to change the text color of a TextView programmatically is by using the code snippet //with a color myText.setTextColor(ContextCompat.getColor(this,R.color.orange_or)) //with a selector myText .

It is essential to consider the advantages of seeing the UI in design time and minimizing the runtime changes when tweaking UI in code. Therefore, it is recommended to define the text color in the XML layout file.

Changing Text Color in Button

Buttons are another essential UI element in Android applications. To change the color of a Button text programmatically, one can pass the specified color to the method Button.setTextColor(new_color) . Different methods are available for changing the color of text in a TextView and a Button.

Change the Text Color of a Substring

We will change the color of some sub-strings of text of TextView using the ''SpannableString Duration: 4:27

Changing Text Color of Menu Item

To change the text color of a Menu item in Android using Kotlin, one can set a custom view for the MenuItem and then define the text color. One can change the color of a part of a textview by using the SpannableString class. This class provides the functionality to change the color of a specific part of the text.

Using Android Resource or RGB to Change Text Color

There are two methods of changing the color of a TextView in code - by using Android resource or by using RGB. Android resources provide an easy way to define colors and other resources that can be used in the XML layout files and Kotlin code. One can change the text color of a TextView using Android resource or RGB.

Automatically Change Text Color Based on Background Color

A new theme can be created for night in the values-night folder to automatically change the text color based on the background color. This feature can be used to provide a better user experience in low light environments.

Other examples of code for changing text color in Kotlin

In Java case in point, android kotlin change text color code example

// Android kotlin changing text color// like this android:textColor="#454545"
// like this android:textColor="@color/red" //or programmtically textView.textColor = Color.RED

Conclusion

Changing the color of text in kotlin programming language for android is a common task for developers. This blog post has provided a guide on how to change the color of text in TextView, Button, Menu item, and a part of a TextView using Kotlin code. It is essential to consider the advantages of seeing the UI in design time and minimizing the runtime changes when tweaking UI in code. By following the best practices and tips provided in this post, developers can efficiently change the color of text in their Android applications.

Источник

Оцените статью