find and replace in android studio - android

I am looking for an efficient method to find and replace in android studio. where I can find a string using regex and replace the selected string with its substring.
e.g.
constants.LOG_FILE_PATH -> SETTINGS['LOG_FILE_PATH']
constants.LOG_DIR -> SETTINGS['LOG_DIR']
I have following regex for find
constants.[A-Z_]*
which selects constants.LOG_FILE_PATH and constants.LOG_DIR
but how to modify the replace string so that the above strings can be replaced by SETTINGS['LOG_FILE_PATH'] and SETTINGS['LOG_DIR'] respectively. I have to find and replace many strings in my project so manual find and replace is little overhead.
Edit 1: If you know how to do in pycharm or intellij-idea it will work in android studio also, thats why i have added tags pycharm and intellij-idea

Perhaps look at Structural Replace. Use the Find Actions shortcut (CMD + Shift + A) and type structural and the option should appear.
I guess something like the following may work:
Search template
constants.$log$
Replacement template
SETTINGS["$log$"]
Disclaimer, this is untested.

Related

How to use LogcatFilter in Android Studio?

While using AndroidStudio I accidentally came across feature called LogcatFilter.
You can create a file with .lcf extension the following way:
Press command + shift + N. The following dialog will appear
2. Select LogcatFilter option. An empty file scratch.lcf will be created.
From the name of this feature I understand that somehow it can be used to filter out logs from Logcat, but I haven't found any documentation or articles on how to use it.
So, my question is how I can use LogcatFilter? Which format should I use to type my filter?

Fastest way to delete a piece of code and its connected code in project (in Android Studio)

I imported a project in Android Studio. It's a very complex code. There's a TabView. I want to replace 2 of the 3 tabs with some new code. What is the fastest way in Android Studio to delete their content and every associated class/method/line in the project?
Edit: For better understanding:
I have for example 3 methods: method1(), method2(), method3()
I simply want to delete everything in the project related to method2() and method3() so I can replace it with any content:
I want to delete all of the submethods, too: method2(), method2.1(), method2.2(), method2.3(), method2.1.1(), method2.1.2(), method3(), method 3.1(), etc.
How can I do this automatically in Android Studio?
Here are some tips that may help:
To find usages of a single method/variable across the entire project
Either hold CTRL + Left Mouse click on this method/variable; so you can see wherever they are used.
Or Right Mouse click on this method/variable >> Find Usages
To safely delete a method usage from the entire project:
Either Right Mouse click on this method/variable >> Refactor >> Safe Delete
Or Alt + Delete
Note: This doesn't include deleting global methods/variables that are wrapped in the method intended to be deleted .. I am not sure if Android Studio supports this or not.
To search for some text in your entire project which includes almost everything in your project including SDK stuff, like method/field names, namespace, Strings, XML attributes, and so on
Edit >> Find >> Find in path…
To search for text in your entire project which includes in file
names, field/method/class names
Shift + Shift
Hope this could help you.

Live Template not working in Kotlin

Hope you have worked with Live Templates which is given by Android by default.
Like:
Generate debug log statement: "logd"+TAB
Generate error log statement: "loge"+TAB
Generate info log statement: "logi"+TAB
Generate TAG declaration: "logt"+TAB
Generate parameter logging: "logm"+TAB
Generate method return log: "logr"+TAB
Which it is not available in KOTLIN?
Is it not available in Android Studio 3.0 Canary Version?
Here is the step-by-step guide:
Firstly, Copy and paste AndroidLog templates to Kotlin (Just select them and use CMD+C, CMD+V (or Ctrl+C, Ctrl+V)
Secondly, You have to adjust them manually:
logd (loge, logv and others)
Select the logd item and press "Edit variables"
Change expression to: kotlinMethodName()
Also, remove ; from the end of the template, as you don't need it in Kotlin.
Now your method name will be shown correctly
logt
This one is a bit trickier.
Solution 1: TAG = class name.
Template text :
private val TAG = "$className$"
Edit variables -> Expression:
groovyScript("_1.take(Math.min(23, _1.length()));", kotlinClassName())
Solution 2: TAG = file name (can be used inside Companion)
Template text :
private const val TAG = "$className$"
or:
companion object {
private const val TAG = "$className$"
}
Edit variables -> Expression:
groovyScript("_1.take(Math.min(23, _1.length()));", fileNameWithoutExtension())
Yet not added log template in Kotlin Live templates section in Android Studio.
Settings -> Editor -> Live Templates -> Kotlin for kotlin templates.
Settings -> Editor -> Live Templates -> AndroidLog for AndroidLog templates
.
So you can't get the same AndroidLog templates in Kotlin code.
So now Question is How to use same Log Functions using templates in Kotlin?
Ans: You can add same Log templates (AndroidLog Templates) in Kotlin Templates section in Android studio as below example.
Then It will be available in your Kotlin code!
I hope in this way you can get an advantage of Log functions templates in Kotlin.
You don't need to copy paste anything as mentioned in the other answers. Locate the "Applicable in *" text in the Live Templates section in Android Studio - clicking on "Change" button gives a list of all scopes. Choose Kotlin (or one of it's child nodes) and apply. Note that some statements from Java will not work in Kotlin (like the ones using static keyword) - It's better to create a new template for those, rather than editing the existing one.
Here is the repository which contains all the templates for Android-Kotlin.
All you need to do this is download the repository, copy the templates directory and paste in your Android Studio config folder.
The config folder for your Android Studio can be found at
Windows: C:\Users\%userName%\.AndroidStudio<version>\config\templates
Linux: ~AndroidStudio<version>/config/templates
macOS: ~/Library/Preferences/AndroidStudio<version>/templates
This is the combination of all good answers by #pRaNaY #Leo and #Jaguar with bonus of TAG-free and variable-ready usage (no need to type "${}")
Template text: android.util.Log.d("$className$", "$methodName$ $$$contents$")
Applicable in Kotlin: top-level, statement, class, expression
variable expressions can be found in dropdown menu
here you can see whole picture
For logt the above two answers works but additionally need to add "define" as "class"

Android Studio not recognizing Espresso/Hamcrest static method names without prefacing with the classname

I wasn't sure exactly what headline to give this question because I'm not sure what this is technically called. In Android Studio while typing out Espresso tests I noticed that it refuses to accept something like this:
onView(withId(R.id.someId)).perform(click());
and instead will only accept this:
Espresso.onView(ViewMatchers.withId(R.id.someId)).perform(ViewActions.click());
even though every example that I've seen online shows the first example as correct code. Why is Android Studio forcing me to preface every ViewMatcher/Espresso/ViewActions/etc. method with the classname even after the imports are included in my class?
To clarify - trying to use the first example shows "cannot resolve method" and using autocomplete on it (which I have to do several times before it will work) invariably autocompletes to the second example. In all the "regular" code for my project autocomplete works correctly and short method names are recognized. I've tried doing a "clean" and "invalidate cache and restart" but no change.
Example of Google doc that shows usage according to the shortened code:
https://developer.android.com/training/testing/ui-testing/espresso-testing.html
You can try to check out your Android Studio Preferences for imports. Just go to Preferences -> Search "imports"
Here are the settings that I use and I don't have that issue:
What fixed the issue for me: just removed red withId in one place. Then just re-entered "withId": after that all the other red withId were replaced with black text color withId and now it compiled successfull!

Find usages of SharedPreference in AndroidStudio

Is there a way to find my usages of certain shared preferences?
You can't 'Ctrl + Click' them since they aren't methodes or variables and can't select them or use the find usages function from androidstudio.
I can use the find usages on the getBoolean() method, however this gives me every single shared preference instead of just the one i am looking for.
You can find the key of SharedPreference by just Ctrl+Shift+F or click on Edit -> Find -> Find in path .
Select the desired module where you want to search and you are good to go .
There is no special support for this in Android Studio. You need to do a regular text search for getBoolean("preferenceName".
Search for
getSharedPreferences("pref_name"
in Edit -> Find -> Find in Path...
This will display occurences where you used that specific SharedPreference.

Categories

Resources