My case is same as Android - three sentences, two styles, one TextView. The only difference is that I want to set a Custom Font on the whole text. I have included roboto Font as an assets in my project and want to apply that font to the TextView with first part of the text will be roboto_LIGHT and middle part roboto_NORMAL and remaining part roboto_LIGHT again. Any suggestions how it can be done ?
Text needed in following format. Custom text with different styles and single textview.
You can use some html tags like <b> for bold with the following code:
textView.setText(Html.fromHtml("Login to the <b>Web Wallet</b> on PC or MAC"));
Then you can define your custom font family as in How to create custom font family with bold font
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
<font
app:fontStyle="normal"
app:fontWeight="400"
app:font="#font/roboto_LIGHT" />
<font
app:fontStyle="normal"
app:fontWeight="700"
app:font="#font/roboto_NORMAL" />
</font-family>
The weight 700 is the font for bold style. And then you should apply that created font family to the TextView:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:fontFamily="#font/my_custom_font"/>
Or you can add 3 TextViews in a ConstraintLayout and give them some constraints to be in the positions you want otherwise.
Related
Since Android XML layouts are all rectangular, how can I have just some parts of the TextView with different font sizes and params?
In the example below, the time (which is in grey and has a smaller font size) is always at the end of the text. I don't know how to reproduce this in XML. If I separate the text in two different textviews and the width of both were wrap content it would just appear at the right of the TextView Layout (at the right of every line from the main message, not at the end).
Is it possible to do this layout configuration with XML or do I need to do it programmatically using JAVA? How can I change the params of certain parts of a textview with Java?
With two TextViews and Wrap Content (not what I want):
What I have understood from your question is you want a single textView with multiple style inside this. to do so you can do this:
SpannableStringBuilder text = new SpannableStringBuilder(); // a spannable string builder
text.append("your friend "); // appending first text
SpannableString fname = new SpannableString("Joana"); //making a span that would have different style
fname.setSpan(new StyleSpan(Typeface.BOLD), 0, fname.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE); // set different style
text.append(fname); // append value
text.append(" his sending you a message ");
SpannableString time_ago = new SpannableString("41 min ago"); //another span
time_ago.setSpan(new RelativeSizeSpan(.7f), 0, time_ago.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
time_ago.setSpan(new ForegroundColorSpan(Color.GRAY), 0, time_ago.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
text.append(time_ago); // append value
textView.setText(text);
XML:
<TextView
android:padding="5sp"
android:textColor="#color/black"
android:textSize="24sp"
android:gravity="start"
android:id="#+id/tvtest"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
I am building a rich text editor.
I have implemented text formatting like bold italic etc and also paragraph formatting like blockQuote. Now I would like to add images in editor and text should wrap around it.
I have implemented all these using SpannableString() and Spanned() and StyleSpan().
I can add image to a line using ImageSpan(), but that add it inline and its just there in place of a character
, what I want is to insert it in paragraph and rest of text should wrap around it. I am able to add image at the beginning of text by following code.. but I cannot align it center and right.
SpannableString string = new SpannableString("Text with icon and padding");
string.setSpan(new IconMarginSpan(bitmap, 30), 0, string.length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
How to do it ? any example? or what procedure to follow?
you can fulfill your requirement like below:
you can set your image in imageview and your text in Html.fromHtml("here put your text")
gradle file:
compile 'com.github.deano2390:FlowTextView:2.0.5'
your XML layout:
<uk.co.deanwild.flowtextview.FlowTextView
android:id="#+id/ftv"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="400dip"
android:padding="10dip"
android:src="#drawable/android2"/>
</uk.co.deanwild.flowtextview.FlowTextView>
your java file:
FlowTextView flowTextView = (FlowTextView) findViewById(R.id.ftv);
Spanned html = Html.fromHtml("here put your text");
flowTextView.setText(html);
you may get help by this link https://github.com/deano2390/FlowTextView also
I hope it help you..!
As per details given by you, It seems you might be using native EditText or you are trying with your custom EditText. Please provide this details to get exact solution of your query.
Apart from that, To build Rich Text Editor with the features mentioned by you (like Bold, Italic, Quote and Images etc), you can use this library RichEditor for Android: https://github.com/wasabeef/richeditor-android. It provides many features which may interest you.
Try with:
EditText text = (EditText)findViewById(R.id.text);
text.setCompoundDrawables(null, null, getResources().getDrawable(R.drawable.check_box), null);
In my app i want to convert my english font into hindi, for that i did localization. that is not supported for lower versions(i.e 2.3). Then i downloaded "shusha.ttf" file and added to assets/fonts folder. Then i set the typeface for a textviews. Like this i am able to convert the english font to hindi. but when i get the text from the text view it is not showing in Hindi(getting the english font). i am using that text to send the mail through gmail.
If any body have idea about this please give me a suggestion.
This is my Code
t = ((TextView)findViewById(R.id.textView1));
t2 = ((TextView)findViewById(R.id.textView2));
Typeface Hindi = Typeface.createFromAsset(getAssets(), "fonts/shusha.ttf");
t.setTypeface(Hindi);
t.setTextSize(20);
String hello=" Hyderabad, Andhra Pradesh, India ";
t.setText(hello);
String lang=t.getText().toString().trim();
t2.setText(lang);
Instead of using and playing with font, use UNICODE characters for hindi words. I had done it for a project of mine and it works great.
You have to change the font of the text of text view.
First put the font in your asset folder.
Then in your java file add code like this to change your font:
Typeface myTypeface = Typeface.createFromAsset(this.getAssets(),
"shusha.ttf");
TextView digital= (TextView) findViewById(R.id.your_textView);
digital.setTypeface(myTypeface);
Put shusha.ttf in your asset folder of your project.
This will might help you.
Create styles in values/styles like,
<style name="MyStyle" parent="#android:style/Theme">
<item name="typeface">font_in_assest.ttf</item>
</style>
Here, font_in_assest.ttf is the font you placed in your assests/fonts.
Then assign the style for you view,
<TextView
android:id="#+id/title"
style="#style/MyStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
It will be helpful for you..
I am using a custom Group indicator as shown in the code below. Now if I leave the default I can use setBounds perfectly, but when using my custom image which is the same dimensions and setup as the same .9 patch file it is always half off screen. No matter what values I use for setBounds()
Drawable plus = (Drawable) getResources().getDrawable(R.drawable.expander_group);
getExpandableListView().setGroupIndicator(plus);
Display newDisplay = getWindowManager().getDefaultDisplay();
int width = newDisplay.getWidth();
getExpandableListView().setIndicatorBounds(0,0);
XML expander_group
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_expanded="true"
android:drawable="#drawable/minus" />
<item
android:drawable="#drawable/plus" />
</selector>
EDIT:
Interestingly. I have copied the exact xml and image files from the standard Android files. And when I use the above method to define it the exact same thing happens! So it is not the xml or image files that are causing the issue I think.
I couldn't find information on how to do this anywhere? I want to define a background color in the xml layout of the activity. How do I do this?
Take your outer layout (e.g. a LinearLayout) and set its background attribute to a color.
<LinearLayout android:background="#color/mycolor"
.... />
These colors can be defined in the res/values/colors.xml file (see here how to do this).
You can also define a color directly at the attribute (android:background="#ffff0000"), but that's usually not good. By defining the colors in the XML file you can give it a descriptive name (improves code readability) and you can reuse it somewhere else.
Edit:
Theres an example in the doc I linked, but here is a short example how it looks:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="red">#ffff0000</color>
<color name="green">#ff00ff00</color>
</resources>
Its basically a resources tag containing multiple color tags. Each color has a name attribute (which you use to reference the color) and an actual color. That is defined between the color tags in hex. See the docs for possible formats. This one is #AARRGGBB, where A=alpha (transparency), R=red, G=green and B=blue. This example file contains a full red and a full green color. They can be referenced via #color/red and #color/green.
there is one thing need to mention is that the "android:background="#ffffffff"" setting does not work if this sentence is applied to an include directive.
for example,
<include
android:id="#+id/fragment_printer_detail_property_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="#dimen/printer_detail_group_vertical_margin"
android:layout_marginLeft="#dimen/printer_detail_group_horizontal_margin"
android:layout_marginRight="#dimen/printer_detail_group_horizontal_margin"
android:layout_marginTop="#dimen/printer_detail_group_vertical_margin"
layout="#layout/module_printer_detail_property"
android:background="#color/module_printer_detail_group_background_color" />
the "android:background" should be set in the layout file of module_printer_detail_property.