Android color ProgressBar by textview - android

I want to set the color of my ProgressBar getting the color of the TextView.
I have not found any documentation on this.
This is my TextView: tv.setBackgroundColor(m.color);
and this is my ProgressBar:
int pr = (int)(value);
mProg = (ProgressBar)row.findViewById(R.id.progress_e);
mProg.setMax(100);
mProg.setProgress(pr);
how can I?

tv.setBackgroundColor(m.color);
mProg.getIndeterminateDrawable().setColorFilter(m.color,
android.graphics.PorterDuff.Mode.MULTIPLY);
In case you want to retrieve the TextView's color, an easy way to do it would be to set its color as its tag, retrieve, parse it and use it as the ProgressBar's setColorFilter's parameter.
//to set it:
tv.setBackgroundColor(m.color);
tv.setTag(m.color);
//to retrieve it:
int color=Integer.parseInt(tv.getTag().toString());
mProg.getIndeterminateDrawable().setColorFilter(color,
android.graphics.PorterDuff.Mode.MULTIPLY);

Related

How to get button text color in Android?

I'm looking for a way to dynamically get the button text color. I found this from here How to get current Button text color in Android?
ColorStateList mList = mButton.getTextColors();
int color = mList.getDefaultColor();
switch(color)
{
case Color.RED:
mButton.setTextColor(Color.BLACK);
break;
case Color.BLACK:
mButton.setTextColor(Color.RED);
break;
}
This code gives me some integer constants for example -1 is Color.WHITE but I want a more general way to get the hex integer value of the color and not just some constants.
To get the current color of the button's text try to use this method:
mButton.getCurrentTextColor();

How to change a strings color?

I want to change this string to the color red
String isFalse = False;
For some reason every tutorial seemed more complicated than I expected and I don't understand them. Is there a simple way to do this? Also, would this override the color of a textview? Because I would like it to.
String is not View, so it has no color at all.
Maybe what you want is to change the appearance color of it host TextView. To achieve this you can use:
TextView text;
//the initialize of this TextView
text.setText(isFalse);
text.setTextColor(Color.Red);
The parameter of the color could be resource from your color XML values file or android.R.color resource file, or from Color class, etc.
Please use this code.
SpannableStringBuilder builder = new SpannableStringBuilder();
String isFalse = "False";
SpannableString redSpannable= new SpannableString(isFalse);
redSpannable.setSpan(new ForegroundColorSpan(Color.RED), 0, Tru_score.length(), 0);
builder.append(redSpannable);
TextView text1 = (TextView)findViewById(R.id.textView1);
text1.setText(builder, BufferType.SPANNABLE);
It is not complicated or hard
Addressing both possibilities..
You want to change the color for your text at runtime.
that would be like TextView.setTextColor()
i.e.
falseTextView.setTextColor(getResources().getColor(R.color.red))
if you wanted a segment of the same textview to have a different color,by which I mean that the isFalse string is only apart of the content of your TextView ,you need to use as mentioned in the other answer.
Try this...
string.xml
<string name="isFalse"><![CDATA[<b><font color=#FF0000>False</b>]]></string>
MainActivity.java
TextView textView1 = (TextView)findViewById(
R.id.textView1);
textView1.setText(Html.fromHtml(isFalse));
And result, you might get like this...

Change the background color of the layout programmatically

Android Studio 0.4.6
minSdkVersion 10
targetSdkVersion 19
I have an activity called ReadingLamp and a Relativelayout called activity_reading_lamp.xml.
I am programmatically trying to set the layout to a different background color.
In my onCreate and set the content view to this layout.
setContentView(R.layout.activity_reading_lamp);
I try and get the root view by doing the following:
mActivityBackground = getWindow().getDecorView().getRootView();
Then later in my app I want to change the color so I do like this:
mActivityBackground.setBackgroundColor(Color.parseColor("#0cf5ff"));
However, the above line doesn't do anything to change the background.
I have also tried doing the following:
mActivityBackground = (RelativeLayout)findViewById(R.layout.activity_reading_lamp);
Where am I going wrong in my code?
you have declared, setContentView(R.layout.activity_reading_lamp); in your Activity. Then you should look for the view you want to change the background color. It has to belongs to R.layout.activity_reading_lamp.
View view = findViewById(R.id.declared_inside_reading_lamp);
Then you can call
view.setBackgroundColor(Color.GREEN)
You have to ensure that all layouts in your activity_reading_lamp.xml have transparent background
You need to specify which background you need to set. For example, you can make id for the parent layout on your activity then do:
RelativeLayout parentLayout = (RelativeLayout)findViewById(R.id."your parent layout id and not your activity name");
parentLayout.setBackgroundColor(Color.TRANSPARENT);
If you would like to make a transition to the new color try this:
#SuppressLint("NewApi") private void tintColor(View rootView, String newColor) {
// currentColor can be given as a new parameter or set as a field
ColorDrawable[] color = {
new ColorDrawable(Color.parseColor(currentColor)),
new ColorDrawable(Color.parseColor(newColor)) };
TransitionDrawable trans = new TransitionDrawable(color);
int sdk = android.os.Build.VERSION.SDK_INT;
if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
rootView.setBackgroundDrawable(trans);
} else {
rootView.setBackground(trans);
}
trans.startTransition(ANIMATION_TIME); // ANIMATION_TIME : time in milliseconds
}
The code accepts hex color strings.
RelativeLayout rl = (RelativeLayout)findViewById(R.id.your_layout_id);
rl.setBackgroundColor(Color.RED);

Android : Crouton lib and custom font

I use custom fonts in my app so i want a custom font for Crouton. I 've tried to do it with setTextAppearance, it doesn't work.
<?xml version="1.0" encoding="utf-8"?>
<com.ecab.ui.custom.TextViewCustomFont
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.crouton"
android:id="#+id/crouton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/ban_confirmation"
android:gravity="center"
android:text="TEST"
android:textColor="#android:color/white"
custom:typeface="gothamBold" />
In Style class :
INFOCUSTOM = new Builder().setDuration(3000).setTextAppearance(R.id.crouton).build();
Then, I've tried to do it by changing setTypeface() with my font, it doesn't work.
In Crouton class :
private TextView initializeTextView(final Resources resources) {
TextView text = new TextView(this.activity);
text.setId(TEXT_ID);
text.setText(this.text);
text.setTypeface(MyFonts.getGothamBookBold(this.activity));
Log.d(Constants.D_TAG, "chaneg the typeFace");
text.setGravity(this.style.gravity);
// set the text color if set
if (this.style.textColorResourceId != 0) {
text.setTextColor(resources.getColor(this.style.textColorResourceId));
}
// Set the text size. If the user has set a text size and text
// appearance, the text size in the text appearance
// will override this.
if (this.style.textSize != 0) {
text.setTextSize(TypedValue.COMPLEX_UNIT_SP, this.style.textSize);
}
// Setup the shadow if requested
if (this.style.textShadowColorResId != 0) {
initializeTextViewShadow(resources, text);
}
// Set the text appearance
if (this.style.textAppearanceResId != 0) {
text.setTextAppearance(this.activity, this.style.textAppearanceResId);
}
return text;
}
What can i do to have a custom Font ?
ps : library version ==> 1.7
Okay, I found the problem !
It works with the second solution by changing the Typeface. I had just forget to remove the
setTextAppearance(R.id.crouton)
in the Style class. So my custom style is like this :
INFOCUSTOM = new Builder().setDuration(3000).setBackgroundDrawable(R.drawable.ban_confirmation).setHeight(LayoutParams.WRAP_CONTENT)
.build();
One problem resolves, another arrives :) ! With the background drawable, the text is not vertically center
You can a custom Style that uses the resourceId of your text
appearance via Style.Builder.setTextAppearance(...).
This takes a reference from your styles.xml and uses it within the
internal TextView of the Crouton.
Then you can call Crouton.makeText or Crouton.showText with your
custom Style.
Source
How does MyFonts.getGothamBookBold() look like?
This however should work:
private TextView initializeTextView(final Resources resources) {
TextView text = new TextView(this.activity);
text.setId(TEXT_ID);
text.setText(this.text);
Typeface myTypeFace = Typeface.createFromAsset(this.activity.getAssets(), "gothamBold.ttf");
text.setTypeface(myTypeFace);
text.setGravity(this.style.gravity);
// set the text color if set
if (this.style.textColorResourceId != 0) {
text.setTextColor(resources.getColor(this.style.textColorResourceId));
}

How to change Android title bar text color programmatically?

In my app I populate title bar label from each activity but the text color in the activity and text color in title bar is same. How do I change the title bar text color to a different one?
In your onCreate-method add the following:
setTitleColor(YOUR PREFERED COLOR);
You can implement a custom title bar (then you have to change the color of a simple TextView). Please check this: How to change the text on the action bar
you can do it from 2 places either in style.xml or proramatically
1.From style.xml : by Modifying TitleTextStyle -setting the android:textColour value as desired
2.Or in code Programatically:
int titleId = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
TextView yourTextView = (TextView)findViewById(titleId);
yourTextView.setTextColor(getResources().getColor(R.color.myColor));
use this one
setTitleColor(Color.BLUE);
you can choose different color from Color class.
ActionBar ab = getActionBar();
TextView tv = new TextView(getApplicationContext());
LayoutParams lp = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, // Width of TextView
LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(lp);
tv.setTextColor(Color.RED);
ab.setCustomView(tv);
For more information check this link :
http://android--code.blogspot.in/2015/09/android-how-to-change-actionbar-title_21.html

Categories

Resources