How to edit a TextView like below? - android

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());

Related

Using a Long String as multi line textview

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.

How can I apply setEllipsize(TextUtils.TruncateAt.END) on EditText in Android?

The effect I want to achieve is shown as below:
I expect that when the button is clicked and if the text length is longer than the EditText's width, the "..." be appended at the end.
I tried in 2 ways.
One is like this:
if (str != null && edt.getWidth() <= edt.getTextSize() * str.length()) {
String dotsString = str.substring(0, (int)(edt.getWidth()/
edt.getTextSize()) - 1) + "...";
edt.setText(dotsString);
}
It looks fine, but when the text is mixed by different languages, Chinese and English characters, for instance, the result will not neet my expectation.
And the another way I tried is like this:
TextView edtTv = (TextView) findViewById(R.id.edit);
edtTv.setSingleLine(true);
edtTv.setEllipsize(TextUtils.TruncateAt.END);
edtTv.setText(edtTv.getText().toString());
But it does not work.
I'm so confused! Any help will be appreciate.
The issue with EditText ellipsis not working is caused by horizontal scrolling. Refer to this answer

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);

How do i set equal characters in a line in a text view?

So basically by default the text view in android wraps contents because of which my text looks something like this
I'd like to disable the text wrapping property and set equal number of characters in the text view.
How do I do it?
Your question is not 100% clear but if you're talking about justification, Android doesn't support it. But here is a library which does.
If you literally don't want the text to wrap use:
android:singleline="true"
You have to use single line "true", and define the padding between the cells.
ps: to set a number of characters , you have get the refference from this textView and edit the content(just format the string).
Example:
char text[] = originalText.toCharArray();
String newText = "":
for(int i=0; i< text.length(); i++){
if(i<x){
newText =newText + text[i];
}
else{
break;
}
}
To achieve this, simply use the newline syntax "\n" where you want a new line to begin.
For example:
String text = "Who is the Boss? \n You are the Boss";
This can also be achieved programmatically of course in code if you're getting a string without one.
Simply write a method that checks for white space and insert the "\n" say after each successive 3 whitespaces have been detected. Then programmatically set the string to the TextView.
try adding this attribute to your textview and then try
android:gravity="center_horizontal

showing a integer number in edit text field in android

I am trying to do the following :
t1 is an edit text
a and b are two integers
t1.setText(a+b);
but this is not working in android but it works perfectly with javaswing
A simple solution would be:
t1.setText("" + (a+b));
or maybe:
t1.setText((a+b).ToString());
please do like the following
EditText et=(EditText)findViewById("ID of your EDITTEXT");
int c=a+b;
String s=c.toString();
et.setText(s);
This'll work fine :) If it works vote me :)
but this is not working in android but it works perfactly with
javaswing
It will not, because you're actually calling setText(int resId). See here. Calling this method will search a string in xml resources (E.g. strings.xml). Every id of a string (E.g. #strings/hello) will be compared to resId, If resId matches the string's id that string will be displayed to that widget or else you wil get a ResourceNotFoundException
To display the actual integer, convert it first to String
t1.setText(String.valueOf(a+b))
You are doing like this t1.setText(a+b); it search this id (a+b) in resource file like strings.xml that is not available. So it will throws a exception ResourceNotFoundException..
So to set the number in text view you need to convert it into string.
In Android you need to do like this:-
int sum = a + b;
String sumString = String.valueOf(sum);
t1.setText(sumString);
OR
t1.setText((a+b) + "");

Categories

Resources