Android Studio edittext is empty - android-studio

When i want to get my text
var edittext1: EditText = findViewById(R.id.edittext1);
var text = edittext1.text.toString()
Then the string is always empty

Please check if you have a synthetic import of your activity: e.g :
import kotlinx.android.synthetic...
If you do, it might seem like the activity is confused between the variable name and id of the field. If you have the synthetic import, you don't need the findViewById,
You can just say something like val edtText = edittext1.text.toString()

Related

Set spinnerMode programmatically

How can I change the spinnerMode when the button is pressed?
I mean mainly the method of implementing the change to the spinner and the rest I hope that I can do it :)
Let's say I have a spinner like below
val spinner= findViewById<Spinner>(R.id.Spinner)
I know you can apply something like this, but how do you implement it in the spinner shown above?
val setSpinnerMode = Spinner(this, null, android.R.style.Widget_Spinner, Spinner.MODE_DROPDOWN)
Or maybe someone has a better idea than the one shown above :)
in kotlin you can try this
val spinner = findViewById<View>(R.id.spinner) as Spinner
val adapter = ArrayAdapter(this#MainActivity,
R.layout.simple_spinner_dropdown_item, list)
adapter.setDropDownViewResource(R.layout.simple_spinner_dropdown_item)
spinner.adapter = adapter
spinner.performClick()

How to convert image name to string after randomly selecting from folder

I'm quite new to progamming. So here's my function to select a random image in my drawable folder.
fun generateimage(index:Int)
{var images=arrayOf(R.drawable.img1,....)
main_imageView.setImageResource(images[index])
This works as an image is shown randomly every time I start the application. But I would like to be able to know which image was selected. Preferably from retrieving the image name to string.
Possible duplicate of
How to get drawable name from imageView
However, a better implementation would be to have the value in a list of int and put your R.drawable items inside it. That was you can access which element is being shown.
You can do something like this:
fun main() {
val arrayImages = arrayOf("image1", "image2", "image3", "image4", "image5")
// in your case actual images.
val randomIndex = (0..arrayImages.size).random()
val randomImage = arrayImages[randomIndex]
println(randomImage) // in your case: main_imageView.setImageResource(randomImage)
}
Note that you need to assign the variable of randomImage so you can access it later on. I don't know what type "R.drawable.img1" is, but the object you set inside this array has to store the name of the file, so you can retrieve it that way.

How to add an email with timestamp in a variable? Or is possible? (Katalon)

I want to use a custom Keyword like example:
public class alalbala {
#Keyword
def email(){
String myEmailAddress = "test" + System.nanoTime() + "#itest.com";
return myEmailAddress;
}
}
And this myEmailAddress I want to check on another page like the example in a DB to see if this email is added. I am thinking to add in a variable and then using the variable to see if the email is present but I don't know-how. Can you help me, please!
Thank you in advance!
Let us say your alalbala class is inside of a package called myPackage.
You need to import that class to the script you need to use. And then you can use your method (function) inside your script.
For example:
import static myPackage.alalbala.email
def example = email()
println example
This will print something like "test722385734582300#itest.com", even though email method is declared in another file.

Clear a select Custom field based a selection from another field on update

I have the following fields:
System,
CMS,
Aquarius,
I would like to clear the Aquarius field based on the system selection. For example, a user. Initially a user created an issue and For system they selected: CMS and completed a value "abcde" on the CMS field.
Now the user would like to update the issue. Now they select Aquarius, then complete the field the Aquarius field with the value "jklm". When this happens, I would like to clear the values stored on the CMS field, so this field will not show on the View Screen.
Please note: These are single select fields.
Below is my code:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.MutableIssue
Issue issue = event.issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def customField2 = customFieldManager.getCustomFieldObjectByName("System")
LazyLoadedOption systemOption = (LazyLoadedOption) issue.getCustomFieldValue(customField2)
def changeHolder = new DefaultIssueChangeHolder();
def customField4 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Aquarius Category"}
if (systemOption?.getValue() == "CMS2") {
customField4.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField4), ""),changeHolder)
}
Unfortunately it's failing
Note: Using a custom listener - Scriptrunner
You could use
issue.setCustomFieldValue(cf, null)
issueManager.updateIssue(user, issue, eventDispatch.Event_Type, boolean)
Managed to get it to work. It was really a minor error. When setting the Value of a custom field. It's import to take not of the field type we are setting. For example when setting a Text Field we can use:
if (systemOption?.getValue() == "CMS2") {
customField4.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField4), ""),changeHolder)
}
But when setting a select (Dropdown) Field Type then we should use:
if (systemOption?.getValue() == "CMS2") {
customField4.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField4), null),changeHolder)
}
In summary null instead of ""

Is there a way to auto-fill arguments names?

When I instantiate classes (or call methods) with a large number of parameters I'm always using named arguments. But it's tiring to type each argument name every time:
data class User(val id: String,
val name: String,
val age: Int)
val user = User(id = "1", name = "John", age = 99)
Can IDEA pre-fill parameters like this?
val user = User(
id = ,
name = ,
age =
)
There's a great plugin for that: https://plugins.jetbrains.com/plugin/10942-kotlin-fill-class
It autofills the constructor with some default parameters so you can override the ones you want ;)
This is the way:
Right click on the constructor method
Show Context Actions
Add names to call arguments
Profit
Though this is not actually generating the whole call template with all the parameter names, it might be helpful anyway.
Kotlin IDEA plugin 1.1.1 suggests the parameter names in auto completion as you start typing them. For the User constructor from the example, start typing:
val u = User(i
^
There should be a suggestion id =:
It is inserted if you press Enter or Tab. Then you can continue with the other arguments:
val u = User(id = "123", n
^
Here, name = should appear in suggestions, and so on.
Also, the parameters info popup should help you with it:
See the following requests:
IDEABKL-6690 Automatic code completion when choosing a signature
IDEABKL-5496 Auto-filling the actual Java call arguments
There is an experimental feature you can enable by adding java.completion.argument.live.template=true into Help | Edit Custom Properties.
If you already added all the params values in the constructor, Android studio will help you to do that.
Just click on the Object, in your case on User, then click on option + enter (on mac) and you will have add names to call arguments.
you can use Live template:
setting > Editor > Live Templates
choice code group and add by Green Plus 1.live Template
now you need fill items
Abbreviation is name for call template code.
in template type your code like it:
val user = User(
id = $arg1$,
name = $arg2$,
age = $arg3$
)
$arg1$ means where you can type new and jump by Tab
in code when you type Abbreviation name of your code, can selected and Code Generate there
GoodLuck

Resources