Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android getText from EditText field

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?

like image 690
Alesito Avatar asked Aug 26 '11 04:08

Alesito


People also ask

How do I get text from EditText?

Now, before we start we need to know some method of EditText which we use to get or fetch the text written in an EditText that is . getText() method and the other one is . setText() method that we use to set some pre-written text or string. EditText demo; demo=(EditText)findViewById(R.

How do I get the number to edit text?

Use android:inputType="number" to force it to be numeric. Convert the resulting string into an integer (e.g., Integer. parseInt(myEditText. getText().


2 Answers

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() 
like image 57
Niranj Patel Avatar answered Sep 28 '22 12:09

Niranj Patel


Try this.

EditText text = (EditText)findViewById(R.id.edittext1); String  str = text.getText().toString().trim(); 
like image 27
Randroid Avatar answered Sep 28 '22 12:09

Randroid