Stephan Rhodes
Stephan Rhodes

Reputation: 11

How to access EditText type in Android Studio?

How can I access an EditText type from Android Studio ? I have already done the UI Design before writing the code on the MainActivity file. Do I need an extra import file in order to access EditText Type?

All I have done was to type: EditText nameTxt. But I got an error message at "EditText"

Upvotes: 0

Views: 5145

Answers (3)

spectre10
spectre10

Reputation: 434

This is very simple. Firstly, make sure that you have given your EditText an id in the Layout (UI) file. This is the line you would add.

android:id="@+id/layout_item_id"

Next go to your Java file and add this line in the onCreate() method.

EditText myEditText = (EditText)findViewById(R.id.yourEditTextId)

Now if you want to get the text that is inside the EditText then you would add this line:

String string = myEditText.getText().toString(); 

Upvotes: 2

behnam
behnam

Reputation: 1

Type below Code for Java

EditText myEditText = (EditText)findViewById(R.id.yourEditTextId);

Upvotes: -1

Antrromet
Antrromet

Reputation: 15424

Most probably yes, you are missing the import in your java file. Try adding the import

import android.widget.EditText;

Upvotes: 0

Related Questions