setTextcolor by using getCurrentTextColor() - android

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?

Integer intColor = -1979711488138;
String hexColor = "#" + Integer.toHexString(intColor).substring(2);
or
String hexColor = String.format("#%06X", (0xFFFFFF & intColor));

Suppose you want to set color from textView1 to textView then you can do like this:
textView.setTextColor(textView1.getCurrentTextColor());

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);
}
}

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.

Related

Android - A variable might not have been initialized

Trying to create a BMI Calculator I already declared a variable for weight and height, but still I'm getting an error of "variable "weight" might not have been initialized.
public class MainActivity extends AppCompatActivity {
private EditText eheight, eweight;
private Button computeb;
private TextView output, category;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
eheight = (EditText) findViewById(R.id.eheight);
eweight = (EditText) findViewById(R.id.eweight);
output = (TextView) findViewById(R.id.output);
category = (TextView) findViewById(R.id.category);
computeb = (Button) findViewById(R.id.computeb);
computeb.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
float weight, height, BMI;
if (checkInputLength())
return;
weight = Float.parseFloat(weight.getText().toString());
height = Float.parseFloat(height.getText().toString());
computeBMI(weight, height);
BMI = weight / (height * height);
}
});
}
Change
weight = Float.parseFloat(weight.getText().toString());
height = Float.parseFloat(height.getText().toString());
to
weight = Float.parseFloat(eweight.getText().toString());
height = Float.parseFloat(eheight.getText().toString());
weight and height are just float variables.If you want to parse data from edittext then you have to get the data from the edittexts and store them in the corresponding variables.
weight.getText()? weight is a float type value which is not initialized here. I think you want to use eweight. And the same with height.
weight = Float.parseFloat(eweight.getText().toString());
height = Float.parseFloat(eheight.getText().toString());
just set it 0.
float weight = 0;
change this:
weight = Float.parseFloat(weight.getText().toString());
to:
weight = Float.parseFloat(eweight.getText().toString());

Android: create a drawable that shows two lines of text one above the other in different fonts and sizes?

I want to create a single drawable that shows two lines of text, one above the other. Each line of text has to be in it's own typeface and textsize and it has to create a single drawable because I want to then set it as the drawable for a floating action button.
private void updateFloatingButtonText(String headlineText, String subHeadlineText, FloatingActionButton floatingActionButton) {
int headlineTextSize = getResources().getDimensionPixelSize(R.dimen.headlineTextSize);
int subheadlineTextSize = getResources().getDimensionPixelSize(R.dimen.subheadlineTextSize);
Spannable spannableStringHeadline = new SpannableString(headlineText);
Spannable spannableStringSubheadline = new SpannableString(subHeadlineText);
CustomTypefaceSpan boldSpan = new CustomTypefaceSpan("FontOne", FontCache.get("FontOne.ttf", this));
CustomTypefaceSpan regularSpan = new CustomTypefaceSpan("FontTwo", FontCache.get("FontTwo.ttf", this));
// set typeface headline
spannableStringHeadline.setSpan(regularSpan, 0,
headlineText.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE
);
// set typeface subtitle
spannableStringSubheadline.setSpan(boldSpan, 0,
subHeadlineText.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE
);
// set text size headline
spannableStringHeadline.setSpan(new AbsoluteSizeSpan(headlineTextSize), 0,
headlineText.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE
);
// set text size subline
spannableStringSubheadline.setSpan(new AbsoluteSizeSpan(subheadlineTextSize), 0,
subHeadlineText.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE
);
String finalString = TextUtils.concat(spannableStringHeadline, "\n", spannableStringSubheadline);
floatingActionButton.setImageDrawable([put the resulting drawable here]);
}
I've written this method that creates a single string formatted exactly the way that I need it, but I still have the issue of creating a drawable out of it.
I've tried to use this third party library, but although it displays the text in the correct typefaces it doesn't change the textsize of the lines of text.
https://github.com/devunwired/textdrawable
Is there a trivial (or nontrivial) way of doing this?
Solved by creating a new class that looks like this:
public class TextToDrawable extends Drawable {
private String headlineText = "";
private String subHeadlineText = "";
private final TextPaint headlinePaint = new TextPaint();
private final TextPaint subHeadlinePaint = new TextPaint();
public TextToDrawable(Context context, String headlineText, String subHeadlineText) {
this.headlineText = headlineText;
headlinePaint.setAntiAlias(true);
headlinePaint.setTypeface(FontCache.get("FontA.ttf", context));
headlinePaint.setTextSize(context.getResources().getDimensionPixelSize(R.dimen.headlineTextSize));
headlinePaint.setColor(ContextCompat.getColor(context, android.R.color.white));
headlinePaint.setStyle(Paint.Style.FILL);
headlinePaint.setTextAlign(Paint.Align.LEFT);
this.subHeadlineText = subHeadlineText;
subHeadlinePaint.setAntiAlias(true);
subHeadlinePaint.setTypeface(FontCache.get("FontB.ttf", context));
subHeadlinePaint.setTextSize(context.getResources().getDimensionPixelSize(R.dimen.subheadlineTextSize));
subHeadlinePaint.setColor(ContextCompat.getColor(context, android.R.color.white));
subHeadlinePaint.setStyle(Paint.Style.FILL);
subHeadlinePaint.setTextAlign(Paint.Align.LEFT);
}
#Override
public void draw(Canvas canvas) {
Rect headlineWidth = new Rect();
Rect subheadlineWidth = new Rect();
headlinePaint.getTextBounds(headlineText, 0, headlineText.length(), headlineWidth);
subHeadlinePaint.getTextBounds(subHeadlineText, 0, subHeadlineText.length(), subheadlineWidth);
Rect bounds = new Rect();
headlinePaint.getTextBounds(headlineText, 0, headlineText.length(), bounds);
int x = getBounds().width() / 2 - (headlineWidth.width()/2);
int y = (getBounds().height() / 2);
canvas.drawText(headlineText, x, y, headlinePaint);
x = getBounds().width()/2 - (subheadlineWidth.width()/2);
y += headlinePaint.getFontSpacing();
canvas.drawText(subHeadlineText, x, y, subHeadlinePaint);
}
#Override
public void setAlpha(int alpha) {
}
#Override
public void setColorFilter(ColorFilter colorFilter) {
}
#Override
public int getOpacity() {
return 0;
}
}
Then using the new class like this:
mFloatingActionButton.setImageDrawable(new TextToDrawable(this, "Headline", "Subheadline"));
This isn't a great solution because it only supports two lines of text - there's nothing dynamic going on here. However, I suppose it would be fairly easy to rewrite to support even more lines and more fonts and it solves the current problem.

How to change the color of underline in android

I am trying to change the color of underline in textView,I came across the link
How to get UnderlineSpan with another color in Android?
But if i tried to implement that,I am not getting color
This is my code
String middleStringText = MyTextView.getText().toString();
Spannable spannable1 = new SpannableString(middleStringText);
CustomUnderLineSpan underLineSpan = new CustomUnderLineSpan(Color.YELLOW,2, 5);
spannable1.setSpan(underLineSpan, 0, 10, spannable1.SPAN_EXCLUSIVE_EXCLUSIVE);
MyTextView.setText(spannable1, TextView.BufferType.SPANNABLE);
Have anyone tried similar sort of implementation?
It is not correct Solution,But it is useful for time being purpose,which i got from some link in stack over flow.
spannable1.setSpan(new ColoredUnderlineSpan(Color.YELLOW), middleStringText.indexOf(startText), middleStringText.indexOf(EndText) + value.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
final class ColoredUnderlineSpan extends CharacterStyle
implements UpdateAppearance {
private final int mColor;
public ColoredUnderlineSpan(final int color) {
mColor = color;
}
#Override
public void updateDrawState(final TextPaint tp) {
try {
final Method method = TextPaint.class.getMethod("setUnderlineText",
Integer.TYPE,
Float.TYPE);
method.invoke(tp, mColor, 8.0f);
} catch (final Exception e) {
tp.setUnderlineText(true);
}
}
}
What you can do is to create a new paint color and then assign that color to the textview.
Paint p = new Paint();
p.setColor(Color.RED);
TextView myTextView = (TextView) findViewById(R.id.textview);
myTextView .setPaintFlags(p.getColor());
myTextView .setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
myTextView .setText("Underline Text with red color.");
Check This
Paint p = new Paint();
p.setColor(Color.RED);
TextView t = (TextView) findViewById(R.id.textview);
t.setPaintFlags(p.getColor());
t.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
t.setText("Hello World");
String Text = "<u><font color='blue'>Underline Text</font></u>.";
textView.setText(Html.fromHtml(Text), TextView.BufferType.SPANNABLE);

Changing background color dynamically (Android)

I have the following code:
public class MainActivity extends Activity {
private BufferedReader br;
private Socket s;
private View v,v1;
private RelativeLayout rl;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
s = new Socket("192.168.1.36",50000);
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
}catch(Exception e){e.printStackTrace();}
color();
}
private void color(){
rl = (RelativeLayout) this.findViewById(R.id.rellay);
while(true){
try{
String received = br.readLine();
if(received != null){
// System.out.println(received);
String[] color = received.split(",");
setColor(color);
}
}catch(Exception e){e.printStackTrace();}
}
}
private void setColor(String[] color){
rl = (RelativeLayout) this.findViewById(R.id.rellay);
int red = Integer.parseInt(color[0]);
int green = Integer.parseInt(color[1]);
int blue = Integer.parseInt(color[2]);
int a = Integer.parseInt(color[3]);
rl.setBackgroundColor(Color.argb(a, red, green, blue));
What I want to do is receive 4 values separated by commas (this works), and I want the values in the range 0-255 to be the RGB color.
I want to change the background of the android activity. I can change the color once, from the onCreate method, but when I try to change it further times I get the default white screen. The values never exceed 255.
How can this be done? Thanks!!
Make sure the alpha value != 0.
The alpha value represent the transparency of the color so 0 is fully transparent.
try to start with fixed values, like:
rl.setBackgroundColor(Color.argb(255, 0, 0, 0));
and make sure it works.
after that, pull your values from the server and parse them into a color.
BTW, Listen to Nick Cardoso comments, It's a bad user experience to feel the network latency.

Textview image in android

In my application i have to show an image in my text view. Also When i click 1st time 1 image should draw and clicking next time another image should be drawn.Is it possible to draw an image in textview?Please help me..
You can use
setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom)
for the textView
You can set a background image using the android:background attribute
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/image" />
Or programatically with the setBackgroundDrawable()or setBackgroundResource() methods:
textView.setBackgroundResource(R.drawable.image);
You can take an array of Drawables, an an index for that:
TextView tv;
Drawable[] drbl = new Drawable[4];
int drblIndex = 0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
drbl[0] = getResources().getDrawable(R.drawable.ic_launcher);
drbl[1] = getResources().getDrawable(R.drawable.img2);
drbl[2] = getResources().getDrawable(R.drawable.img3);
drbl[3] = getResources().getDrawable(R.drawable.right_arrow);
tv = (TextView) findViewById(R.id.textView1);
tv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
tv.setBackgroundDrawable(drbl[drblIndex++]);
if(drblIndex == drbl.length)
drblIndex = 0;
}
});
}
Then you can set values of that array as I have done.
Then onClick you can move to next Index and can set new Drawable-Image to TextView.
When Index reaches to last value, set it to zero, simply.
Set OnClickListener on the TextView and in its onClick() method, use the following methods to set the images :
textview.setBackgroundDrawable();
or
textview.setBackgroundResource();
try this code, textview with image.
CharSequence dogstate = null ;
Html.fromHtml("" + " "
+ "your text" + "", featuregetter, null);
dogatstatus.append(dogstate);
private ImageGetter featuregetter = new ImageGetter() {
public Drawable getDrawable(String source) {
Log.i(" get drawable mathod ", "");
Bitmap B = BitmapFactory.decodeResource(getResources(),
R.drawable.bone);
BitmapDrawable BD = new BitmapDrawable(B);
BD.setBounds(0, 0, B.getWidth(), B.getHeight());
return BD;
}
};

Categories

Resources