1

I want to show TextView editable like the app "Google Keep" but

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

didn't work

3
  • Why doesn't it work? What happens? Commented Aug 25, 2013 at 14:34
  • On Eclipse the variable "value" is not used, and on debug the edittext doesn't make any string on my layout Commented Aug 25, 2013 at 14:37
  • 2
    Can you please describe more about the problem Commented Aug 25, 2013 at 14:44

3 Answers 3

5

Hello I've made a example that I think you can use:

Layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:textColor="#FF0000" android:text="@string/hello_world" /> <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/getInfoButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="GET INFO" android:layout_gravity="center_horizontal" /> </LinearLayout> 

MainActivity

package com.example.testedittext; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity { private TextView info; private EditText input; private Button getInfo; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); info = (TextView) findViewById(R.id.textView); input = (EditText)findViewById(R.id.editText); getInfo = (Button)findViewById(R.id.getInfoButton); getInfo.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String inputText = input.getText().toString(); info.setText(inputText); } }); } } 

Here is the output:

Edit Text Example

Hope this helps, Cheers

Sign up to request clarification or add additional context in comments.

Comments

3

That is the correct syntax for getting the text in string format of an edittext.

The value of String "value" is actually "" right now because you called

text.getText().toString(); 

IMMEDIATELY after EditText text was instantiated. As you can imagine, the moment it was created, there was no text inside it, so that's why "value" has an empty string.

If you want to retrieve the value of the edittext at a specific call, I'd recommend adding a button in your xml layout, and in your code, add this:

Button button = (Button) findViewById(R.id.somebutton); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { text.getText().toString(); } 

});

This will get the current String value of the edittext when you click on the button.

Comments

0

You have to use the value String somewhere else it will tell you that its not used. Then you have to use that piece of code at a point of time where you have had a change to set the value of that ediettext.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.