How do I set the value of an EditText

by Lovedumplingx » Thu, 19 Mar 2009 22:43:35 GMT


Sponsored Links
 Ok...so I've scoured and scoured and played and fiddled but I can't
figure it out.

I want to allow the user to set preferences for an application and I
want the preferences to be displayed in the EditText area if/when they
come back to change them again.

In my head I'm thinking I would be able to use setText() but no...that
takes a CharSequence and I have a string and don't know how to make a
CharSequence (which according to what I've read is supposed to be a
read-only thing anyway).

So...does anyone know how to put text into an EditText field without
relying on the XML?  I really want to do this via application
preferences.

Thanks.
--~--~---------~--~----~------------~-------~--~----~



How do I set the value of an EditText

by Will » Fri, 20 Mar 2009 06:35:41 GMT


 Cast the String to a CharSequence.

String x = "foo";
EditText ET;
ET.setText((CharSequence) x);




--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


How do I set the value of an EditText

by Isaac Waller » Fri, 20 Mar 2009 12:28:20 GMT


 A String _is_ a CharSequence. There is no need for a cast.





--~--~---------~--~----~------------~-------~--~----~



How do I set the value of an EditText

by Lovedumplingx » Sat, 21 Mar 2009 04:00:59 GMT


 That's what I thought too but I crash the task every time the activity
that contains this code is started:

EditText userText = (EditText) findViewById(R.id.userText);
userText.setText(userParam);

This is the basic way I thought to have the value set but it crashes
every time.





--~--~---------~--~----~------------~-------~--~----~



How do I set the value of an EditText

by Tseng » Sat, 21 Mar 2009 17:57:05 GMT


 An error message would be helpful.... Otherwise it's hard to help you,
other than "guessing" what could be the reasons for it.

Maybe you haven't set the ID correctly, using a wrong ID (which
doesn't even exist in the XML Layout file) or you're using
setContentView/setView after you have using "findViewById". This could
throw up a NullPointer Exception when you try to access (because
findViewById returns 0 if no layout has been set or the View with this
ID was not found).







--~--~---------~--~----~------------~-------~--~----~



How do I set the value of an EditText

by sm1 » Sun, 22 Mar 2009 00:28:11 GMT


 i use

  myEditText.setText("hello", TextView.BufferType.EDITABLE);

works fine
serge







--~--~---------~--~----~------------~-------~--~----~



How do I set the value of an EditText

by Lovedumplingx » Mon, 23 Mar 2009 22:55:06 GMT


 I get a NullPointer Exception when I use the Debug tool.

So you're saying that I haven't successfully read the View probably?
Ok...that's a great starting point.







--~--~---------~--~----~------------~-------~--~----~



How do I set the value of an EditText

by Lovedumplingx » Thu, 26 Mar 2009 04:22:21 GMT


 o I see the NullPointer Exception as a good starting point but I'm
still perplexed.

I created a new project just for testing. And I can't get this to
work:

package com.test;

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;

public class main extends Activity {
/** Called when the activity is first created. */
EditText justFillIn;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

justFillIn = (EditText) findViewById(R.id.ipText);
justFillIn.setText("charSequence is a string");

setContentView(R.layout.main);
}
}

All I want to do is set the value for that EditText and it's just not
working.

Any help?

On Mar 23, 10:55am, Lovedumplingx <lovedumpli...@gmail.com> wrote:
--~--~---------~--~----~------------~-------~--~----~



How do I set the value of an EditText

by Mark Murphy » Thu, 26 Mar 2009 04:34:27 GMT


 


You must call setContentView() first.

You aren't telling Android what layout to display until you call
setContentView(). Hence, until that time, findViewById() is guaranteed
not to work, because there are no views to find.

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com 
Android App Developer Books:  http://commonsware.com/books.html 

--~--~---------~--~----~------------~-------~--~----~



How do I set the value of an EditText

by Lovedumplingx » Thu, 26 Mar 2009 21:41:27 GMT


 Holy Mother Dogfood!!!

I am quite possibly an idiot (that's the second time I'm saying this
today)!  Wow.  So simple and yet I was looking at it all backward.

Thanks so much.  It makes sense now.





--~--~---------~--~----~------------~-------~--~----~



How do I set the value of an EditText

by Tseng » Sun, 29 Mar 2009 00:39:41 GMT


 Hehe,
yep, that's the most common error beginers make when start with
android and i already mentioned it in my post above ^^





--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. selecting text

Hi,
I want the user to be able to select text from the screen..much like
we do when we copy paste from one document to another. Is there an
easy way to do this? I need to know the exact string selected by the
user.

Thanks,
Sylvester
--~--~---------~--~----~------------~-------~--~----~

2. referring to res-id across packages

Hi:

  In the example
http://code.google.com/android/samples/ApiDemos/src/com/google/android/samples/view/LabelView.html
refers to
http://code.google.com/android/samples/ApiDemos/res/layout/custom_view_1.html

In java file, I see code to access new derived attributes using
"R.styleable.LabelView_textColor"

Can I access "android:background" in Java??
Given that the LabelView resides in a different package, what is the
programmatic way to access android:background in java code?  Is this
prohibited?

thanks
Jey

--~--~---------~--~----~------------~-------~--~----~

3. How to use a WindowAnimation?

4. Android Reviews

5. scalable vector graphics scalable vector graphics scalable vector graphics

6. scalable vector graphics scalable vector graphics scalable vector graphics

7. (D)GPS good to 10 cm