Populate EditText widget text from another EditText widget text
I am trying to populate the text of a second EditText widget with the text from the first EditText widget only when the second EditText widget receives focus, the second widget is not empty, and the first widget is not empty.
I have the following OnFocusChangeListener handler code.
public void onFocusChange(View v, boolean hasFocus) {
EditText stxt = (EditText) findViewById(R.id.numberone);
EditText etxt = (EditText) findViewById(R.id.numbertwo);
if (hasFocus && stxt.getText().toString().trim() != "" && etxt.getText ().toString().trim() == "")
{
etxt.setText(stxt.getText().toString().trim());
}
}
When I run it and click into the second widget it does not populate. When I remove the third constraint ('etxt.getText ().toString().trim() == ""')) it works. so getText() on the second EditText widget is returning something even though the second widget has no initial value other then the text that is displayed via the hint attribute.
How can I accomplish this.
Thanks
Asked by: Carina880 | Posted: 20-01-2022
Answer 1
You should be doing an object comparison using String.equals()
, not just x == ""
.
I would rewrite this using Android's handy TextUtils
class:
String one = stxt.getText().toString();
String two = etxt.getText().toString();
if (hasFocus && TextUtils.getTrimmedLength(one) != 0
&& TextUtils.getTrimmedLength(two) == 0) {
etxt.setText(one);
}
Answered by: Vanessa684 | Posted: 21-02-2022
Answer 2
Never compare strings in Java with ==
or !=
. Use equals()
. Try that and see if you get better results.
Similar questions
java - Auto populate second EditText based on first EditText input
I'm brainstorming the next phase of my project, and I'm not certain which path to take. My idea is to have 2 EditText fields, one beneath the other. When the user types a unique ID into the first field, the second field will populate automatically with corresponding text. For example, entering "X5432" in box 1 will put "1957 Thunderbird" in box 2. I estimate having about 500 value pairs to work from, so I assume a SQLite s...
android - Populate editText from a listView
I have an editText, next to it is a button. When you click the button, I use onClick to trigger an intent to start a new activity, that brings up the listView.
When you click an item in the listView, I want the listView activity to close and populate the editText with the item. It seems like I'm going at this all wrong, any suggestions?
android - Populate EditText with value from database
In my Android application I have a TextView with an EditText. I want to prepopulate the EditText with the last value entered, which is stored in the SQLite database?
android - How to populate the EditText with the button click?
I want to get the values in the button to be populated in the edit text box. i.e I am creating a login page in which the pin has to entered I have given a set of numbers as buttons, when I click the button the corresponding values has to be populated in the edit text box( just like the ATM action).
I am now using:
b1.setOnClickListener(new View.OnClickListener() {
@Override
...
Android populate edittext value using bytes?
Is it possible to populate EditText value using bytes in android. I want to display a few number of images in an Edittext. Is there any way i can do this ?
java - Populate data in EditText boxes
Is there is any possible way to populate my EditText boxes with the data that is saved in SQLite database. I search, try to learn and understand from many websites that how to get data that is already inserted in SQLite database and I can retrieve it from any activity and populate in required EditText. Please help how to insert data in SQLite and how to read ...
java - Auto populate second EditText based on first EditText input
I'm brainstorming the next phase of my project, and I'm not certain which path to take. My idea is to have 2 EditText fields, one beneath the other. When the user types a unique ID into the first field, the second field will populate automatically with corresponding text. For example, entering "X5432" in box 1 will put "1957 Thunderbird" in box 2. I estimate having about 500 value pairs to work from, so I assume a SQLite s...
android - populate custom List View from editText
I'd like to populate the custom List View with data from EditText in run time. How could I achive it? .....................................................................................................................................................................
My code so far:
package com.example.additemlistrun;
import java.util.ArrayList;
import android.app.ListActivity;
public class MainListActivi...
How to populate values when I try to enter values in EditText field in Android?
I've following requirement and Since I'm new to this Android stuff, I'm not able to figure it out.
I've two Edit text fields for a driverName and a vehicleNumber. But these values shouldn't be new i.e When I try to enter driverName in particular EditText field, I should get all the driverNames populated so that I can choose one them. Same for vehicleNumber too.
For this I already have data of driv...
java - How do populate an EditText from one array list based on another array list
I started making a simple oil changing app but I've hit a wall with it. I'm not sure if it a fault in my logic or code. You add a car to the app with it's details these go into an array list and the car is displayed on a card view on the main screen showing the name, make, model, etc. When you click on a car from the main screen it opens another screen populated with the car and details with a list of existing oil changes...
arrays - Android - How to populate Content Provider only once on App install?
I have a database in my application that is used as a ContentProvider.
It holds settings values for the application, and when I install the application I
want it to add a hardcoded set of values just once.
This is how I am trying to do it at the minute.
if(settings.size()<= 0){
Settings s = new Settings("voipusernameTa", "xxxxxxxxx", "xxxxx",
"displayNameTa", "sip.networks.com", "s...
Android Dev.: How to populate gallery by image URL?
I'm trying to build a gallery where the images within will be pulled from the images URL from a website. i.e. Instead of doing R.drawable.xxxx.jpg, you use a url to represent the image.
Is this possible? If so, can you show me an example in code?
Thanks
Populate Android Database From CSV file?
Is it possible to take a csv file stored in the res/raw resource directory and use it to populate a table in the sqlite3 database?
My thought was that, if there was a way to do a bulk import for the entire file into the table then that would be cleaner and faster than iterating over each line in the file and executing individual insert statements...
I've found that there is a sqlite import command that allo...
How to populate a test database in Android?
I have a test class that extends ProviderTestCase2<>.
I would like to populate this test class database with data from some .db files.
Is there some particular method to push some .db file into the Mock Context of a ProviderTestCase2?
Otherwise which way is the easier to populate the database from the .db file?!
Thank you very much!!
android - possible to populate list view from cursor row id?
is it possible to populate a listview based on a row Id from a custom cursor adapter. I keep getting an error "illegalArguementException column: _id does not exist". but the database has it and is already being used correctly. I don't know what to do, because I would need to populate different listviews from the same database and i don't want to have to create multiple database which will still have the same column names. ...
layout - How create and populate three columns on Android
I need populate three columns in Android activity with dynamic data. How can I do it?
android - How can I populate a menu using intents?
How can I populate a menu using intents? I didn't understand that thing.
Or is there any better way for that?
Update:
Suppose I have an application that need to resize the image,and there are many other applications that have a capability of resizing the image. How can I show the list of app...
java - Android populate listview with cursor
My app uses a simple SQLite database. Everything works fine I can now present a Toast with a chosen row from the return query using:
{
//etc etc etc...
c.movetofirst();
DisplayTitle(c);
}
public void DisplayTitle(Cursor c)
{
Toast.makeText(main.this,
"id: " + c.getString(0) + "\n" +
"reg: " + c.getString(1) + "\n" +
"type: " + c...
android - how to populate a gridview for a calendar?
i have a 7x6 grid.here i have to populate the calendar for the selected month.
i have date,month and year.with the help of these value is it possible to populate my grid view with the help of any algorithm? same like this
android - Populate listview from database
hey guys i can currently fetch data from database into a table but im stuck with listview. my code is this to fetch all data:
public ArrayList<Object> getRowAsArray(long rowID)
{
// create an array list to store data from the database row.
// I would recommend creating a JavaBean compliant object
// to store this data instead. That way you can ensure
// data types are correct.
Ar...
Still can't find your answer? Check out these communities...
Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android