Printing all extracted emails in one text field and one activity in android app development

55 views Asked by At

i succeeded in writing an android program that extracts emails from inputted words but then the program doesnt print all the emails it found into one text field designated for it. Instead it prints the emails one after another on different screens but on the same designed activity... (For example; if the program found 8 emails, it would print them one after the other on different screens using the same activity, instead of printing all the 8 emails at once in a particular text field.)

Here is the code...

import android.app.*;
import android.os.*;
import android.view.*;
import android.content.*;
import android.widget.*;
import java.util.*;

public class MainActivity.
extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.main);
    }

    public void
    OnExtractButtonClick(View view) {

        EditText.mainEditText1 = (EditText) findViewById(R.id.mainEditText1);
        String txt = mainEditText1.getText().toString();
        String[] words = txt.split("\\s+");


        for (String word: words) {
            if (word.contains("@")) {
                Intent intent = new.Intent(this, SubActivity.class);
                intent.putExtra("word2", word);
                startActivity(intent);
            }

            if (!txt.contains("@")) {
                Toast.makeText(MainActivity.this, "No email. address found in the document!", Toast.LENGTH_LONG).show();
            }

            //How do i get all the emails found printed in one text field and on one activity.
        }
    }
}

forgive me, the code might not be arranged the way it should be due to the small android mobile that i am using to get my codes right.

Thanks y'all.

1

There are 1 answers

9
oschlueter On BEST ANSWER

we have to again work together on this. Please try this solution and let me know if it does what you need it to do. Otherwise please specify how it performs in your UI and what exactly needs to be different:

public void OnExtractButtonClick(View view) {

    EditText mainEditText1 = (EditText) findViewById(R.id.mainEditText1);
    String txt = mainEditText1.getText().toString();
    String[] words = txt.split("\\s+");

    List<String> resultList = new ArrayList<>();

    for (String word: words) {
        if (word.contains("@")) {
            // this is where you're creating a new "screen" per message
            // Intent intent = new.Intent(this, SubActivity.class);
            // intent.putExtra("word2", word);
            // startActivity(intent);

            // instead we store a word into a list if it's a match:
            resultList.add(word);
        }

        if (!txt.contains("@")) {
            Toast.makeText(MainActivity.this, "No email. address found in the document!", Toast.LENGTH_LONG).show();
        }

        //How do i get all the emails found printed in one text field and on one activity.

        // create a String that displays the emails in a way you like,
        // e.g. separated by newlines:
        String result = TextUtils.join("\n", resultList);
        mainEditText1.setText(result);
    }
}

This uses TextUtils which is part of Android, so it should be available to you.