setText on EditText and TextView not working - android

We have a bigger Android 2.1 project, and for some reason, only at two places calling setText() on an EditText or a TextView object just doesn't work. It does not crashes, does not throw an exception, only the TextView/EditText is not updating.
Here's the code for setting the text:
EditText etzip_a = (EditText)activity.findViewById(R.id.editTextZip_a);
etzip.setText(m_addressA.zip);
etzip.addTextChangedListener(new onInputChanged(120));
this code runs on the GUI thread
it parses the R.id.editTextZip_a object successfully from the layout
a totally similar code for other tens of EditText are working fine
Does anybody encountered a problem like this?

try using this code:
You have create a object etzip_a of the EditText. Now after that you are using different object etzip.
EditText etzip_a = (EditText)activity.findViewById(R.id.editTextZip_a);
etzip_a.setText(m_addressA.zip);
etzip_a.addTextChangedListener(new onInputChanged(120));

EditText etzip = (EditText)activity.findViewById(R.id.editTextZip_a);
replace this line of code

Related

How to edit the text of a textView using viewbinding?

I am new to kotlin and android and I just started getting into using viewbindings.
Im trying to make this locations app and when setting the date through a datepickerdialog, when i try to edit the text of a textview with a binding its giving me errors. Can someone help me?
private fun displaySelectedDate(timestamp : Long){
binding?.etDate?.text = format.format(timestamp)
Change it to
binding?.etDate?.setText(format.format(timestamp))
If you check the docs for EditText, you'll find a setText() method. It takes in a String and a TextView.BufferType. For example:
EditText editText = (EditText)findViewById(R.id.edit_text);
editText.setText("This sets the text.", TextView.BufferType.EDITABLE);
It also inherits TextView's setText(CharSequence) and setText(int) methods, so you can set it just like a regular TextView:
editText.setText("Hello world!");
editText.setText(R.string.hello_world);

how is an object created?

EditText text1;
text1 = (EditText) findViewById(R.id.editText1);
text1.getText().toString();
Hi im new to android programming an need a little help. :) I just want to clarify if text1 is an object? Because it can call a method. But if text1 is an object how come that there is no "new" keyword. Thanks in advance for any response. :)
It is not necessary that all variables do initialization with new keyword.
Like if you write
String s = "";
Your String has been initialized without new keyword.
Same like this EditText is provided initialization in findViewById(). Here findViewById returned Edittext instance.
I suggest you complete Java Tutorial before continue work on Android. Because Android is based on Java language.
EditText is derived from the Super class View. Here findViewById method is returning an an object of the View class. You are explicitly typecasting it to EditText and assigning it to text1. So new is not required. It is being managed in findViewById method. Alternatively you can do this as:
EditText text1;
text1 = new EditText(An instance of Context); //Create an object of Edittext class
Now do whatever with this object text1.
It's me 2 years after, Now I want to answer your question bud. I know you just started and had a dream of creating apps that will be used by others, and guess what? You already achieved that and you can now also create apps not just for android but for IOS since you're using flutter now, you also have your own account on google play store now. Listen Bud, on the first line you declare what type is the text1. on the second line that's where you declare the text1 as an object now, then on the third line you are then using that object's capabilities like getting text. You've learned so much in this journey, and still learning not just in programming but in life.

TextView setText Cannot resolve type

Im using Android Studio, trying set text to Text View but every time i do this
TextView test = (TextView) findViewById(R.id.testView);
test.setText("fds");
on Android Studio i see Cannot resolve symbol 'setText'.
Can someone tell me what im doing wrong?
setText() will accept String which you have passed, so there is no issue in that.
One possible reason for this might be you have not written your code inside onCreate() or onCreateView().
If it is Activity you need to use these lines after setContentView() in onCreate().
If it is Fragment you need to use these lines after inflating your view in onCreateView().
Second reason, you might be having one more test variable of different type like String or something else

Use EditText in a DialogFragment to change TextView in Main Activity?

So I'm working on my first real attempt at an Android app, just a simple scorekeeper for softball games. I've got it tallying scores, outs, etc, and right now it just displays "Home" and "Away." I'd like to give users the chance to enter in actual team names. To this effect, I added a custom AlertDialog that pops up with an EditText, so when you hit "OK" it'll update the Home/Away team name.
The problem is I've been Googling this for most of the week and I've not found a single way to actually do this. I've tried tagging the fragment's layout XML so I can find the EditText, but it always gives me a null reference and crashes the app. I added a TextWatcher that presumably watched the fragment's text, but once changed and hit "OK," nothing happened. Tried adding the TextWatch to the fragment, that crashed I think, it was about two hours ago and I'm exhausted.
Really, I think I need a way to have the fragment find the TextView with the team name and change it to the value of the EditText when I positive click, but I don't know how to do that. Or maybe I've seen it and don't understand it, I've only been doing this about two months. I'd post my code, but I deleted out all the stuff that didn't work because it was taking up most of the screen real estate. Any ideas?
EDIT: So I followed advice below on defining views, that found the value of the EditText presumably, but hitting "OK" just made it set the TextView to a blank value. I think this is because the EditText's contents went away as the dialog was closed. Either that or this is all wrong. View dialogname = getActivity().getLayoutInflater().inflate(R.layout.fragment_home, null);
EditText mEtName = (EditText) dialogname.findViewById(R.id.homeName);
View mainAct = getActivity().getLayoutInflater().inflate(R.layout.activity_softball, null);
TextView oTextView = (TextView) mainAct.findViewById(R.id.teamOne);
newName = mEtName.getText().toString();
oTextView.setText(newName)
I think you are doing wrong at time of defining id for EditText. You need to give it dialog reference.
rather than doing
Edittext edittext = (EditText) findViewById(R.id.editText);
do like
Edittext edittext = (EditText) dialog.findViewById(R.id.editText);
Been through this issue a while ago. I created my own class which extended DialogFragment. When I tried to initialize EditText which was in the dialog, I got null like
mEtName = (EditText)dialog.findViewById(R.id.editText);
So, I initialized it in this way and it worked:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
...
View dialogName = getActivity().getLayoutInflater().inflate(R.layout.dialog_name, null);
mEtName = (EditText) dialogName.findViewById(R.id.dialog_etxt_name);
}
Where R.layout.dialog_name is my custom dialog layout.

Got exception in create run time editText object

I attempt to create an EditText object at run time this way:
EditText et=new EditText(MyActivity.this);
And i have got below exception in Samsung galaxy tab in other tablet it works fine.
android.os.Handler.<init>(Handler.java:121)
android.sec.clipboard.ClipboardExManager$1.<init>(ClipboardExManager.java:90)
android.sec.clipboard.ClipboardExManager.<init>(ClipboardExManager.java:89)
android.app.ContextImpl$8.createService(ContextImpl.java:292)
android.app.ContextImpl$ServiceFetcher.getService(ContextImpl.java:199)
android.app.ContextImpl.getSystemService(ContextImpl.java:1158)
android.view.ContextThemeWrapper.getSystemService(ContextThemeWrapper.java:79)
android.app.Activity.getSystemService(Activity.java:3932)
android.widget.EditText.<init>(EditText.java:68)
android.widget.EditText.<init>(EditText.java:62)
android.widget.EditText.<init>(EditText.java:58)
com.example.myProject.MyActivity.insertTextBox(MyActivity.java:8249)
com.example.myProject.MyActivity$19.run(com.example.myProject.MyActivity.java:8057)
I have solve this issue just by adding one view from xml layout which visually gone and than add all view runtime in linearlayout and its works fine. I don't know what's the issue but it's solve.
You should really post more code for the problem to become apparent.
Based on the little information you provided my guess is you tried to create that EditText before the context is initialized.
Try to move that code in the onCreate() method if it's not already there.

Categories

Resources