How to create a new composable function in Android Studio? - android

Each time, I need to create a composable function, I do:
Right click on a package -> New -> Kotlin Class/File
And I end up with this:
class MyClass {}
Now I have to do three changes manually:
Add #Composable annotation
Change class with fun
Create the constructor
And this is really annoying. But this is the result:
#Composable
fun MyClass(
//Add arguments
) {
//Add logic
}
How can I do this operation quicker? Is there any shortcut in Android Studio? I couldn't find anything in the menu.

You can define you own template for this:
Open the Settings
Go to Editor > File and Code Templates
Click the + icon to add a new template
set the Name to My Composable template or whatever you like
set the Extension to kt
Copy and paste the code block from your question into the large empty text box on the right (directly under File name)
Click OK
Then, instead of New > Kotlin Class you can click New > My Composable template (or whatever you named it), and you start without the extra manual steps.

Related

How to refer to a button that is in the list Kotlin

I am trying to create an app in android studio. I've only recently started to get interested in this and ran into a problem. As planned, for each move in the game, the player presses several buttons, I would like to put the id of these buttons in a separate list, and when necessary, use this list to change the color of all those buttons that are in this list
What i did:
I have the list->
val move_list: MutableList<Any> = mutableListOf()
When the player presses the button, I add its id to move_list
fun for_btn_buba2(view: View){
move_list.add(buba2.id)
In activity_main.xml my button seems like:
<Button
android:id="#+id/buba2"
android:text="Buba 2"
...
android:onClick="for_btn_buba2"/>
And on click of another button i wanted to insert code like this:
move_list[0].setBackgroundColor(Color.parseColor(colorString:"#FFFC9D45"))
move_list[0] means id for button buba2
In python it can be, but it isnt python)
How can I change the color of the button through the list index with the buttons?
Firstly I suggest you indicate the type of what is in the list, which is Int so like
val move_list: MutableList<Int> = mutableListOf()
Then you can probably do this in the onClick of the other button
findViewById<View>(move_list[0]).setBackgroundColor(Color.parseColor("#FFFC9D45"))
or to do it to all
move_list.forEach {
findViewById<View>(it).setBackgroundColor(Color.parseColor("#FFFC9D45"))
}
I also notice you directly refer to buba2 in it the for_btn_buba2. I have the feeling that you are writing this function for every buba. This is unnecessary. You can get the id from the view parameter because that is in fact the same id. So do
fun for_btn_buba(view: View){
move_list.add(view.id)
}
then you can give each buba the same for_btn_buba as android:onClick
Alternatively you don't even work with ids at all and make it a
val move_list: MutableList<View> = mutableListOf()
and then do
fun for_btn_buba(view: View){
move_list.add(view)
}
and then you can actually change the background like you wrote it:
move_list[0].setBackgroundColor(Color.parseColor("#FFFC9D45"))
or
move_list.forEach {
it.setBackgroundColor(Color.parseColor("#FFFC9D45"))
}

How do I create a button in kotlin without xml?

How do I make a button without any XML? I tried XML but it did not work and it is "Old" I heard.
Yeah, using XML is old but it's the standard way of defining views in Android. Nowadays exist alternatives to that such as Jetpack Compose which takes a more React style when declaring the GUI where you write #Composable functions that produce a UI. Quite nice.
In any case you can create the views yourself programatically but it's much more tedious, less maintainable and imho 💩
With that said, from your activity you can create instances of any of the layouts that you would use in XML and then add more views into it:
class YourActivty: AppCompatActivity() {
override fun onCreate(...) {
val frameLayout = FrameLayout(this).apply {
// Configure the layout's properties
}
val button = AppCompatButton(this).apply {
// Configure all button's properties
}
frameLayout.addView(button)
// Indicate your activity to use the framelayout as its content
setContentView(frameLayout)
}
}

How can I have 2 different fragment, implementing same methods without writing the code twice?

I have a fragment screen where there is a form that is used to create Questions and Answers for my app. For this fragment, I use data binding, then I created many functions to validate the form, and check other stuffs.
Now, I'm creating a different fragment screen, where I'll be able to edit this Questions and Answers that were created, and for this, I want to use the same functions that were used when I created on the other fragment, for example to validate the fields that the user is editing.
I thought about implementing an Interface, and put these common functions there, so I could use it on both fragments. However, in these functions I use DataBinding, and I don't know how I can use it on the interface, so it would get the correct XML variables regarding to one fragment, or the other one.
On the screenshot bellow, it shows that I'm trying to use the binding, however I can't specify which one I'm using, otherwise the code will work only for a fragment, and not for both. Consequently, I tried to declare as DataBindingUtil but it didn't work.
Screenshot interface
If you want to go with this approach you could add the views as fields in the interface:
interface IQuestionForm {
var newQuestionTextInput: EditText
var answer1TextField: EditText
fun validateAllParametersToCreateNewQuestion(){
var allTextInputSet = true
if (newQuestionTextInput.text.isNullOrEmpty()){
newQuestionTextInput.error = "You have to enter the question"
allTextInputSet = false
}
if (answer1TextField.text.isNullOrEmpty()){
answer1TextField.error = "You have to enter an answer"
allTextInputSet = false
}
....
}
}
Then initialise those fields after creating the binding in the fragment.

Clear text before edit in Kotlin

I want to clear a field text before tap a text inside. I'm trying to do an Android Test using Kotlin.
The code:
suspend fun navegarMenu(){
clickSafe(R.id.nav_menu)
waitElementAndClick(R.id.tvProfile)
delay(4000)
// I WANT TO CLEAR THE TEXT INSIDE "et.NickName" FIELD BEFORE EDIT THE TEXT
fillEditText(R.id.etNickName, "digdindigdin")
delay(2000)
clickSafe(R.id.btnAlterNickName)
delay(10000)
}
Im this case, I need to clear the field before put "digdindigdin" inside the field. I'm using Espresso JUnit on Android Studio.
onView(withIdR.id.etNickName()).perform(clearText());

Android Studio autocomplete wrap object in another object

So is my existing code:
fun getAllPeople(): List<People> {
return peopleDao.getAll()
...
}
and I want to wrap the List<People> in a LiveData object.
When I start typing in the front, autocomplete gives me the suggestion for LiveData here,
but then when I hit enter it completes to this.
I know I can then type < and move to the end and type >.
But isn't there an easy way to wrap something with another object correctly?
You can do this for your current selection with a custom live template:
Go to Settings -> Editor -> Live Templates
Under Android, add a new template (Alt+Insert or the green 'plus' button on the right)
Give an abbreviation and a description to your template
Set the template text to LiveData<$SELECTION$>
Set the applicable contexts to Java and Kotlin
Click Apply
Now when you select your List<People> and use "Surround with Live Template" (Ctrl+Alt+J on Windows/Linux, Cmd+Alt+J on Mac by default), you can choose your custom template from the list and watch as the selected declaration becomes LiveData<List<People>>.
Tip: You can also use the "Extend Selection" shortcut to more easily select the declaration (Ctrl+W on Win/Linux, Alt+Up on Mac by default).

Categories

Resources