How to do I write a fraction in android studio. What's the best practice for a fraction with a horizontal line I am trying to produce.
Use html format. However you must add a extra space on the top and bottom to keep it from being cut off.
SpannableStringBuilder test = new SpannableStringBuilder();
test.append("\n");
test.append(Html.fromHtml("<sup>5</sup>/<sub>9</sub>"));
test.append("\n");
You can try this solution: http://blog.sqisland.com/2014/11/android-stacked-fractions.html
Basically, you have to looking for a font that supports afrc. Now, you can play with your TextViews and TagHandlers until you get the result desired.
The solution does not seem simple, however if you want to try fraction in xml, go to this website.
http://unicode-search.net/unicode-namesearch.pl?term=fraction
This code worked for me to show 1⁄4.
The xml code is.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="\u00BC"
android:id="#+id/textView"
android:textSize="25sp" />
The required line is android:text="\u00BC" to show 1⁄4 as fraction.
So if you want to show 1⁄2 or any other as fraction from the above mentioned website, just append the last two characters(in this case BD) to \u00.
So 1/2 becomes \u00BD
Related
I need to show SmallCaps text in TextView. I am trying to use code below:
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFeatureSettings="smcp"
android:textColor="#color/black"
android:textSize="16sp"
android:text="TEXT" />
But it just shows me normal text, without SmallCaps effect. I also try to set it programmatically (text1.fontFeatureSettings = "smcp") but also without success.
How to make SmallCaps text in Android? Why fontFeatureSettings doesn't work?
In order for this feature to work you have to have a font that actually has Small Caps characters - thus to make it work pass the font with Small Caps via app:fontFamily(android:fontFamily) for example Aller Display, and then use android:fontFeatureSettings="smcp" to make it Small Caps.
Hope it helps
Edit
as #Cliabhach pointed in comments, in code it will look something like
text1.typeface = resources.getFont(R.font.Aller_Display)
text1.fontFeatureSettings = "smcp"
Edit2
Keep in mind
For those who do not know - Small Caps will only work on lowercase characters
As you are using TextView so I guess you set value to it at runtime like says you have some String object which will get set to the TextView like:
textView.setText(someStringObj);
So you can easily set it as in lower case on it by adding:
textView.setText(someStringObj.toLowerCase());
I want to use a list separator in my .xml file, but everytime i do that, the text style changes, it becomes bigger and bolder.
Below are links of the screenshots. The picture on the left is what shows up from my code, and on the right is what i really want.
Picture for output
I only want to know how to stop the text from getting bigger and bolder, and at the same time how to put a horizontal line below the <TextView>.
Below is the link for the code
Code
Building from #John Kalimeris' answer, i have a better way, in which no foriegn resource will be needed. This should work for you.
<TextView
android:layout_width="match_parent"
android:layout_height="5dp"
style="?android:attr/listSeparatorTextViewStyle"
android:layout_marginBottom="3dp"/>
for the horizontal line you can use this code:
<TextView
android:id="#+id/tevi_divide"
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_margin="3dp"
android:background="#color/detail_line_divider"
android:text="#string/str_empty" />
you put this below your textview. You change the values of height and margin according to your needs.
I am trying to create a separator with header per Google Material Design spec, and I couldn't get it to work. Why would google roll out a spec that is not even available on either support or target platform level? So here I am recreating material spec to work with the older version and here is what I got so far:
<TextView
android:id="#+id/textview_task_header1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="42dp"
android:layout_marginBottom="12dp"
android:drawableTop="#drawable/drawable_separator"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#bfbfbf"
android:text="Location"/>
I want to create a header, a separator of some sort. I don't want to use list because list will require me to create an adapter which I find really impractical in my case. I have to layout the UI using XML and I can't figure it out.
I need something to look like this, I need to have a header without the line separator and a header with one separator:
Also, I was able to achieve to create a header via TextView and set margin top and bottom. The problem is I can't show the drawable line on my case and I don't feel like I am doing the right thing here. Its too messy.
I appreciate if you don't post an answer indicating the use of a hackish solution. I don't like to go down that path for some reason. :/
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#android:color/darker_gray"/>
To add vertical separator, switch the layout_width and layout_height values
I've read through both of these questions, which seem to have the same problem i'm struggling with, namely I have text for my buttons that i'd like to display as a superscript. In my fragment class in the onCreateView method I have added
if (rootView.findViewById(R.id.squared) != null) {
((TextView) rootView.findViewById(R.id.squared)).setText(Html.fromHtml("X <sup><small> 2 </small></sup>"));
rootView.findViewById(R.id.squared).setOnClickListener(this);
}
if (rootView.findViewById(R.id.cubed) != null) {
((TextView) rootView.findViewById(R.id.cubed)).setText(Html.fromHtml("X <sup><small> 3 </small></sup>"));
rootView.findViewById(R.id.cubed).setOnClickListener(this);
In my fragment_main the buttons are coded as
<Button
android:layout_height="match_parent"
android:layout_width="0dp"
android:fontFamily="helvetica"
android:id="#+id/squared"
android:layout_weight="0.25"
style="?android:attr/borderlessButtonStyle"
android:textSize="15sp" />
<Button
android:layout_height="match_parent"
android:layout_width="0dp"
android:fontFamily="helvetica"
android:id="#+id/cubed"
android:layout_weight="0.25"
style="?android:attr/borderlessButtonStyle"
android:textSize="15sp" />
However, when I run my application the layout doesn't any superscripted text.
Even when I change the text size from 15sp to 10sp, the text only gets smaller, but not superscripted. What am I not doing correctly?
I'd comment if I could, but at this point I can't. I JUST posted something similar to this, an issue I'm having as well. I had my problem solved halfway. The method Html.fromHtml works for me only on a textview, but not on my butons. I just wanted to say this in case it helps you or anyone else figure out how to employ this method on the buttons. I'd very much like the answer myself.
Basically:
textview.setText(Html.fromHtml("x<sup>2</sup")); displays properly
BUT button.setText(Html.fromHtml("x<sup>2</sup")); displays x2 (without superscript).
EDIT: AlanV helped me out. Here's a link to the solution he provided someone else. https://stackoverflow.com/a/29056795/4535064
All you need to do is add this to your button:
<button
android:textAllCaps="false";/>
The new version 5.0 (I think) forces buttons to produce all caps. This is something I noticed when I produce the same string in a textview and button, and the button was all caps whereas the textview was not. Because of this, you don't see the HTML formatting such as and . Setting the textAllCaps feature to "false" allows the HTML to be utilized.
Again, giving credit where it's due, AlanV is the man! :-)
This is a known issue, as shown here.
I think the best thing to do is to set android:textAllCaps="false" , and then to set the text to be capitalized by yourself.
I think an image will explain this far better than words. The screenshot is from the layout viewer thingy in Intellij, but this happens on device as well. I want the cut off text to not be displayed at all. Is this possible?
<TextView
....
android:lines="2"
or:
<TextView
....
android:maxLines="2"
First option sets exact number of lines for your text view, second options sets the maximum number.