Using a Long String as multi line textview - android

I am developing an calculator app. That app take input using edit text and shows the value in a textview using setText. Some times the input get bigger and it becomes hard to fit the output in a single line. That's why I need to spilt the output into multiple line.
I've used some line separator solutions from SO but nothing properly works for me.
Please give me a solution to show a way to show the output text into multiple line. Please don't flag as similar .
Note: All my input and output are number and they have no space or punctuation mark in them.
Thanks in Advance.

Im not allowed to comment yet. So have u heard of "Spanned"? Im Using kotlin and Assuming max items in one line is 20 .
Try This:
String yourString = "1010101110001101011010011100111000111100001110000";
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(yourString);
for(int i = 0; i<yourString.length();i++){
if(i%20 == 0){
stringBuilder.insert(i,"<br/>");
}
}
Spanned spannedString = Html.fromHtml(stringBuilder.toString());
yourtextView.setText(spannedString);
feel free to give feedback.

Related

How to edit a TextView like below?

i am kind of starting coding in android studio. I am trying to get my TextView to be displayed in the app like the below example:
1
2
3
4
5
.
.
.
Like this
Is it possible to change the TextView itself withing the Java xml text Activity or must there be a code written in the mainactivity?
If any of the named above, could you please reach out for me the code for that with a simple and short explanation of what is happening and why is it needed?
Thanks a lot in advance guys!
Have a nice day!
Regards
If you include in the text \n after every char you get what you want:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1\n2\n3\n4\n5\n.\n.\n."
you just need to add new line after every letter, for do it, you need to use simple loop to get letter by Index, and String to cat result, then set result in the TextView after loop complete, this is the java code, just copy past and change textview id to your textview id:
TextView myTextview = findViewById(R.id.textview_id);
String Result = "";
for(int i = 0; i < myTextview.length(); i++){
Result = Result + "\n" + myTextview.getText().toString().charAt(i);
}
Result = Result.replace(" ", "");
myTextview.setText(Result.trim());

JSON string to TextView newline

First of all, I have gone through questions similar to the problem I am facing and those solutions are not working for me.
I have a TextView field on my Android app which is supposed to display multiple paragraphs i.e multiple new lines. I am getting this string from a database present in my online server as a JSON.
The text contains \n in it and I am expecting it to create new lines once it is received by the app. But it displays the whole text without any breaks along with "\n" character.
Below is the text present in my database.
First line. \nSecond line. \nThird line.
JSON string received by me inside the app.
{
"server_response": [{
"news_expand": "First line. \\nSecond line. \\nThird line."
}]
}
Code to extract string from JSON. I have left out the code to get get JSONArray and JSONObject for simplicity.
na_expand = gna_jo.getString("news_expand");
String extracted from the JSON. Got this by printing the na_expand string.
First line. \nSecond line. \nThird line.
Code to display the text in the TextView. Note the below 'na_expand' is an SparseArray present in a different activity hence the 'get(position)' code.
art_expand.setText(na_expand.get(position));
Below is the text I get on the emulator.
First line. \nSecond line. \nThird line.
What am I doing wrong here?
I think you should replace \n with \n in your string before setting test to your textview same below
b= b.replaceAll("\\n","\n");
So I found a workaround to the problem. As I was not sure where the issue was happening with \n, I modified my text present in the database to have a symbol other than \n. For eg: ~
First line.~Second line.~Third line.
You can use a website like this - https://www.gillmeister-software.com/online-tools/text/remove-line-breaks.aspx to replace the line breaks with any symbol you want.
Next, I used the StringSplitter class to break the string received in JSON and then again join it together with \n.
String joined;
String expand_temp = na_expand.get(position);
TextUtils.StringSplitter splitter = new TextUtils.SimpleStringSplitter('~');
splitter.setString(expand_temp);
StringBuilder stringBuilder = new StringBuilder();
for (String s_temp : splitter) {
stringBuilder.append(s_temp + "\n");
}
joined = stringBuilder.toString().trim();
This worked! I used this string in setText.
art_expand.setText(joined);
Try below code
myTextView.setText(Html.fromHtml("yourString with additional html tags"));
It will resolve all the html tags accordingly and effects of the tags will be reflected as well.
NOte: For devices greater than Nougat use below code
myTextView.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>", Html.FROM_HTML_MODE_COMPACT));
Hope that helps
The \ character is an escape character in JSON. So, when you get \\n, it actually means \n, not the newline character, which should have been just \n. So what you see is an expected behaviour. The JSON you get should have ideally been:
{
"server_response": [{
"news_expand": "First line. \nSecond line. \nThird line."
}]
}
Get your server to respond properly, otherwise you'll have to strip the unnecessary \.
Do you haveandroid:singleLine="true" on your TextView? If yes it will ignore the \n and will place the text in a single line.
You can just add replaceAll("\\n","\n") when you set value to your art_expand EditText. It should be:
art_expand.setText(na_expand.get(position).replaceAll("\\n","\n"));

Hide only part of text in TextView

Good Day i want to hide some specified or certain part of text in textview!Important: Im not talking about hide the full textview with TextView.setVisibility(View.Gone) I'm not talking about transparent of TEXT in textview!im not talking about hiding full text in textview!So please help me to hide some text.
Example: lets say i have a textview with this text (10-Sporting Goods)
I want to hide the (10-) and show only Sporting Goods text.Any help will be appreciated!Thank you very much beforehand!
Although even i would appreciate for your case to strongly go with DroidWorm/Gabriella approach , just for the information of all the other folks who may see this in future.
If you really wish to hide just a portion of your textview which has the entire string in itself, you should use a SpannableString , as below:-
tvHello = (TextView) findViewById(R.id.tvHello);
SpannableString customText = new SpannableString("10-Sporting Good");
customText.setSpan(new RelativeSizeSpan(.1f), 0, 3, 0);
tvHello.setText(customText);
This code will technically HIDE the 10- from 10-Sporting Good without using a substring.
You could try to get the whole text like
String text = textView.getText().toString();
and then make substring of it like this:
String wantedSubstr = text.substring(4); //for example - everything from the 4th index to the end
then set this substring as text of your textView like this:
textView.setText(wantedSubstr);
There is one the possible solution of it is that..First you have to find the index(position) of "-" and than split the string according to it therefore use below code
String text = textView.getText().toString();
int position=text.indexOf('-');
String wantedSubstr = text.substring(position+1);
textView.setText(wantedSubstr);
Will there always been "10_" in front of it? Or will there always be 3 characters before the text you want? Or will there always be a "-" or "_" before the text you want?
If so, you could just do a simple method which takes the substring and then updates the textview. If so I can help you write a simple method
You cannot hide part of textView, instead you can make a substring of the specific string and setText using it.
Do it like:
String originalString = "10-Sporting Goods";
String subString = originalString.substring(3);
textView.setText(asubstring);

String manipulation In android for printing

In my application need to send String to a printer, with structured String
Like first line is bold and middle of the printing page.
After draw line and make three column
Item code item quantity
set items in each row
String line="_____________________________________________________________________"+"\n";
String cl= " <H1>Company Name(CLPPL) <H1>"+"\n";
print=line+Html.fromHtml(cl)+line;
I am trying Like above
I am using Html but it is not working
Please Help Me how i can make this kind of the String
Thanks IN Advance
Embedded html in Java code.Use StringBuilder class to append string one by one.
Use hint
StringBuilder stringBuilder=new StringBuilder();
stringBuilder_month.append("<html><body>");
stringBuilder_month.append("<font color="+"#413839"+"><h3 align="+"center"+"<b1>");
stringBuilder_month.append(" ");
stringBuilder_month.append("Your Heading</b1>");
stringBuilder_month.append("&nbsp&nbsp&nbsp&nbsp");
stringBuilder_month.append("</body></html>");
Add html tags as you required.You need to modify it according to your needs. This code will solve your problem

having unneeded spaces between lines

I am writing my data in a database and getting it in android to set it in a textview but when I write for example:
"hello,
how are you?"
which means each line on a row if the row in the phone completes I get an extra line space and I don't want this need to remove it how can I do that?
thanks.
EDIT:
how can I place a line space in an arabic database: I added \n didn't work "\n" didn't work '\n' didn't work!! any ideas?
even if the database wasn't arabic that didn't work so can I place the \n in the database in a way that the android program takes it as a line space?
Edit2
Is there any other method? other than the one that I've mentioned in my answer? thanks.
check to see if you insert to your DB with that extra line.
showing more code would help us understand whats causing your issue...
but if you want to brute force it you could always use substring() for each or your elements or
`String input = EditTextinput.getText().toString();
input = input.replace(" ", "");`
but like #Nitin Sethi said more code especially the entry to your DB would give us a better understanding of what's going on
so what I did is that I've added the word "line space" in my database and I added
in my android code:
String input = //here I take the cde from cursor;
input = input.replace("line space", "\n");
and it worked perfectly but is there another way?

Categories

Resources