Skip to content
Advertisement

Get string line by line android programmatically

I have this code but the problem is that this continually showing toast of each string line when I pressed my desired button.

Like this:

Clicked the button once:

Toast shows continually:

A

B

C

What I want is to get string from a single line once I click the button.

Like this:

EditText value: A

B

C

Result first click:

A only

Result second click:

B only

Result third click:

C only

Like that

Btw this is the code I use:

Button checkButton = (Button) findViewById(R.id.check_Button);
        checkButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    
                    EditText editBox = (EditText) findViewById(R.id.edit_Text);
                    String items = editBox.getText().toString();
                    Scanner scanner = new Scanner(items);
                    while (scanner.hasNextLine()) {
                        String line = scanner.nextLine();
                        Toast.makeText(getApplicationContext(), line, Toast.LENGTH_SHORT).show();
                    }
                    scanner.close();
                    
                }
            }
        );

I know it’s hard to understand but please bare with me.

Advertisement

Answer

I think you can go for an approach like this.

    int clicks = 0;
    Button checkButton = (Button) findViewById(R.id.check_Button);
    checkButton.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
         EditText editBox = (EditText) findViewById(R.id.edit_Text);
         String items = editBox.getText().toString();
         if (i == 0) {  
            Toast.makeText(getApplicationContext(), items , Toast.LENGTH_LONG).show();  
            i++;  
            editBox.setText("");
         } else if (i == 1) {  
           Toast.makeText(getApplicationContext(), items , Toast.LENGTH_LONG).show();  
           i++; 
           editBox.setText("");
         } else if (i==2) {
           Toast.makeText(getApplicationContext(), items , Toast.LENGTH_LONG).show();  
           i = 0;
         }  
  } 
    
}

Just enter a value in the editText when the value is reset after a single button click.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement