Question

I am new to Android Development and Java. I have problems stashing the value of an EditText into a variable and printing it out in the Console afterwards. I have read about getText(), but i dont quite understand how it works. Here is my script:

<RelativeLayout 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:background="@drawable/simplenote_mainpage_background_200"
android:paddingBottom="0px"
android:paddingLeft="0px"
android:paddingRight="0px"
android:paddingTop="0px"
tools:context=".MainActivity" >

<RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</RelativeLayout>

<Button
    android:id="@+id/button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="41dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="2dp"
    android:layout_marginRight="1dp"
    android:text="Save"
    android:textColor="#FFFFFF"
    android:textSize="20sp"
    android:typeface="sans" />

<EditText
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/relativeLayout1"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="10dp"
    android:textStyle="bold"
    android:hint="Title.."
    android:ems="10" />

<EditText
    android:id="@+id/note"
    android:layout_width="285dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/title"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginTop="15dp"
    android:hint="Note.."
    android:ems="10" >

    <requestFocus />
</EditText>



</RelativeLayout>

.

    public class notescreen extends Activity {

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

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



   // TODO Auto-generated method stub
}

}

I have tried some things you suggested, by i am getting a lot of problems: I would like to upload a screenshot, but i dont have 10 reputations. Here is the edited code:

package com.example.simplenote;

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;

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

    Button b1 = (Button)findViewById(R.id.button1);
    TextView tv = (TextView)findViewById(R.id.tv);
    EditText title = (EditText)findViewById(R.id.title);

     b1.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {

             String value = title.getText().toString();

            tv =settext(value);

                }
            });
     final String TAG = "MyClass";
             Log.d(TAG, value);
    //EditText text = (EditText)findViewById(R.id.title);
    //String value = text.getText().toString();



   // TODO Auto-generated method stub
}


  }
Was it helpful?

Solution

Try this:

//Add your packagename here please

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class notescreen extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        // Here is where you call the super on this method.
        // Mostly always comes as first line of any onCreate.
        super.onCreate(savedInstanceState);

        // After you have your layout set, you can retrieve your references
        setContentView(R.layout.notescreen);

        // You have just recovered your view class
        final EditText text = (EditText) findViewById(R.id.title);

        // You have just recovered the text of that view class (text)
        // final String value = text.getText().toString();

        // The log works as a printer on the console
        // Log.d("notescreen", "This is the text: " + value);

        // But hey... there is no text right "onCreate", right?
        // Soo...

        // Retrieve a button (you will need to create it on XML first)
        final Button clickForTextBt = (Button) findViewById(R.id.clickfortextbt);

        // Make that button print your message
        clickForTextBt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.d("notescreen", "This is the text: " + text.getText());
            }
        });
    }
}

OTHER TIPS

From the beginning, you do not which is the variable which is storing the value.

EditText text = (EditText)findViewById(R.id.title);

The line above, that is storing a reference to your EditText. This allows you to call functions on that object relevant to the EditText, i.e. getText() and setText(). There are other functions you can call as well but those two are the main two you are going to want to use.

getText retrieves the value from the text box, e.g. if you typed into the text box "hello" then getText() would return the string "hello". getText() will always return a string, even if a number has been entered.

setText sets the contents shown in the text box. E.g. if you were to run s text.setText("hello"); when this line is executed it will show "hello" in the edit text.

String value = text.getText().toString();

The line above is where you are storing the actual value of the edit text. So, using the example above, you typed "hello" into the edit text, when the line above is called the variable value will now equal "hello".

To output this variable to the console, there is a class called Log. This class has various functions that outputs to the console (otherwise known as logcat in android).

The various types are

Log.d = debug type messages Log.e = messages to indicate error (coloured in red) Log.v = verbose messages (coloured green) Log.w = warning messages (coloured yellow)

I think there are a couple of others but I can't remember them.

To output your variable that has the value from what the text.getText() function returned you pass this variable into one of the above Log functions.

Each of the above log functions take two parameters, a tag parameter and a value parameter.

The tag parameter is just a name, so you can filter the messages to only show that name, it is quite common to create a static final string for the tag which is the class name, for example in the line below.

private static final string TAG = "MyClass"

Then to output the text variable you would use the following line

Log.d(TAG, value);

You could if you wanted to make the message clearer as to what that value represents, concat a string at the beginning so the could might become

Log.d(TAG, "The value of my edit text is: " + value);

Using the example above, if in your edit text you typed hello, the log cat would show one of the following, depending on what line you used above

MyClass       hello
MyClass       The value of my edit text is: hello

Hope this helps

public class notescreen extends Activity {


//Its a good practice to declare all the things that you want in activity
String value;
Button b1;
TextView tv;
EditText title;

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

// here you have to initialize by its id
b1 = (Button)findViewById(R.id.button1);
tv = (TextView)findViewById(R.id.tv);
title = (EditText)findViewById(R.id.title);

 b1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

         value = title.getText().toString();

        tv.settext(value);

            }
        });


  }

}

Make an TextView in your xml file

<TextView
android:id="@+id/tv"
android:layout_width="285dp"
android:layout_height="wrap_content"

 >
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top