Mikołaj Mularczyk Mikołaj Mularczyk - 1 year ago 126
Android Question

Android Retrieve data from EditText

I am new to android programming, so please be forgiving. This is my first app, its purpose is to find strings typed in the search widget by the user in txt placed on sd card. I almost finished it, but my last goal was to add a feature that would allow the user to decide how many answers he wants to get after searching. So I added an EditText field where i can type a number. And that is my problem: I can't retrieve data that I typed in EditText field, and I don't know why. This is my onCreate.

public void onCreate(Bundle savedInstanceState) {

EditText editText = (EditText) findViewById(R.id.enter_a_number); // I think this is the firs part of my problem...

Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
int number = Integer.valueOf(editText.getText().toString()); // ...and that is the second
do_my_search(query, number);

and do_my_search method:

public void do_my_search(String query, int number) {
//Find the directory for the SD Card using the API
File sdcard = Environment.getExternalStorageDirectory();

//Get the text file
File file = new File(sdcard,"fragment.txt");

try {

BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "windows-1250"),8192);
String line;
String[] text = new String[5];
int i = 0;
TextView textView1 = (TextView)findViewById(R.id.textView1);
textView1.setMovementMethod(new ScrollingMovementMethod());

while ((line = br.readLine()) != null) {
if(line.toLowerCase().contains(query.toLowerCase()) == true && i < number){
text[i] = i + 1 + "." + line + "\n";
for(i = 0; i < number ; i ++){
if(text[i] == null)
text[i] = "";
StringBuilder builder = new StringBuilder();
for (String value : text) {
String final_string = builder.toString();
Spannable wordtoSpan = new SpannableString(final_string);
for(i = 0;; ){
int k = final_string.indexOf(query,i);
if (k == -1)
wordtoSpan.setSpan(new ForegroundColorSpan(Color.RED), k, k + query.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
i = k + query.length();

catch (IOException e) {
//You'll need to add proper error handling here

Everything but the text field works fine. I have browsed some similiar threads and I think my problem is that the variable number gets the value before I type anything in the field, it always picks the default text, even if it is showing something different. I know that probably there will be some errors in my code, but right now I'm interested in solving this particular issue: what should I do to make my variable number pick the value that i type in my edittext field? And is it possible without adding buttons or TextWatcher?

Answer Source

You need to add a listener to your EditText, in order to retrieve its value once changed.

Your "first problem" should not be an actual problem (retrieving the EditText by id as a child View of the main Layout), as long as the EditText is declared in your main Layout xml, and has that id assigned.

The simplest way to solve your issue is to declare a String variable as instance property of your main Activity. Let's call it theText and assign it an empty String.

Note that you don't really need an instance variable, you might want to use the value of the text change directly - it really boils down to whether you need to re-use that after processing it with your search method or not. In the example below, I assume you have an instance String variable theText declared and assigned in your Activity.

Add the listener to your EditText as such:

editText.addTextChangedListener(new TextWatcher() {

    public void onTextChanged(CharSequence s, int start, int before, int count) {

    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {

    public void afterTextChanged(Editable s) {
        // we assign "theText" a value here
        theText = s.toString();
        // we test it here (assuming your main Activity is called "MainActivity"
        Toast.makeText(MainActivity.this, theText, Toast.LENGTH_LONG).show();
        // you can do whatever with the value here directly (like call "do_search"), 
        // or launch a background Thread to do it from here

Now once the text has been changed by the user, the value of theText will be updated to that text and you can use it elsewhere in your code.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download