Android Studio autocomplete wrap object in another object - android

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

Related

How to create a new composable function in Android Studio?

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.

Combine both on text change and debounce in rxjava for edittext

I've been using rxjava2 for a while but mostly kind of a boilerplate, I didn't clearly know its features.
I supposed to create a search Edittext which call method after sometime. I got it working by the following code:
RxTextView.textChanges(edittextSearch)
.filter(charSequence -> charSequence.length() > 3)
.debounce(800, TimeUnit.MILLISECONDS)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(string -> {
search(string.toString());
}, error -> {
Log.e("ERROR","ERROR LISTENING: " + error.getMessage());
});
However, I need one more feature for this EditText, which is as soon as I type, even with just one character, I would display an "X" icon to clear the input, or hide it if user manually delete all text.
I don't know if the textChanges method above could do that or I have to add another text watcher for the edittextSearch
Thank you for your time.
You can achieve what you want by using a TextInputLayout and adding app:endIconMode="clear_text". It's part of the material design library, there are plenty of guides on how to use them.
This will automatically add a clear icon when the user begins typing, clicking the icon will clear the text and update your textChanges observable.

Android - Select all items using SelectionTracker

I'm using SelectionTracker form the support-library-v28
It works great, just as expected.
Only thing I need, is to enable Select All feature (using the ToolBar)
Looking at the API, I see that there is one way to select all, but that one requires creating Iterable<Long> with all the values, meaning, create an array which hold Long values from 1 to datasource.size()
Is there any simpler way to select all the items in my datasource?
Seeing as I've recently had to set up the same sort of functionality, I thought I'd share my approach. Using the setItemsSelected(Iterable<K> keys, boolean selected) method really isn't as complex as it seems.
Yes, you will need to pass in an iterable. What I did was, loop through my data and store the index of each item as a 'long' inside of an arrayList().
EX:
yourData.forEachWithIndex { i, item ->
//Be sure to start at one, just plus one
someOtherTempArray.add(i.toLong() + 1)
}
Then I created a method that makes it easy to 'trigger' the select all functionality:
EX:
private fun startHandler(isChecked: Boolean){
val handler = android.os.Handler(Looper.getMainLooper())
val runnable = Runnable {
kotlin.run {
mTracker!!.setItemsSelected(someOtherTempArray.asIterable(),
isChecked)
}
}
handler.post(runnable)
}
The 'isChecked' parameter, which will essentially determine if we want to select all or deselect all.
Note that I am simply using my arrayList of 'keys' (for me this is just the index of the item. However, this may differ depending on how you have your ItemDetails Builder set up) and calling the Kotlin .asIterable() function to turn it into an iterable.
I have also placed the call to setItemsSelected() inside of a handler to force the selection to take place on the UI thread.
Now whenever you want to select/deselect all, you can call the startHandler(true)!

google sheet custom menu function not visible in google sheet on android

I created in google sheets a custom menu linked to a custom function that pops up a dialog window with a youtube video in it. All this i did on my PC in a browser.
I now installed google sheets on my android phone, and shared the sheet myself (using a second gmail account). I now notice that the custom menu doesn't appear in google sheets app on android.
I am wondering, does custom menus and dialogs work in google sheets on android. Am i doing something incorrect in relation to permissions -- i.e. are there any permissions i need to assign to other users for them to see and use custom menu items and related functions. If yes, how can i make the correct settings.
thank you,
Dan
i used a drop-down list/combo - populated using data validation. this is supported on mobile device. then i use the onEdit(e) event to trigger the code... the first step is to identify what's been edited (which cell) and then act accordingly... an example follows where the drop-down would contain two items "Do Task 1" and "Do Task 2"... the event is triggered when any cell on the sheet is changed... the process then identifies if the cell change is that of the drop-down (cell value set to a global variable 'FunctionsCell') if it is the drop-down that triggered the event, it then gets the value (see the 'getValue()' part of the code) and then inspects it's value and then executes the code accordingly (see '// do something part')
var FunctionsCell = "B2" // global
function onEdit(e) {
var editCell = e.range.getA1Notation()
switch (editCell) {
case FunctionsCell:
{
var functionType = SpreadsheetApp.getActiveSheet().getRange(FunctionsCell).getValue()
switch(functionType) {
case "Do Task 1": {
// do something
break
}
case "Do Task 2": {
// do something
break
}
}
}
}
}

UIAutomator click listview based on index

I'm trying to implement an UIAutomator testcase with a general method to perform a click on a ListView item (regardless of the type of viewgroup holding the listitem).
Currently I have following code, but it keeps on clicking the first item.
public void clickListViewItem(int index) throws UiObjectNotFoundException {
UiObject listview = new UiObject(new UiSelector().className("android.widget.ListView"));
if(index <= listview.getChildCount()){
listview.getChild(new UiSelector().index(index)).click();
}else{
throw new UIObjectNotFoundException("Index is greater than listSize");
}
}
I got it to work with following code, it is based on the clickable attribute of an UISelector:
listview.getChild(new UiSelector().clickable(true).index(index)).click();
The developer page implements a similar scenario, found here - although this assumes there is some identifying feature that exists in the child by which to select (like in example below, a string "Apps"):
If more than one matching element is found, the first matching element in the layout hierarchy is returned as the target UiObject. When constructing a UiSelector, you can chain together multiple properties to refine your search. If no matching UI element is found, a UiAutomatorObjectNotFoundException is thrown.
You can use the childSelector() method to nest multiple UiSelector instances. For example, the following code example shows how your test might specify a search to find the first ListView in the currently displayed UI, then search within that ListView to find a UI element with the text property Apps.
val appItem: UiObject = device.findObject(
UiSelector().className("android.widget.ListView")
.instance(0)
.childSelector(
UiSelector().text("Apps")
)
)

Categories

Resources