Find all hardcoded dimens in code using Android Studio - android

How to find all hardcoded dimensions both in xml layout files and in code in Android Studio? I want to move them over to dimens.xml.
Basically, I want something like https://stackoverflow.com/a/39130143, but for dimensions instead of strings.

This should work -
Press Ctrl + Shift + F or Cmd + Shift + F (in mac) to search in project.
Go to Options and choose Regular Expression.
Now search "((\d)+)dp" in search bar which should give you all the hardcoded dimensions.
If you have dimensions as decimal values you can change the regex pattern accordingly. This one worked in my case.
Also, if you want to search in specific area, you can do that too with this method. Thanks.

You can search all the hard coded dimension in Android studio using Find in path.
Go to
Edit > Find > Find in path
In option tab ->
General -> select Regular expression
Scope -> You can select the sope of the search
File name filter - > you can select FileMasks as *.xml
And you can start the search now, add \ddp or [0-9]dp in Text to find.
Done you can see all the usage.

I think you have to create the custom lint for that, since currently it can check for 0dp in code and use of pixel instead of dimension.

Related

Strings inside left drop down menu

I'm trying to modify strings inside an apk, I could find almost all strings at the strings.xml file, but there are still strings I can't find as shown in the image
enter image description here
I tried to use the Ctrl + Shift + F to search in the entire proyect but no results, I even tried to search for harcoded strings but same result, are they stored at somewhere else? are they added with java code inside files? does anyone has any clue?
Clean the project and restart once check it.

How to replace all dp value such as 50dp with a #dimen/50_dp at once?

I have got lots of XML files in which margin and padding dimensions are hardcode I have to replace all of them in format #denem/margin_20 ....etc
I have made dimens and saved all the values I need. But I have lots of XML files it will take a lot of time for me to go and change one by one file manually
SO I want to ask is there any trick to change all the values at once in Android Studio.
Thanks a lot for Helping.
Press Ctrl + Shift + R to replace and write "50dp" with inverted commas in find field . Then in replace field write #dimen/_50dp. Replace one occurrence and if it worked as expected and replace all other occurrences.
NOTE: if you want to change this in just res folder then you can select this folder only in find and replace dialog.
The approved answer is good but then you will have to do it one by one for each dimension so you can do the following:
Press Ctrl+Shift+R to replace all.
Select layout Directory by selecting "Directory" Option
Select Regrex ".*" from right side of find text input
in find first write (?!")\d\d(?=dp")
in replace write #dimen/_$0
Then click replace all
do similar thing for (?!")\d(?=dp")
It will replace all 2 digits and 1 digit dp values to dimension. You will manually need to edit negative and floating dp values.

Rearrange style properties in Android Studio

Android Studio allows to easily order properties of the views in XML files (layout_width, style, id, etc.) with a simple shortcut CTRL + ALT + L
However I could not find a way to arrange those same properties within style declarations. Is that possible in any way?
Edit: Corresponding issue for starring if not possible

How to search inside current xml file in eclipse

I cannot figure out how to search for text inside an individual XML file from Eclipse. Basically if I am editing one of my xml layout files, and I hit CTL + H -> file search -> Selected resources radio button checked, I always get '0 results in empty scope' as my results. Google hasn`t given me any help on this.
My workaround is to choose 'Workspace' instead of 'Selected resources' and then navigate to the file I want in my search results which is time-consuming. Why doesn't eclipse recognize my current XML file as a selected resource like it does with java files? Has anyone else had this issue? I`m using Eclipse ADT 23.0.2.129578
Go to the Source tab of the layout file editor, then use Ctrl+F to open the Find/Replace dialog. You can also use Ctrl+K to traverse through matches of the last search, even when the Find/Replace dialog is closed.
You can use Ctrl + Shift + R for searching the XML file quickly in eclipse.

Eclipse Android shortcut for content assist in XML

While editing an XML layout file, I would like Eclipse to display content proposals.
E.g. when I type in "marg" and press Cmd+Space, it should offer me a dropdown list of
layout_marginLeft, layout_marginRight etc.
How can I set this keybinding?
(On Mac it is by default Ctrl+Space, but I want it to be Cmd+Space)
Command+Space usually brings up Spotlight. You'll have to turn this off first.
Go to System Preferences / Spotlight.
Disable or remap Command+Space.
Then in Eclipse:
Go to Eclipse / Preferences.
Select General / Keys.
Select Content Assist in the list.
Delete the ^Space in the binding textbox.
Type in Command+Space.
If you go to
Eclipse > Preferences > Keys
find the option 'Content Assist' and changing the binding to cmd + space.

Categories

Resources