Android Gettext from Edittext Field

Get Value of a Edit Text field

By using getText():

Button   mButton;
EditText mEdit;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mButton = (Button)findViewById(R.id.button);
mEdit = (EditText)findViewById(R.id.edittext);

mButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
Log.v("EditText", mEdit.getText().toString());
}
});
}

Android getText from EditText field

Sample code for How to get text from EditText.

Android Java Syntax

EditText text = (EditText)findViewById(R.id.vnosEmaila);
String value = text.getText().toString();

Kotlin Syntax

val text = findViewById<View>(R.id.vnosEmaila) as EditText
val value = text.text.toString()

getText() from EditText: is it possible to get a 'null'?

I think the lint check is telling you something slightly differently:

String value = input.getText().toString();
if (value == null || value.length() == 0) {

Condition 'value == null' is always false.

It actually doesn't matter in this if-statement whether or not EditText.getText() can ever return null. Because if it returns null, then the .toString() will have caused a NullPointerException before the if-statement is even reached. Therefore, if the if-statement is at all reachable, value cannot be null.

You can dig all through the Android source code, and maybe you will find that there's no possible way for EditText to return null. If that is the case, then you can just ignore the lint warning if you know that the standard EditText is in your app.

However, in the more general case, nothing is stopping me from sticking this in the view hierarchy:

public class EvilEditText extends EditText {
// Constructors

@Override
public Editable getText() {
return null;
}
}

so the safest option is to rewrite your code to be completely nullsafe:

Editable e = input.getText();
String value = (e == null ? null : e.toString());
if (TextUtils.isEmpty(value)) { //hat tip to Deepak Goyal's answer. Could also use isEmpty(e)

Android getText from EditText return NULL

Please move this

final String Textusername = username.getText().toString();
final String Textpassword = password.getText().toString();

inside onClick().

EditText.getText().toString() returns hint. How to return empty String instead?

As this answer states, hint will only be returned with editText.getHint().toString().

If you are doing neither of the following then the only thing you can try is to rebuild/clean build the project:

  1. editText.setText() in your Java code, nor
  2. android:text="@string/title_hint" in your XML.

How to get text from EditText?

Well, it depends on your needs. Very often I keep my references to widgets in activity (as a class fields) - and set them in onCreate method. I think that is a good idea

Probably the reason for your nulls is that you are trying to call findViewById() before you set contentView() in your onCreate() method - please check that.

Can't get text from editText Android

Can't really tell what your error is without a logCat.

but one thing that might fix your problem is replacing

textData = (EditText) findViewById(R.id.editText);
text = textData.getText().toString();

with

textData = (EditText)getActivity().findViewById(R.id.editText);
text = textData.getText().toString();
// Toast for debugging purposes
Toast.makeText(getActivity(),text,0).show();

When using Fragments you should really read about getView() & getActivity().

Update

where in the xml have you set the text?

you need to set your text to something before you actually call the getText() command.

 <EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/buttonSave"
android:ems="10"
android:gravity="top"
----- add this line ------
android:text="whatever you want"
--------------------------
android:inputType="textMultiLine" >

Good Luck

editText get text kotlin

You're missing a cast of the View you get from findViewById to EditText:

var editTextHello = findViewById(R.id.editTextHello) as EditText

Then, you want to display the text property of the EditText in your toast:

Toast.makeText(this, editTextHello.text, Toast.LENGTH_SHORT).show()

For the record, this is just the more idiomatic Kotlin equivalent to calling getText() on your EditText, like you'd do it in Java:

Toast.makeText(this, editTextHello.getText(), Toast.LENGTH_SHORT).show()

how to get text from EditText within a Fragment and not activity

In the onCreateView method, do this:

name = (EditText) rootView.findViewById(R.id.editContactName);

Hope it will help fix your problem.



Related Topics



Leave a reply



Submit