40

I have a problem with getting text from EditText field to insert it in Email composer with intent. I've declared EditText field in layout file (@+id/vnosEmaila):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/navodiloEmail"
    android:text="@string/navodiloEmail"
    android:textSize="15dip"/>
<EditText 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/vnosEmaila"
    android:layout_below="@id/navodiloEmail"/>
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/navodiloZadeva"
    android:text="@string/navodiloZadeva"
    android:layout_below="@id/vnosEmaila"
    android:textSize="15dip"/>
<EditText 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/vnosZadeve"
    android:layout_below="@id/navodiloZadeva"/>
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/navodiloBody"
    android:text="@string/navodiloBody"
    android:layout_below="@id/vnosZadeve"
    android:textSize="15dip"/>
<EditText 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/vnosBody"
    android:layout_below="@id/navodiloBody"/>
<Button 
    android:id="@+id/klicIntentEmail"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/sestaviEmail"
    android:onClick="sestaviEmail"
    android:layout_below="@id/vnosBody"/>
</RelativeLayout>

The button calls onClick method "sestaviEmail" and I have declared it:

public void sestaviEmail (View view){
    CharSequence test = getText(R.id.vnosEmaila);
    Toast toast = Toast.makeText(EmailGumb.this, test, Toast.LENGTH_LONG);
    toast.setGravity(Gravity.CENTER, 0, 0);
    toast.show();
    }

I'm just showing it with Toast because it's faster but everytime I try to get text from field I get "false". All other questions that I've found had code which declared Button in methods and not in layout, maybe this is a part of the problem?

Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
Alesito
  • 585
  • 1
  • 8
  • 15
  • possible duplicate of [Get Value of a Edit Text field](http://stackoverflow.com/questions/4531396/get-value-of-a-edit-text-field) – Mafro34 Dec 11 '13 at 10:47

6 Answers6

112

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()
shareef
  • 9,255
  • 13
  • 58
  • 89
Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
  • 2
    There's always a warning that `text.getTExt()` could return null. got any good short and concise solution to check this? (that also looks like clean readable code?) – David T. Mar 05 '14 at 00:54
  • @DavidT I don't like the situation either, but I don't think there's anything unreadable about declaring `Editable editable = text.getText();` and then assigning your value like `value = editable == null ? "" : editable.toString();` (or whatever default/logic you want). – async Jul 20 '14 at 12:57
11

Try this.

EditText text = (EditText)findViewById(R.id.edittext1);
String  str = text.getText().toString().trim();
Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
Randroid
  • 3,688
  • 5
  • 30
  • 55
5

Try this -

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

String text = myEditText.getText().toString();
Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
Abhinav Manchanda
  • 6,546
  • 3
  • 39
  • 46
1

Try out this will solve ur problem ....

EditText etxt = (EditText)findviewbyid(R.id.etxt);
String str_value = etxt.getText().toString();
Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
Richa
  • 3,165
  • 1
  • 22
  • 26
0

You can simply get the text in editText by applying below code:

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

then you can toast string text!

Happy coding!

Android Geek
  • 596
  • 8
  • 14
-1

Use this:

  setContentView(R.layout.yourlayout);

  //after setting yor layout do the following
  EditText email = (EdiText) findViewById(R.id.vnosEmaila);
  String val = email.getText().toString;   // Use the toString method to convert the return value to a String.

  //Your Toast with String val; 
  Toast toast = Toast.makeText(EmailGumb.this, val, Toast.LENGTH_LONG);
  toast.setGravity(Gravity.CENTER, 0, 0);
  toast.show();

Thanks

Tim Fulmer
  • 14,970
  • 7
  • 28
  • 34
Shah
  • 4,990
  • 10
  • 48
  • 70