How to edit the text of a textView using viewbinding? - android

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);

Related

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.

Xamarin: Setting text for a TextView programmatically

This is probably a mistake or lack of comprehension on my part, but I am quite confused right now. I'm trying to set a TextView in my Xamarin Android application programmatically. Here's my code:
TextView currentCharacterName =
FindViewById(Resource.Id.characterName);
currentCharacterName.SetText("test");
Unfortunately, this does not work, as I get the error "Argument 1: cannot convert from 'string' to 'int'". After reading in the available methods for SetText, I noticed the method I'm trying to call demands a ResId. I don't really understand why I would need a ResId to modify the text of a TextView.
I tried searching on Google for answers, and I came across this answer from 2014 that had the exact same problem as I do. The solution was to use the Text() method instead to set the TextView. Unfortunately, when I try this solution, I get the error "Non-invocable member 'TextView.Text' cannot be used like a method". When I try to check the Text method description, I see "string TextView {get/set} To be added."
Does this mean there's no implementation yet to set the text of a TextView? I am really reluctant to believe this, as it baffles me that such a big framework like Xamarin wouldn't even have get/set functions for something as simple as setting the text of TextView. I feel like there's a very simple solution for my problem, but I can't seem to find it.
TextView.SetText(X) allows you to set the text from a Resource id:
currentCharacterName.SetText(Resources.Id.MyString);
You are looking for the Text property:
currentCharacterName.Text = "test";
Xamarin: TextView class
Android.Widget.TextView.Text Property
Syntax:
public String Text { get; set; }
Test this code:
TextView currentCharacterName = FindViewById<TextView>(Resource.Id.characterName);
currentCharacterName.Text = "Your Text";

Editable textview on click

I have a screen with textviews now i want to make this editable on click of that
i tried one solution using edittext making it as transparent background but initially it will show cursor and the click is not recognizing properly,if i set focusbaleintouchmode to false in xml it is not getting focus.but some how the click is not working properly as expected.first is this correct approach?
expected result is textview should be there once user clicks on it it should be editable once user clicks outside it it should be not editable. any sample code will helps me a lot.sorry for my english
Thanks in advance
finally i got one solution using below code
in xml edit text i gave foucasbletouchmode to false which makes click works properly after that with in onclick
et.setFocusable(true);
et.setEnabled(true);
et.setFocusableInTouchMode(true);
et.requestFocus();
to lose focus
et.setFocusable(false);
et.setClickable(true);
et.clearFocus();
You can use the below code :
private makeEditable(boolean isEditable,EditText et){
if(isEditable){
et.setBackgroundDrawable("Give the textbox background here");//You can store it in some variable and use it over here while making non editable.
et.setFocusable(true);
et.setEnabled(true);
et.setClickable(true);
et.setFocusableInTouchMode(true);
et.setKeyListener("Set edit text key listener here"); //You can store it in some variable and use it over here while making non editable.
}else{
et.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
et.setFocusable(false);
et.setClickable(false);
et.setFocusableInTouchMode(false);
et.setEnabled(false);
et.setKeyListener(null);
}
}

setText on EditText and TextView not working

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

testPreconditions for EditText hint field

I am trying to test that my EditText hint field is not null in junit's
testPreconditions()
There are examples online for asserting that a TextView is not null, but using EditText in the following way
private EditText editTextField;
public void testPreconditions() {
assertNotNull(editTextField);
}
does not work (tests fail).
How do I access the android:hint field of EditText in the assert?
And while this question targets the hint field, answers that also work for similar situations (text of a button, title / elements of a spinner, etc) would be greatly appreciated.
Thanks!
To get hint from EditText use getHint function.
But it looks like your problem is different. Your test fails because your editTextField is null. You should read "Activity Testing" article. Shortly, JUnit is not enough, you need to setup a special Android Test project in order to access UI views of Activity under test.
If you want to check for not null of hint you can use the following code
EditText et = new EditText(this);
if((et.getHint().toString() != null) && (et.getHint().toString().trim().length()>0)){
}

Categories

Resources