-2

The following is my string. I would like to display the string in textview one by one. Means 1st text should get replaced by the 2nd in same textview and so on. Please suggest me how to do this.

String str = "அவனுடைய கையும் இயல்பாக உறைவாளிடம் சென்றது.\nஇந்த சாலை தஞ்சாவூருக்குத்தான் போகிறது.\n" +
            "ஆனால் இதில் முக்கியமானவர்கள் மட்டுமே போகலாம் மற்றவர்களுக்கு வேறு சாலை இருக்கிறது என்றான் வீரன்.\n" +
            "அப்படியா ஆனால் நானும் ரொம்ப ரொம்ப முக்கியமான மனுஷன் தான் என்றான் வந்தியத்தேவன்.";
Zoe
  • 27,060
  • 21
  • 118
  • 148
Gayu R
  • 41
  • 6

3 Answers3

2

First you have to add the parameter id to your TextView, for example id1(the name doesn't matter).
Then in your java class you have to do this:

TextView text = findViewById(R.id.id1);
// USE THE SPLIT METHOD
String[] strs = str.split("\n");

You have to put this lines in the event where you change the text.

text.setText(strs[0]);
text.setText(strs[1]);
text.setText(strs[2]);

strs[0] is அவனுடைய கையும் இயல்பாக உறைவாளிடம் சென்றது.\nஇந்த சாலை தஞ்சாவூருக்குத்தான் போகிறது.

strs[1] is ஆனால் இதில் முக்கியமானவர்கள் மட்டுமே போகலாம் மற்றவர்களுக்கு வேறு சாலை இருக்கிறது என்றான் வீரன்.

strs[2] is அப்படியா ஆனால் நானும் ரொம்ப ரொம்ப முக்கியமான மனுஷன் தான் என்றான் வந்தியத்தேவன்.

You also can use an int attribute variable and use it in your event, something like this:

// Your class attributes
int i = 0;
//Your event method....
text.setText(strs[i]);
i++;

For the 'timer' question you can use a handler, inside your event method something like this:

Handler handler = new Handler(); 
handler.postDelayed(new Runnable() {
   public void run() { 

        if(i<strs.length()){
            text.setText(strs[i]);
            i++;
        }
    } 
}, 5000); // This number is the time in miliseconds

This will change your text every 5 seconds.

icortazar
  • 110
  • 1
  • 15
  • I am having two different `textview's` one is to display the above `string`. Other one is to split and display the `string` one by one on `onTouch` event. – Gayu R Apr 24 '19 at 08:52
  • This works fine using for loop. The loop executes fine but displays the last `string` after overriding the other. I want some thing like timer to display the 1st `string` then 2nd and so on – Gayu R Apr 24 '19 at 09:54
  • I edited my answer with and example of the 'timer' – icortazar Apr 24 '19 at 10:06
  • If it doens't work maybe you need a loop inside the method run – icortazar Apr 24 '19 at 10:10
  • Thanks I will check with your code and let you know the output. – Gayu R Apr 24 '19 at 10:18
0

Ok first of all you will need to split that string into small strings . You will need an array of strings .

String str = "அவனுடைய கையும் இயல்பாக உறைவாளிடம் சென்றது.\nஇந்த சாலை தஞ்சாவூருக்குத்தான் போகிறது.\n" +
            "ஆனால் இதில் முக்கியமானவர்கள் மட்டுமே போகலாம் மற்றவர்களுக்கு வேறு சாலை இருக்கிறது என்றான் வீரன்.\n" +
            "அப்படியா ஆனால் நானும் ரொம்ப ரொம்ப முக்கியமான மனுஷன் தான் என்றான் வந்தியத்தேவன்.";

String[] splitStr = str.split("\\s+");

And than you can do whatever you like with that and catch whatever string you want like :

splitStr[0] is அவனுடைய

splitStr[1] is கையும்

and so on , so forth untill your last one.

After that load it in your TextView , perhaps with a for loop or I don't know when you are going to show it .

text.setText(splitStr[1]);
coroutineDispatcher
  • 7,718
  • 6
  • 30
  • 58
  • I want to override the 1st `sting` with another **"அவனுடைய கையும் இயல்பாக உறைவாளிடம் சென்றது."** This should get displayed 1st and **இந்த சாலை தஞ்சாவூருக்குத்தான் போகிறது.** This should get overridden in the 1st text. – Gayu R Apr 24 '19 at 09:16
  • ok than you should do it this way `String[] splitStr = str.split("\\r?\\n");` – coroutineDispatcher Apr 24 '19 at 09:19
-1
 private int count = 0;

        List<String> list = new ArrayList<>();
        list.add(firstString);
        list.add(secondString); //so on
        tvName.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(count < list.size()-1){
                    count = ++count;
                }
                tvName.setText(list.get(count));
                return false;
            }
        });

try this.and make sure you add your string into the "list"

shadow
  • 114
  • 11
  • why do you need a list there ? and how do you know how many strings he has ? – coroutineDispatcher Apr 24 '19 at 09:06
  • @StavroXhardha cause i think when he + the String like that,there is no way he can get his part of string one by one. – shadow Apr 24 '19 at 09:09
  • there is no condition to split his string.so how can he split it into string1,string2.thats what i think – shadow Apr 24 '19 at 09:10
  • first of all check my own answer here , and second of all refer to this link https://stackoverflow.com/questions/7899525/how-to-split-a-string-by-space/7899558 – coroutineDispatcher Apr 24 '19 at 09:12
  • @StavroXhardha rofl :) just because i though he want to set text as the string அவனுடைய கையும் இயல்பாக உறைவாளிடம் சென்றது.\nஇந்த சாலை தஞ்சாவூருக்குத்தான் போகிறது.\n :) sr for dont understand the question :) – shadow Apr 24 '19 at 09:21