Arcadii Rubailo
Arcadii Rubailo

Reputation: 113

setTextcolor by using getCurrentTextColor()

I need to get current text color form TextView and then assign this value to TextView.setTextColor(). But I get a large int -1979711488138, how can I get a color from it?

Upvotes: 6

Views: 1072

Answers (4)

headsvk
headsvk

Reputation: 2826

You cannot fit the number into an int, I get -1979711488 which is #8A000000 i.e. black with 138 alpha. You can get all parts of the color like this:

int color = getCurrentTextColor();
int a = Color.alpha(color);
int r = Color.red(color);
int g = Color.green(color);
int b = Color.blue(color);

It's really strange why the default text color is not a solid color but rather black with an alpha value as that is more expensive for the system.

Upvotes: 0

Anand Singh
Anand Singh

Reputation: 5692

Suppose you want to set color from textView1 to textView then you can do like this:

textView.setTextColor(textView1.getCurrentTextColor());

Upvotes: 2

Mahesh Suthar
Mahesh Suthar

Reputation: 247

 public class MainActivity extends Activity
 {

    private TextView txtViewIpLable,textView1;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        textView1.setTextColor(txtViewIpLable.getTextColors());
    }
    private void init() 
    {
        // TODO Auto-generated method stub
       txtViewIpLable = (TextView) findViewById(R.id.txtViewIpLable);
       textView1 = (TextView) findViewById(R.id.textView1);
    }

 }

Upvotes: 0

Sasi Kumar
Sasi Kumar

Reputation: 13348

Integer intColor = -1979711488138;
String hexColor = "#" + Integer.toHexString(intColor).substring(2);

or

String hexColor = String.format("#%06X", (0xFFFFFF & intColor));

Upvotes: 4

Related Questions