What is the best way to read the text from an EditText into code, and to write some text from code to the EditText?
Sorry I have ment not the TextView but the EditText
Hi all
I am a new to android I wish to write automatically from code to EditText and read in code from EditText
What is the best way to do it.
Java classes usually expose readable attributes with a get* method, and writable attributes with a set* method. In the case of a EditText these are:
getText
and
setText
see here and here (they are inherited from TextView)
Note: Scroll around a bit. You will see that they are defined multiple time. With different parameters. Pick the one you need.
A simple example. Let's assume you have a TextView with the id myTextField:
EditText myText = (EditText) this.findViewById(R.id.myTextField);
// Setting the text:
myText.setText( "Hello World!" );
// "Reading" the text (printing it to stdout):
System.out.println( myText.getText() );
Related
I would like to create an array of EditText for android but it seems that finding the id has been a very challenging task.Can someone reach out?
My code for the array:
EdiTText[] mEditText = new EditText[20];
for(int i =0;i<mEditText.length;i++){
mEditText[i] = (EditText) findViewById(i);
}
You cannot set id to view in runtime. Read more about android id here.
Just use constructor to initialize EditText:
mEditText[i] = new EditText(this);
There's two approaches to UI elements like EditText:
a. Implement them in your layout xml like this:
<EditText android:id="#+id/myedittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
Here the EditText would have the id "myedittext". You can then do stuff with that EditText by referencing it from your code with
EditText et = (EditText)findViewById(R.id.myedittext);
b. Programmatically create them like
EditText et = new EditText(context)
With this approach you will also have to manually add this EditText to your UI / layout.
You mixed those two approaches in a way that won't work.
If you don't know beforehand how many UI elements (in your case EditTexts) you need, it's cool to create them programmatically, but you should definitely read up on how to do that properly.
I'm not going to ask why you're trying to do this. However...
The resource IDs needed by findViewById will not start at zero as implied by your code snippet which uses the loop index for id.
If the EditText are created in XML, then you need the resource IDs of the form R.id.xxxx. You could create an int[] of the IDs and pass the corresponding id to findViewById.
If you create the EditText in code, then you won't have IDs, so will have to store the EditText objects at creation time.
I'm trying to create a dynamic text field that will take multiple fields of user input during a single activity, do a calculation in the java file, and then display the resulting value within the SAME activity in a text field.
Is there any way of doing this? I just figured out that I can't edit strings.xml dynamically, so are there any structures I can use that will allow me to constantly change the values?
Thanks all.
This will be pretty straight forward:
String yourTextValue = "text";
TextView myTextView = (TextView) findViewById(R.id.textView1);
myTextView.setText(yourTextValue);
Just call setText() with your String value (whatever it may be) each time you want to change the value of your TextView.
Hi
I am new to android,
How can i get text from a text field and store into variable and how can i place text in a text field.
Whati mean is, in C# or Flex or other languages we have functions or properties like if there is some text field txtName, and we do txtName.text or txtName.getText(), it returns text, and if we do like txtName.text ="abc", it assigns a value to it,
but i have not found any thing like that in android yet, please help me.
Regards
Atif
Use getText() and setText() method of TextView.
For example:
TextView tf=new TextView(this);
tf.setText("Hello");
String s=(String) tf.getText();
assuming in your XML layout your EditText has the id text you can get it by
final EditText text = (EditText) findViewById(R.id.text);
String textInEditText = text.getText().toString();
or you can set it by:
text.setText("Some text");
You can find more information about EditText and general Android Development (e.g. what is a layout?) in the normal guides.
You should first get ids for the EditTexts:
EditText et1=(EditText) findViewById(R.id.edit_text1); //replace edit_text1 with your id
EditText et2=(EditText) findViewById(R.id.edit_text2); //replace edit_text2 with your id
et2.setText(et1.getText());
I've searched high and low for something that seems to be a simple task. Forgive me, I am coming to Android from other programming languages and am new to this platform and Java.
What I want to do is create a dialog pop-up where a user enters text to search for and the code would take that text and search for it within all the text in an EditText control and if it's found, highlight it.
I've done this before, for example in VB and it went something similar to this pseudo code:
grab the text from the (EditText) assign it to a string
search the length of that string (character by character) for the substring, if it's found return the position (index) of the substring within the string.
if found, start the (EditText).setSelection highlight beginning on the returned position for the length of
Does this make sense?
I just want to search a EditText for and when found, scroll to it and it'll be highlighted. Maybe there's something in Android/Java equivalent to what I need here?
Any help / pointers would be greatly appreciated
grab the text from the (EditText) assign it to a string
Try the code sample below:
EditText inputUsername = (EditText) findViewById(R.id.manageAccountInputUsername);
inputUsername.getText().toString()
^^ Replace the IDs with the IDs you are using.
After this, you have the standard string methods available. You could also use Regex for a complex search query:
http://developer.android.com/reference/java/lang/String.html
http://developer.android.com/reference/java/util/regex/package-summary.html
Ok right , i asked how to create a random number from 1-100 for android and i came to this
TextView tv = new TextView(this);
int random = (int)Math.ceil(Math.random()*101);
tv.setText("Your Number Is..."+ random );
What this does is create the default kinda "hello world" style text view and says "Your Number Is.... [Then Random Number]
My problem is that i cant change the layout of this text , because it is not defined in XML, if someone could tell me how to change the style , or like make the random number into a string so i could use it for any Textview layout that would be great ..
Thanks :)
If by change the style you mean the text color, text size, and you want to change them programmatically, have a look at the setTextColor and setTextSize methods.
More info here
If you want more advanced formatting to set programmatically, see this link.
The below example demonstrates how to make your text bold and italic.
tv.setText("Your Number Is..."+ random, TextView.BufferType.SPANNABLE );
Spannable myText = (Spannable) tv.getText();
myText.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC),0,myText.length(),0);
Edit:
Try the below for the android:textSize="100dp" and android:gravity="center" :
tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 100);
tv.setGravity(Gravity.CENTER);
Putting it into a string is easy.
String randomAsAString = Integer.toString(random)
You can then use the XML properties of the TextView to change its formatting, such as android:textSize="30dp" or android:textColor="#900".
By the way, if you're happy with the answer to your previous question, you should go back and mark an answer as "Accepted". That gives 'reputation' points to the person whose answer you accepted and closes the question so that people don't think you're still waiting for a better answer. You can read more about reputation in the FAQ.
Edit:
You can't reference the string entirely in xml while still giving it a random number. This is because the "#string/some_string" format only allows unchangeable strings. The execption to this is using parameters, e.g. setting the string as
<string name="random_number">The random number is %d</string>
Then you could call up that string using something like
yourTextView.setText(this.getString(R.string.random_number, random))
As for your other question about setting a background to a textView, that's also easy.
yourTextView.setBackgroundDrawable(R.drawable.....)
You should take advantage of Eclipse's autocomplete feature... it makes finding these commands a lot easier. For example, simply type the name of your TextView followed by a period, pause half a second for the list of options to come up, then "setB" and it should then filter the list to the three setBackground Drawable/Resource/Color options.
tv.setText(Html.fromHtml("Your number is: <b>" + random + "</b>"));
For basic HTML text-styling tags.
You could also do something like this.
Define your string in strings.xml like:
<string name="your_number_is">Your number is <xliff:g id="number">%s</xliff:g>.</string>
Create a TextView in a layout xml:
<TextView android:id="#+id/your_number_is"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/your_number_is"
android:gravity="center"
android:textSize="100dip"
/>
Then your code would look like:
TextView tv = (TextView) findViewById(R.id.your_number_is);
int random = (int)Math.ceil(Math.random()*101);
tv.setText(getString(R.string.your_number_is, random));
This will make it a lot easier when you later on would like to change your text or maybe localize your app.
if you have thead troubles use this:
new Thread(){
public void run(){
TextView v = (TextView)findViewById(R.id.mytext);
v.setText("TEST");
}
}.start();