3

What is the meaning of (EditText) in the following snippet ?

EditText editText = (EditText) findViewById(R.id.edit_message);

I understand that it is the declaration of a new instance of class EditText...

But i'm puzzled with the (EditText) part ? What does it mean ?

1
  • it is a class cast. revise your Java concepts before going ahead with Android it will make things easier for you. Commented Nov 9, 2012 at 23:19

3 Answers 3

2

The findViewById method returns a View object and to make it an EditText object that it represents you have to cast it that way. You actually want to work with instance of EditText and not View so that you can have access to specific properties of EditText

EditText editText = findViewById(R.id.edit_message);

The above code won't be valid as EditText and View are not the same, so it need to be cast/converted.

Sign up to request clarification or add additional context in comments.

Comments

2

It is an explicit class cast: the generic View object returned by findViewById() is cast / converted to an EditText object.

(EditText) findViewById(R.id.edit_message);

Comments

2

EditText is an android class that represents an edit field on the Ui of your application. In the current case it looks as if you have defined an edittext field in the xml of your activity and you are now getting a reference to it so that you can interact with it programatically using the methods on the class.

You can see everything you can do with it: Android Developer: EditText

If you did not cast it to an EditText you would only be able to handle it as a View Android Developer: View

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.