Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6544415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:27:47+00:00 2026-05-25T11:27:47+00:00

I’m new to android and I have not done any Java programming for probably

  • 0

I’m new to android and I have not done any Java programming for probably near on 2 years.

I’m following the tutorials at the android developer resources.
More specifically http://developer.android.com/resources/tutorials/views/hello-formstuff.html
Using the EditText widget.

Now the code in the example runs fine (creating the listener in the onCreate method)

However, when playing around I like to define the listener elsewhere, simply because it separates the code out to make it easier to read and manage,

So here is my class with all other stuff taken out

public class HelloFormStuf extends Activity {
private EditText edittext;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(btnOnClickListener);
    edittext = (EditText)findViewById(R.id.edittext);
    edittext.setOnKeyListener(etOnKeyListener);

}
private OnClickListener btnOnClickListener = new OnClickListener() {
    public void onClick(View v) {
        Toast.makeText(HelloFormStuf.this, "Beep Bop", Toast.LENGTH_SHORT).show();
    }
};

private OnKeyListener etOnKeyListener = new OnKeyListener() {

    public boolean onKey(View v, int keyCode, KeyEvent event) {
        // if the event is a key-down event on the enter button
        if ((event.getAction() == KeyEvent.ACTION_DOWN) && 
                (keyCode == KeyEvent.KEYCODE_ENTER)) 
        {
            Toast.makeText(HelloFormStuf.this, edittext.getText() ,Toast.LENGTH_SHORT).show();
            return true;
        }
        return false;
    }
}; }

Now the original tutorial the edittext is final.

final EditText edittext = (EditText) findViewById(R.id.edittext);
edittext.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
    // If the event is a key-down event on the "enter" button
    if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
        (keyCode == KeyEvent.KEYCODE_ENTER)) {
      // Perform action on key press
      Toast.makeText(HelloFormStuff.this, edittext.getText(), Toast.LENGTH_SHORT).show();
      return true;
    }
    return false;
}});

My question is, is this safe to do it this way compared to the tutorial? Because the big difference is now really is that the EditText object is now no longer final and is declared outside the class, but initialized inside the onCreate

Is this safe? such as there will be no little quirks laying around the corner to cause bugs. Does making the EditText object not final really matter?

My button is final, its listener is declared elsewhere and android is happy, I guess because I’m not altering the button or getting any properties of the button. Where as if I keep EditText final, my listener code cannot resolve edittext (The EditText object).

Can I carry on doing this or is there another way (other then declaring it all in the onCreate method).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:27:47+00:00Added an answer on May 25, 2026 at 11:27 am

    They make the EditText final so that the reference could not change since you couldn’t reassign it to other value. By putting it as an instance variable, a reference to the EditText might change during onKey in

    Toast.makeText(HelloFormStuf.this, edittext.getText() ,Toast.LENGTH_SHORT).show();
    return true;
    

    Below is the code showing reference reassignment:

    
    // get the EditText from the XML
    this.editText = (EditText)this.findViewById(R.id.edittext);
    this.editText.setOnKeyListener(etOnKeyListener);
    
    // Create a new EditText
    EditText modifiedText = new EditText(this);
    modifiedText.setText("Foo Bar");
    
    // reassign the reference to modified text
    this.editText = modifiedText;
    

    Now when you press the key, you will see “Foo Bar” in your toast instead of what is in the EditText
    in the layout

    Optionally, you could get the right reference of the View that you are setting the listener to from the View v argument in onKey below

    public abstract boolean onKey (View v, int keyCode, KeyEvent event)
    

    Below is the modification of the listener code to get the text from the EditText where the OnKeyListener is listening to

    
    private OnKeyListener etOnKeyListener = new OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // if the event is a key-down event on the enter button
            if ((event.getAction() == KeyEvent.ACTION_DOWN) && 
                    (keyCode == KeyEvent.KEYCODE_ENTER)) 
            {
                // get the EditText that this listener is set on
                EditText editTextWithListener = (EditText)v;
    
                // now you will get the text from the EditText where you are listening to even if you
                // change the reference as above
                Toast.makeText(WebViewMain.this, editTextWithListener.getText() ,Toast.LENGTH_SHORT).show();
                return true;
            }
            return false;
        }
    }; 
    

    Note that despite the little risk of having the reference changes, putting your View as instance variable is not an uncommon practice. Many code that I’ve seen only assign the View once via onCreate like you did thereby reducing the risk of reference reassignment.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have thousands of HTML files to process using Groovy/Java and I need to
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.