Roboto light and Roboto bold in a TextView - java

Can I apply Roboto light and Roboto bold in same TextView on Android 2.3 like that?
**user** has been publish a beez
Where **user** is Roboto bold and has been publish a beez is Roboto light

Yes you can do ..
String firstWord = "user";
String secondWord = "has been publish a beez";
// Create a new spannable with the two strings
Spannable spannable = new SpannableString(firstWord+secondWord);
// Set the custom typeface to span over a section of the spannable object
spannable.setSpan( new CustomTypefaceSpan("sans-serif",CUSTOM_TYPEFACE), 0, firstWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan( new CustomTypefaceSpan("sans-serif-light",SECOND_CUSTOM_TYPEFACE), firstWord.length(), firstWord.length() + secondWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// Set the text of a textView with the spannable object
textView.setText( spannable );
You can use Roboto natively from Android 4.1+ like this:
android:fontFamily="sans-serif" // roboto regular
android:fontFamily="sans-serif-light" // roboto light
android:fontFamily="sans-serif-condensed"

Related

How to change text color of a String mentioned between a Text

I have a TextView. Setting text on Textview and including a String between text like below code.
How to change color and text style of String in Android.
tv.setText("some text"+String+"some text")
You can use Foreground Color Span class
String yourString = "color me red";
SpannableString spannableString = new SpannableString(yourString);
ForegroundColorSpan red = new ForegroundColorSpan(Color.RED);
spannableString.setSpan(red, 9, 11, Spanned,SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(spannableString);
The code above would change the color of "red" in your string to red as defined in ForegroundColorSpan. You would have to define where your span starts and ends, as in my case item 9 to 11 on the string array.
You can use SpannableString
Spannable span = new SpannableString("some text" + String + "some text");
wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 8, String.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
You could move your entire text to strings.xml and use CDATA
<string name="tvText">
<![CDATA[
some text <font face="sans-serif-thin" color=#000000> text to color </font> some text
]]>
</string>
In your code you would use Html Parser to load
tv.text = HtmlCompat.fromHtml(getString(R.string.tvText), HtmlCompat.FROM_HTML_MODE_LEGACY)
This trick worked for me. Thanks, everyone for the suggestions.
SpannableString spannableString = new SpannableString("Text "+String+" Text");
ForegroundColorSpan red = new ForegroundColorSpan(Color.RED);
spannableString.setSpan(red,4, 4+String.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(spannableString)

Vector drawable in any location within text view (not outside of it)

Is there a way to add a drawable in any position within a text view programmatically without having to position it on a particular side of a text view? The following code works when using unicode character but I want to try the same with a vector drawable.
textView.text = getString(R.string.app_settings) + " \u2794 " + getString(R.string.display)
For me, ImageSpan works.
You can put a delimiter and replace it with the drawable. I used a google icon in this example
:
Code with delimiter replacement:
Drawable drawable = ContextCompat.getDrawable(this, R.drawable.google_icon);
drawable.setBounds(0, 0, 100,100);
String text = " Google %google_icon% icon";
String delimiter = "%google_icon%";
int icon_index = text.indexOf("%google_icon%");
text = text.replace(delimiter," ");
Spannable span = new SpannableString(text);
ImageSpan image = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);
span.setSpan(image, icon_index, icon_index+1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setText(span);
Or, you can place the drawable on any index like:
span.setSpan(image, start_index, end_index, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
PS: I used Display1 in text appearance. You need to change drawable bounds according to your own needs.

How to set dynamically color for TextView in Android

In my application I want show some text into TextView, and I get this text from server.
I should use just one textView in XML layout, and I should set color for this Texts.
My XML :
<TextView
android:id="#+id/rowExplore_userName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/size3"
android:layout_marginTop="#dimen/padding8"
android:layout_toRightOf="#+id/rowExplore_imageCard"
android:fontFamily="sans-serif"
android:gravity="left|center_vertical"
android:text="Adam"
android:textColor="#color/colorPrimary"
android:textSize="#dimen/font14" />
JSON :
"name": "Adam Sandel",
"info": "liked",
"movieName": "SpiderMan",
Java code :
rowExplore_userName.setText(response.getName() + response.getInfo() + response.getMovieName() );
I want set color dynamically such as below image :
How can I it? Please help me
You can use HTML
String text = "<font color=#cc0029>First Color</font> <font color=#ffcc00>Second Color</font>";
yourtextview.setText(Html.fromHtml(text));
A better approach to this is to create a string resource with appropriate HTML tags:
<string name="html_string" formatted="false">
<![CDATA[
<font color="yellow">%s</font> %s <font color="yellow">%s</font>
]]>
</string>
And then get that string with resources:
String htmlString = getString(R.strings.html_string, response.getName(), response.getInfo(), response.getMovieName());
rowExplore_userName.setText(htmlString);
You can use SpannableString if you want to avoid using HTML tags
TextView textview = (TextView)findViewById(R.id.textview);
Spannable wordToSpan = new SpannableString("Your text is here");
wordToSpan.setSpan(new ForegroundColorSpan(Color.BLUE), indexStart, indexEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(wordToSpan);
and it is more flexible since you can make more changes to your text (text size, bold/italic, etc)
I used this code:
String colorStr="#FFFFFF" //put your hex color
int color = Color.parseColor(colorStr);
editText.setTextColor(color);
I would have another approach just create one method
public String getColoredString(String pname) {
Random rnd = new Random();
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
Spannable wordToSpan = new SpannableString(pname);
wordToSpan.setSpan(new ForegroundColorSpan(color), 0, pname.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return wordToSpan.toString();
}
and use above method
rowExplore_userName.setText(getColoredString(response.getName())+getColoredString(response.getInfo())+getColoredString(response.getMovieName()));
this should work.

Append image to TextView

I've tried for two day now to append an image to a TextView, but it didn't work. I have tried this for Android 6/API Level 23. ImageSpan, Html.fromHtml and other Methods did not work. So is it possible to append image (Bitmap) between other words in a TextView on Android 6?
EDIT
Here is the XML:
<TextView
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:text="------"
android:gravity="bottom|left"
android:typeface="monospace"
android:id="#+id/mainTextView1"
android:textIsSelectable="true"/>
And image span Code:
SpannableStringBuilder ssb = new SpannableStringBuilder(mOutEditText.getText()); ssb.setSpan(new ImageSpan(bitmapArray.get(0), ImageSpan.ALIGN_BASELINE), cursorPosition, cursorPosition+2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); mOutEditText.setText(ssb, BufferType.SPANNABLE);
Add image in xml
<TextView
android:drawableBottom="#drawable/ic_add_circle_black_24dp"
android:drawableEnd="#drawable/ic_add_circle_black_24dp"
android:drawableLeft="#drawable/ic_add_circle_black_24dp"
android:drawablePadding="#drawable/ic_add_circle_black_24dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
programmatically
textView1.setCompoundDrawables(left,top,right,bottom);
Or only left
textView1.setCompoundDrawables(left,null,null,null);
I have found the solution now for Api 21, 22, 23+:
private void appendImage(Bitmap bmp)
{
tv.setTransformationMethod(null);
SpannableString ss = new SpannableString(" ");
ss.setSpan(new ImageSpan(bmp, ImageSpan.ALIGN_BASELINE), 0, 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
tv.append(ss);
}
And in the XML:
android:textAllCaps="false"
The error was that in api level 21+ you have to set the TransformationMethod null.
If you want to set the background of the TextView use the following methods:
txt.setBackgroundResource(int rsid);
txt.setBakgroundDrawable(Drawable object);
If by append, you mean you want to add it to the end of the TextView, then use an ImageView for the image. Or you can overlay your TextView in an ImageView
It is not possible to add images between text in a TextView. You can add an image a TextView at the start and end. This is done with
android:drawableLeft

Android TextView horizontal scroll does not work

I read some thread in this topic, but I have not found the solution.
I would like to use the scrollHorizontally attribute on a TextView, but from Java code.
Here's what I tried:
nameTextView = new TextView(context);
nameTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
nameTextView.setId(R.id.header_text_id);
nameTextView.setGravity(Gravity.CENTER | Gravity.LEFT);
nameTextView.setSingleLine();
nameTextView.setHorizontallyScrolling(true);
nameTextView.setEllipsize(TextUtils.TruncateAt.MARQUEE);
nameTextView.setFocusableInTouchMode(true);
nameTextView.setMarqueeRepeatLimit(-1);
nameTextView.setFocusable(true);
Typeface tf = Typeface.createFromAsset(context.getAssets(), "fonts/robotocondensed.ttf");
nameTextView.setTypeface(tf);
nameTextView.setTextColor(getResources().getColor(android.R.color.white));
I set the text later. The text does not scroll, what can be the problem here?
android:maxLines = "AN_INTEGER"
android:scrollbars = "vertical"
Then set:
yourTextView.setMovementMethod(new ScrollingMovementMethod())
excelent article about how to make an horizontal scrollable textview in android, It works!!!
http://gabrielbl.com/2013/08/09/android-auto-horizontal-scrolling-marquee-textview/

Categories