arjava
arjava

Reputation: 1899

Edittext can not be converted to string

I created a message using EditText for the criticism and suggestion that will be sent using intent email, but when I picked up the message there was no result.

I took a string of using Edittext

message = msg_feedback.getText().toString();

and if I use directly

message = msg_feedback.toString();

then it appears in the following log

android.support.v7.widget.AppCompatEditText{2bd670a VFED..CL. ......I. 0,0-0,0 #7f0f00ce app:id/msgFeedback}

following his complete code

Activity

@InjectView(R.id.msgFeedback)
EditText msg_feedback;

String message;

message = msg_feedback.getText().toString();

Layout

<EditText
    android:id="@+id/msgFeedback"
    android:layout_width="344dp"
    android:layout_height="190dp"
    android:ems="10"
    android:inputType="textMultiLine"
    android:singleLine="false"
    android:maxLines="9"
    android:lines="9"
    android:maxLength="397"
    android:gravity="top"
    android:fitsSystemWindows="true"
    android:breakStrategy="balanced"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginTop="8dp"
    app:layout_constraintTop_toBottomOf="@+id/nohpFeedback" />

Upvotes: 0

Views: 1698

Answers (4)

arjava
arjava

Reputation: 1899

this code is correct

message = msg_feedback.getText().toString();

sorry, everything turns out my carelessness, this is not about the actual conversion. but the string access problem, I call in the function keys I forgot to change its access.

Upvotes: 0

user4696837
user4696837

Reputation:

EditText is a class and msg_feedback is instance or object of EditText

getText() is a method of EditText that return CharSequence

toString() is a method of String class and toString() use to convert objcet to String

If you directly convert EditText object to String object it is return the Object reference and information about EditText

Carefully see it. It is your output..

android.support.v7.widget.AppCompatEditText{//Class Info
    2bd670a VFED..CL. ......I. 0,
    0-0,
    0 
    #7f0f00ce 
    app:id/msgFeedback //EditText ID
}

not the text inside EditText . So If you want text inside the EditText you must use

msg_feedback.getText().toString();

Do you understand? If any query feel free to ask.

Upvotes: 0

Pankaj Kumar
Pankaj Kumar

Reputation: 83018

and if I use directly

message = msg_feedback.toString();

Why you are using it? This is not a direct way, But this is incorrect way. This will not give you the text which EditText contains, it will call toString() of object of EditText.

So below way is correct way, which you are using already

message = msg_feedback.getText().toString();

Upvotes: 0

Unique Identifier
Unique Identifier

Reputation: 370

You can get the message from editext as string using this approach.

message = msg_feedback.getText().toString();

Now you can use message as string which will be the text from the edittext.

and here message = msg_feedback.toString(); You actually getting the name of your EditText as string.

Upvotes: 0

Related Questions