var text:String = ""
var result = TextRecognition.getClient().process(image)
.addOnSuccessListener { visionText ->
text = visionText.text
Log.d("OUTPUT",visionText.text)
}
findViewById<TextView>(R.id.txt_Original).setText("ORIGNAL: "+text);
I'm giving the text recognition an image to read text from. I then want to assign that text to my variable "text". However, the text from the image isn't being assigned to my variable. I added a Log.d to see if anything is being read and the text from the photo is being read as it returns the correct text from the image.
I added the "ORIGNAL" to the setText just to make sure it isn't just skipping that line, and it is setting the text to "ORIGNAL: " but no text. But I know Its getting to that point
If I assign the TextView directly inside the addOnSucessListener, then it works. However, I need to do things to the text so I would like to keep it as a variable
After adding a Log after the function call, I found out that the log afterwards was running first and then the log inside the function ran. I'm assuming the function is acting asynchronous. Thus I moved all the code I wanted to run (basically all of it) into the addOnSuccessListener. Its messy but it runs now!
Related
Hello i want to know why is my program changing selectedDataEdited List when i only changing editTransactionList ?
var editTransactionList: MutableList<Transaction>? = mutableListOf()
var selectedDataEdited: List<Transaction>? = listOf()
editTransactionList = listTest as MutableList<Transaction>
selectedDataEdited = listTest
var position = 0
println("edit $editTransactionList")
println("select $selectedDataEdited")
editTransactionList.get(position).apply {
amount = 2000
name = "K"
}
println("edit $editTransactionList")
println("select $selectedDataEdited")
editTransactionList.get(position).apply {
amount = 3000
name = "Z"
}
println("edit $editTransactionList")
println("select $selectedDataEdited")
the output is
edit [Transaction(amount=1000, name=T, test=1)]
select [Transaction(amount=1000, name=T, test=1)]
edit [Transaction(amount=2000, name=K, test=1)]
select [Transaction(amount=2000, name=K, test=1)]
edit [Transaction(amount=3000, name=Z, test=1)]
select [Transaction(amount=3000, name=Z, test=1)]
Variables are basically references. When you store an object in a variable you actually say "when using this variable please refer to this object". So if you "store" the same object into 2 different variables, each of them still refers to that same object. Getting the object using the first variable, making changes to it, and then accessing the second variable, will still get you that changed object.
You will need to copy the list to prevent the unwanted behavior. Keep in mind though that you would probably need a deep copy. Simply calling toList() on it for example only makes a shallow copy, which means that even though it will be a different list, the objects inside it will still refer to the original.
It's hard to tell what would work without knowing what Transaction looks like. If Transaction is a data class then selectedDataEdited = listTest.map { it.copy() } might work. See this example https://pl.kotl.in/Q_o8pYXVs
KOTLIN Why when i changed a List it is also accidentally changing another list
Because you don't have "another" list. You only have one list.
When you do selectedDataEdited = listTest, you assign a second reference to the same list. If you want two separate lists, you must create them, possibly by cloning the original list.
Instead of using as MutableList use toMutableList:
editTransactionList = listTest.toMutableList()
It will make a copy of your list instead of passing a reference to the same list.
I'm creating an app where I need to quickly test some different languages. I've got 3 string resource files, values/strings.xml, values-es/strings.xml, and values-fr/strings.xml. Each of the files have their respective translated strings.
When I launch the app, I'm preloading some text into a TextView (Locale.getDefault().displayLanguage) and text that I pull in from an array in the xml file and assign one string to an EditText:
<string-array name="some_text">
<item>Overall, how severe were your flu symptoms today? Please select one response only.</item>
<item>No flu symptoms today</item>
<item>Mild</item>
<item>Moderate</item>
<item>Severe</item>
<item>Very Severe</item>
</string-array>
I set the text of the language label like this:
languageTxt = findViewById(R.id .language_text)
languageTxt.text = Locale.getDefault().displayLanguage
Then load the strings in the array and set one of them to the EditText like this:
textList = resources.getStringArray(R.array.some_text)
editTxt = findViewById(R.id.edit_text)
if(textList.isNotEmpty()) {
val txt = textList[currentIndex] //currentIndex = 0
editTxt.setText(txt)
}
textList, languageTxt, and editTxt are declared private like:
private lateinit var textList: Array<String>
private lateinit var languageTxt: TextView
private lateinit var editTxt: EditText
The first run through onCreate, everything works fine. If the language is set to Spanish, both languageTxt and editTxt are in Spanish as expected. Then if I go to settings and change the language to French, when I bring my app back to the foreground, the languageTxt says French (well spelled in French). Then I watch in the debugger, the textList loading the text, and it's all the French strings like I expected. The problem I'm seeing is when I set editText, the control isn't updating to display the French string. It's still in Spanish. I can clearly see that the text is in French, but calling setText() doesn't seem to be working. I've tried clearing the exitTxt first, setting it to null, empty, etc, nothing seems to work. Does anyone know what's going on here? I've been scratching my head for a couple hours. Ugh
Thanks to Giddy Naya's answer for giving me a hint on what is going on. Android saves the state of the app when it goes in the background so that it's the same when it comes back to the foreground. It's so strange, because even a editTxt.setText() doesn't work and gets overridden by Android. So what I had to do is set the new state of app (i.e. update the text) in the onResume function:
override fun onResume() {
super.onResume()
if(textList.isNotEmpty()) {
val txt = textList[currentIndex]
editTxt.setText(txt)
}
}
If you're new to Android, this feature can make debugging quite a pain, since you're trying to set a new state in onCreate and it doesn't change.
I'm still not sure why setting a TextView works in onCreate() but setting an EditText does not.
If your app is in foreground during language changes your UI may need reloading onResume so expected resources can be loaded.
You can save the default language in preference and check for value changes onActivityResume and if true then reload UI.
Something like
override fun onResume() {
super.onResume()
...
if (localeHasChanged)
setContentView(R.layout.xxx) //force UI reload
...
}
How can i take user input from popup dialogue and pass it into a BackGround Async Task?
I have a "createGarden" button. When i click it i want to retrieve a string from user, and pass it to my Background AsyncTask.
In my onClick, i have tried calling String myGardenName = getGardenName(), which returns input from dialogue. Then passing this into my Background task.
new HomeBackgroundTask().execute("create_garden", UserID_String,myGardenName );
I also tried using a value container, and passing this instead of "myGardenName":
String myGN = gnVC.getVal();
HomeBackgroundTask mhomebackgroundtask1 = new HomeBackgroundTask();
mhomebackgroundtask1.execute("create_garden", UserID_String, myGN);
instantiated my 'gardenValueContainer' value container as final in my "getGardenName()" method (enclosing class?) as well as instantiating it in my onCreate()
- I then try SETTING that value from within my onClick (inner class?)
--Also tried calling my HomeBackgroundTask directly from the onClick
Problem
Seems that my create garden always tries to insert a BLANK as the garden name. resulting in "garden "" already exists". When debugging, the user input get's passed through as a paramater, there was an issue with moving from "onPreExectute" to "doInBackground" but now when i'm debugging i get stuck in looper where i can't step over/into/out and my app just says freezes on connecting. (debug halts on a comment line, which might be bad?)
My php scripts work just fine with the same logic for registering a user.
No errors in my console!
http://pastebin.com/2AzWmcM5
Any help greatly appreciated!
"debug halts on a comment line, which might be bad"
Have you tried removing all breakpoints, or putting breakpoints on method implementations rather than on the, say, first line of the method?
In my app I have a screen where I display some text and then a photo. The text is variable in length (sometimes none at all, sometimes a lot), so I wanted to have it set up so the text never takes up more than a few lines (but can be scrolled) leaving enough room for the image below.
My view component for this part is created programatically, and I've adjusted the code to have the following (currently in my text-setting method, but the same thing happens if it's in the initial view-create code)
public void SetDescription(String description)
{
mTxtDescription.setText(Html.fromHtml(description));
mTxtDescription.setClickable(true);
mTxtDescription.setMaxLines(5);
mTxtDescription.setLines(5); //this makes no difference either!
mTxtDescription.setSingleLine(false);
mTxtDescription.setScrollbarFadingEnabled(true);
mTxtDescription.setScrollBarStyle(VERTICAL);
mTxtDescription.setMovementMethod(ScrollingMovementMethod.getInstance());
mTxtDescription.invalidate(); //adding this made no difference...
}
However it doesn't work- long text still fills the whole screen and the image has vanished due to being pushed down to a height of 0. How can I get the text to never be more than 5 lines?
Try removing the call to setSingleLine. And use setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE). It'd also put this call before the setMaxLines and setLines call to be sure.
Note: setLines overrides the settings of setMaxLines and setMinLines.
The TextView has many issues surrounding the various calls to how it should display multiple, ellipses, etc.
The setSingleLine(false) seemes to reset the setMaxLines command. Try to move the setSingleLine command before the setText. That worked for me.
The below code is working fine for me
txt = (TextView)findViewById(R.id.textview);
txt.setMaxLines(5);
txt.setMovementMethod(new ScrollingMovementMethod());
txt.setScrollContainer(true);
txt.setText("Example Text");
txt.setTextColor(Color.WHITE);
txt.setScrollbarFadingEnabled(true);
in xml inside textview
android:scrollbars="vertical"
I have no idea why this doesn't work. The TextView is defined from an tag in the view. The base TextView doesn't have text set and I want to set it in the View on display.
I have tried placing the below in onCreate and onStart but it doesn't seem to work. The last two lines are just for debugging. I can verify that the header does get the text. The thing is, the TextView doesn't actually get updated. Any ideas?
TextView header=(TextView) findViewById(R.id.acheader);
header.setText(R.string.accounts);
header.invalidate();
header=(TextView) findViewById(R.id.acheader);
String blah=(String) header.getText();
Try again removing the text in 4th line
header=(TextView) findViewById(R.id.acheader);
header.invalidate() is not needed.
Instead of String blah = (String) header.getText() try
String blah = heager.getText().toString();
And why are you verifying a "setText()" on text view using code? Why can't you check the
actual output?
The above code might not work the way you are trying to use it, because the redraw of text view is handled by the framework and generally it tries to group item updates (Dirty rectangles to be specific) and update them all at once. It may do it well after your function exits, Try to validate visually, thats the best way.