show textview in next line - java

TextView textView =(TextView)findViewById(R.id.text);
for(int i=1;i<6;i++)
{
textView.setText(i);
textView.setText("\n");
}
This is not printing
1
2
3
4
5
Please help me how to print the above output

whenever you call setText on a TextView you change the entire text.
you should create your String first and then set that as your TextView's text.
String text = "";
for(int i=1;i<6;i++) {
text += i+"\n";
}
textView.setText(text)'
another solution is using append instead of setText
for(int i=1;i<6;i++) {
textView.append(i+"\n");
}

I want from you in one comment to please show what it is printing right now ? and what you want to print ?
if you have 6 textviews then just do this
tv1.setText(""+i+"");
tv1.setText(""+i+"");
tv1.setText(""+i+"");
tv1.setText(""+i+"");
tv1.setText(""+i+"");

try this
String value="";
for(int i=1;i<6;i++)
{
value = value + i + "\n";
}
textView.setText(value);

Related

Checking for space in an edittext

I want to check if there's any space in an edittext. I use this method to get the selected text of edittext:
private String getSelection(EditText editText) {
int start = editText.getSelectionStart();
int end = editText.getSelectionEnd();
Editable edit = editText.getText();
return edit.toString().substring(start, end);
}
But when i do this:
if (getSelection(et).getText.toString.contains(" ")) Log.d("TAG","There's a space")
contains() will be always false.
I think this:
if (getSelection(et).getText.toString.contains(" ")).....
Must be like this:
if (getSelection(et).contains(" ")) .....

How to get index of element from edit text

How can I code the following:
For example, I have one EditText in which I insert a word (or words) like "abcdawa". I have another EditText in which I enter the search element. Let's say I want to search "a".
When I click a Button the indexes of the word in the first EditText which equals the search element should be shown in a TextView. Expected output: 1, 5, 7
Consider that you have 2 edittext with id edText_1 & edText_2, and a button
Now in on click of your button call following,
setOnClickListener{
String query = edText_2.getText().toString();
char[] dataArray = edText_1.getText().toString().toCharArray();
for (int i = 0; i < dataArray.length; i++) {
if (query.equalsIgnoreCase(String.valueOf(dataArray[i]))) {
textView.append(" " + i);
}
}
}

How to split string array?

It worked but it took the last song settext for all
for (int i = 1; i < items.length; i++) {
String SongsName = items[i];
String [] abcd = SongsName.split("_");
if(abcd[0] != null)
{
txtTitle.setText(abcd[0]);
txt2.setText(abcd[1]);
}
else
{
txtTitle.setText("Zero");
}
}
How does it show each line? Thanks in advance
After splitting the string, you end up with an array of song names. That array has an undefined number of items inside so you cannot just reference abcd[0] and abcd[1] and expect to see all songs. You need another loop that will iterate through all your song names in variable abcd.
Maybe I did not understand properly though, if variable abcd contains just one song name and you are just splitting to get the different pices of information for that song, then here is what is happening: you are looping through all the songs but you are overwriting txtTitle and txt2 everytime, so in the end, you end up with only the last one showing.

How to set text on textview from an ArrayList of unknown size

I am receiving a response from the server in an ArrayList of String. Now how can I set those strings on TextView? The size of the ArrayList is unknown.
Solution
When you do get the response from the server then you know the size of the ArrayList so try this method I think it should do the trick!
public void setTextViewValues (ArrayList<String> vals, TextView text) {
//Variable to hold all the values
String output = "";
for (int i = 0; i < vals.size(); i++) {
//Append all the values to a string
output += vals.get(i);
}
//Set the textview to the output string
text.setText(output);
}
Just add all values to one string value
String result = "";
for (String s : arrayList) {
result +=s;
}
And then set it
textView.setText(result);
What exactly you mean?
Display it in a row?
StringBuilder builder = new StringBuilder();
for(String s : arrayListName){
builder.append(s+" ,");
}
textView.setText(builder.toString());
you can simply convert your arry of string into a string with the method
Arrays.toString(YourArray);
wich will convert your arry into a string
then you simply put it in the textView.setText like this
textView.setText(Arrays.toString(YourArray));
And if you want it displayed without the special character you can just add this
textView.setText(Arrays.toString(YourArray).replaceAll("\\[|\\]", ""));

Need Help to optimize the following java code. It is the code for setting color of specific keywords in a textview.(Android Application)

I'm developing an app to view C-programs.I wanted to give a simple color scheme to the text which is stored in database,retrieved as string and then passed on to the textview.
The code I have written assigns green color to header file declarations and brackets,blue color is assigned to numbers,printf,scanf...red is assigned to datatypes such as int,char,float.
It is however very inefficient.Before applying this color scheme,my app was displaying the textview activity instantly.Now,depending on the length of the programs it takes up to 4 to 5 seconds which is really poor performance.
what it does is,it takes one keyword at a time,then iterates the complete text of textview looking for that particular keyword only and changes its color,sets the text again.
Thus,it traverses text of the entire textview 29 times as I have defined 29 keywords in String arrays( namely keywordsgreen,keywordsblue,keywordsred).
The activity's onCreate function contains the following code :
textView = (TextView) findViewById(R.id.textView1);
textView.setText(programtext);
textView.setBackgroundColor(0xFFE6E6E6);
//The problem stars here
String [] keywordsgreen={"#define","#include","stdio.h","conio.h","stdlib.h","math.h","graphics.h","string.h","malloc.h","time.h","{","}","(",")","<",">","&","while ","for "};
for(String y:keywordsgreen)
{
fontcolor(y,0xff21610B);
}
String [] keywordsred={"%d","%f","%c","%s","int ","char ","float","typedef","struct ","void "};
for(String y:keywordsred)
{
fontcolor(y,0xFFB40404);
}
String [] keywordsblue={"printf","scanf","\n","getch","0","1","2","3","4","5","6","7","8","9"};
for(String y:keywordsblue)
{
fontcolor(y,0xFF00056f);
}
The fontcolor function is as follows :
private void fontcolor(String text,int color)
{
Spannable raw=new SpannableString(textView.getText());
int index=TextUtils.indexOf(raw, text);
while (index >= 0)
{
raw.setSpan(new ForegroundColorSpan(color), index, index + text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
index=TextUtils.indexOf(raw, text, index + text.length());
}
textView.setText(raw);
}

Categories

Resources