Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextChanged Listener

Tags:

android

I am still new to Android development and what I want to do is create a listener that will contain two TextView objects which hold the area and perimeter. The width and height are EditText objects. After I have entered the width and height, the values for perimeter and area should be shown in real-time based on the calcArea and calcPerimeter methods. The code that I used for the listener is based off an example I found on here. My code:

    package com.jtryon.rectanglecalc;

    import android.support.v7.app.ActionBarActivity;
    import android.support.v7.app.ActionBar;
    import android.support.v4.app.Fragment;
    import android.text.Editable;
    import android.text.TextWatcher;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.os.Build;

    public class MainActivity extends ActionBarActivity {

        // fields in the class
        // variables that are global to this file   
        double width;
        double height;
        double area;
        double perimeter;

        // "handles" to the objects from the XML
        EditText widthEdit;
        EditText heightEdit;
        TextView areaText;
        TextView perimText;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            // set up handles
            widthEdit = (EditText)findViewById(R.id.width_edit);
            heightEdit = (EditText)findViewById(R.id.height_edit);
            areaText = (TextView)findViewById(R.id.area_value);
            perimText = (TextView)findViewById(R.id.perim_value);

            widthEdit.addTextChangedListener(
                    new TextWatcher() {

                        @Override
                        public void afterTextChanged(Editable arg0) {
                            // TODO Auto-generated method stub

                            // read the width out of widthEdit
                            String widthString = widthEdit.getText().toString();

                            // convert the String into a double
                            if (widthString.length() > 0) {
                                width = Double.parseDouble(widthString);
                            }

                            // read the height out of heightEdit
                            String heightString = heightEdit.getText().toString();

                            if (heightString.length() > 0) {
                                height = Double.parseDouble(heightString);
                            }


                            // calculate area
                            double area = calcArea();

                        // calculate perimeter
                        double perim = calcPerim();

                        // set the label for areaText
                        areaText.setText(Double.toString(area));

                        // set the label for perimText  
                        perimText.setText(Double.toString(perim));

                    }

                    @Override
                    public void beforeTextChanged(CharSequence s, int start,
                            int count, int after) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start,
                            int before, int count) {
                        // TODO Auto-generated method stub

                    }
                }
        );
    }

    double calcArea() 
    {
        return width * height;
    }

    double calcPerim()
    {
        return 2 * width * height;
    }
}

User interface

like image 488
JimT Avatar asked Mar 19 '23 19:03

JimT


1 Answers

Define this under your edittext in onCreate

  youredittext.addTextChangedListener(new TextWatcher()
    {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count)
        {

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int aft )                                                                          
        {

        }

        @Override
        public void afterTextChanged(Editable s)
        {

            //call your function here of calculation here 
             yourfunctioname();

        }
    });
like image 100
Tushar Narang Avatar answered Apr 02 '23 09:04

Tushar Narang