Android Development: Line Spacing With Line Numbering - java

In my app I have line numbers to the left of an EditText - everything is great so far, the line numbers are perfectly aligned with the lines of the EditText.
The problem is, if the user changes the text size of the EditText the line numbers aren't aligned properly. So I've added code so when the text size of the EditText is changed, the line numbers text size is changed too; but that causes another problem: if the user picks a big text size, the line numbers hog all the space, so there's barely any room to write in the EditText.
The only solution would be to set line spacing. So I eventually came across the setLineSpacing() function, and have tried using it based off tutorials but I don't understand how it works and can't get it aligned with the EditText's lines.
Can someone help me understand how to use it, or suggest a different way of getting each line in the line numbers TextView aligned with the EditText's lines?
Sorry if this is confusing, I'm not very good at explaining things well sigh.
Thanks,
Alex.

I faced a similar situation in the past, in which the EditText's size was limited. It was decided that the max font size that can be chosen should be limited to a value in which the visual appearance does not look ugly. I feel it is useless to plug cases that can occur but don't make sense. It is better to block such cases. Perhaps you can think along these lines.
HTH,
Akshay

Related

Spannable text in a TextView breaks line randomly after the substring "K/" when at least one of both characters has a Span defined on it

I've discovered a puzzling bug that, from what I can tell, only happens when a TextView has a Spannable text that contains the substring K/ and one or both of these characters have a ForegroundColorSpan (or maybe spans in general?) set on them (but are not in the same span). The effect is that all text following the / is forced on a new line, although I've noticed that other symbols like another / or a . directly following the K/ stay on the same line.
Additional Note: Both K and X behave the same here, regardless of capitalization.
Needless to say, I have no clue what is going on. My best guess would be some kind of weird formatting interpretation.
What my TextView/MaterialButton should look like:
What it looks like on my phone:
It is not an issue with the defined width of the TextView, which is set to wrap_content. It works fine on all other devices I've tried, even with far longer Strings. It also happens on a completely default TextView with both dimensions set to wrap_content.
Some code that recreates the issue on my phone:
// Create String which includes "K/"
String textString = "1K/2K";
// Create SpannableString and set a color span up to the first "K"
SpannableString spannableString = new SpannableString(textString);
spannableString.setSpan(
new ForegroundColorSpan(someColor),
0,
textString.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
myMaterialButton.setText(spannableString, TextView.BufferType.SPANNABLE);
This happens 100% of the time on my Samsung J4 Plus with Android 9 (API 28). I've tried to reproduce it on a Pixel 5 Emulator with several Android versions including 9, with the exact same data and situation but there it all works as expected.
If anyone has any kind of insight, tip or any kind of information why this might be happening, I'd be eternally grateful.
Update: Inserting ⁠word joiners (⁠⁠⁠\u2060) at both sides of the "/" solved the issue. I guess it might have something to do with the Unicode Line Breaking Algorithm that TextView uses.

Adjust Line Spacing Java

Is there a way to adjust the spacing between new lines when outputting to the console through System.out.println? I'm attempting to print out a square with a basic nested for-loop, but I keep getting a rectangle despite having the right number of characters. This is because the spacing between the characters is different than the spacing between lines. Any ideas?
The standard output stream is just a stream of data; how that data is displayed is up to the application displaying it (e.g. a terminal or your IDE). You'll either have to settle for a rectangle, or find a different method of output than standard out.
Without your current code, it's hard to say exactly whats going on. My first thought is, make sure you're using a character that is as high as it is wide, e.g. *.
Second thought is (and again, I have no code to reference), see if you get the same result using one of the algorithms here: Printing a Square with loops, perhaps there is an issue with the square-printing logic that you overlooked.

Blank space in the start of the Text in Listview

Hey, I have a simple list which contains Strings in Arabic, the problem is that some strings get a blank space on the begging of the word, so not all the words are allaigned properly..
Here is a screen shot of what I mean:
As you can see in the 3rd and 4th line, there is a blank space before the name, and the source of the strings there is no blank space.. Here are the 2 lines that get blank space that are in strings.xml:
<item>التاريخ الإسلامي</item>
<item>الحضارة الإسلامية</item>
Can anyone tell me why they get blank space?
Thanks.
It does not seem that you are doing anything wrong there. Try another font, as this might be a font issue. If that does not work, a workaround I would suggest you to set that text in code in the meantime.
If it is reproducible with several fonts, I believe you should post a bug report here:
http://code.google.com/p/android/issues/entry

Text is not fully shown in JTextField

They say a single image is worth 1000 words:
I'll just note that the size is set to default. (build in NetBeans)
any idea how do I fix this?
Adam.
Without you showing code, I'd say that your JTextField width is not set to be wide enough. You can resize it to be large enough for the number of characters you anticipate.
However, this does not guarantee that the user will not type in more characters, which would show the text cutoff as well.
You can extend the Document that JTextField uses to add the maximum character restriction, as shown at
http://www.rgagnon.com/javadetails/java-0198.html
what are the lengths of your data,it seems you changed the layout and that's causing that problem as the border seems also occupying half of the character.
They say a single image is worth 1000
words:
Actually its not. When posting a question a SSCCE is worth 1000 words.
Stuff like that usually happens when you don't use a layout manager. Assuming (which is all we can do since you didn't post any code) that you are using a proper layout manager then your basic code for creating a text field to display 3 characters is:
JTextField width = new JTextField(3);
The reason is the LNF that was assigned to the frame, once I've changed that, it all works fine.

Restrict number of lines in html JLabel

I have a JLabel that needs to display some html-formatted text. However, I want to restrict this to being 4 lines long (and if so, provide a button to see everything).
So far, I've tried setting the maximum size manually or via a layout manager. However, both of these solutions can cause part of a line to be displayed.
edit: To add a little more details, I need to force 4 lines even when respecting line wrapping correctly, resizing components, and changing font sizes. I've considered handling resize/fontsize changes by replacing the label with a new one that fits correctly.
JLabel seems to handle incomplete tags well, so I could probably do something like a binary search on the input string finding which character would cause it to go over the 4 line limit (using FontMetric to determine how many pixels 4 lines would be), and then replacing an existing label with the new one. The big downside to this approach is that I need to run the computation every time the user resizes the panel or changes fonts (and it feels like a dirty dirty hack).
Add the JLabel to a JScrollPane as set the scrollpane with a reasonable preferred size. Scrollbars will appear a necessary.
I don't know of any absolute solution to the questions since I doubt you can define what a "line" is. One line of text may be font 12 and another 24. I don't know of any way to calculate the height of each given line.
Even if you did use a ComponentListener to handle the componentResized() event I'm not sure you can come up with a reasonable algorithm to to calculate the exact width/height of of a 4 line display.
I would try running through the String of text and removing all text after the third "\n"
String shortenText(String oldtext){
String newText = "";
for(int i=0;i<3;i++){
newText += oldtext.substring(0,oldtext.indexOf("\n"));//adds one line to String
oldtext = oldtext.substring(indexOf("\n")+1);//shorten old string to prepare for next iteration
}
return newText;
}
You may also want to try the same algorithm, except strip of <p> and <br> tags as well...
If you know the values of the possible tags just switch the text from "\n" to "<br>" or any tag you need
Hey, I found a way that works. The framework I'm working with allows me to create a listener for font size changes. In this listener, I determine what the new max size of the label is (getFontMetrics(font).getHeight() * 4) and then re-set the maximum height on the label to this and then relayout everything. This even handles the word wrap case well. I'm guessing that someone could do nasty things with silly HTML input, but this covers the 99% case pretty well.

Categories

Resources