Assign a view ID programmatically in Android - android

I'm trying to create a RelativeLayout with several children programmatically. To make the rules like RelativeLayout.RIGHT_OF work, the child views must have proper IDs.
However, when I try to assign an ID, Android Studio flags it as an error:
view.setId(123);
ERROR: Expected resource of type id

Found it:
view.setId(View.generateViewId());

You have two options:
This is not a compiler error. It is just editor validation error as this is not a common way to deal with Ids. So compile and run with no problems
Use pre-defined list of Ids with type "id" as this accepted answer https://stackoverflow.com/a/8937477/1657333 so the editor will be happy.

In Android Studio click on lightbulb on line with this 'error'. And select 'Disable inspection' in first submenu.

As others have said, you shouldn't be using Integers directly in this case. There are some very rare cases where you might need to do this or in general suppress validation warnings. If so then you need to look into Lint.
In Android Studio you can click on the red light bulb at the left side of the line of code. This will show a context menu with a list of methods of suppression.

Related

How to find a specific warning in the whole project

Is there a way to search a specific warning in the whole project?
e.g. I have this warning in one of my LinearLayouts:
Set android:baselineAligned="false" on this element for better
performance
and I want to check if there are any similar warnings in other LinearLayouts in my XMLs. I know about Analyze -> Inspect Code but it doesn't help me in this situation and in this kind of problem.
How can I find them?
There is no filters but you can select custom scope in your case is xml folder as
file[app]:src/main/res/layout/*
Or you can Run Inspection by Name ... and this should be -> Missing baselineAligned attribute

Annoying default formatting

I am working on a simple android application in eclipse IDE and I got a little yellow icon on the left hand side of a line of xml code that looks like a light bulb with an exclamation mark beside it. When I hovered over, it says "[I18N] Hardcoded string "input..., should use #string resource input". The running and debug was successful but I just want to get rid of it as I find it annoying. What should I do?
If it's annoying, there is a reason. You totally should use #string resources instead of your hardcoded strings. All you have to do is to put your string in res/values/strings.xml and reference it in your layout via #string/my_string_id_here.
This is extremely useful for multi language support, or for plurals strings.
You can learn more here.
Hope this will help you.
The right way:
Move all your strings into resource files, as suggested, and reference them in your views like so: #string/mystringname
The "other" way:
Turn off Lint warnings in Eclipse in Window/Preferences/Android/Lint Error Checking
Both ways will remove that annoying triangle :)
This warning is there because hardcoding strings into the android app's Java source code is not recommended. It will compile fine - but Android Lint will complain about it, so that's why it's a "warning" and not an "error". Generally, it is preferable to define them in the separate "string.xml" file.
If you want to know why, check this answer.
For an example, check this answer.
You should also take a look at the official documentation for string resources.

Hide warnings in layout files

I updated Eclipse and SDK and now in every xml file the TextViews have the warning "Consider making the text value selectable by specifying android:textIsSelectable="true"".
Is it possible to hide the warnings, I hate to have my project with a lot of yellow exclamation marks.
Go to the propeties of your project. Go to Android Lint Preferences. Search for SelectableText and change the severity to ignore.
You can also do this in Eclipse preferences for all projects.
There is a good reason for this warning. By just disabling it, you are basically ignoring user interface guidelines of Android and you are not using the full potential of the user interface of your app. Therefore looking at each text view and explicitly setting this attribute to either false or true will increase the quality of your app.

How to rename widget ID's in Eclipse?

I am working on an app built upon an example from a tutorial. Now the different widget IDs no longer reflect their purpose so I would like to rename them. However, this seems quite a task as the IDs are used in multiple files.
Is it possible somehow to rename the IDs so the changes are migrated into the other files in the project? That is pretty much similar to refactor source code names, but for widget IDs.
I don't think a tool like that exists in Eclipse. The easiest way to do it manually is to rename an item in the XML layout and then track down the errors in the Java classes. If you do it one-by-one then you should have it cleaned up in a minute or two.
You can try to use the Find/Replace function is Eclipse. I have found this useful several times when changing ID's or something to that effect. Let us know what you end up doing.
In eclipse:
Go to the xml layout -> Graphical Layout -> Properties then click the ... button near the desired field:
In case anyone stumbles across this problem now, you can rename the ID from the visual layout editor and it will do all the hard work automatically.

How to get class/method/attribute info in Android XML file?

When I am in a Java class file, I can get context info of a certain method/attribute/etc. (in IntelliJ, this shortcut is Ctrl+Q), which is basically a short help file describing what that element does. Look at the image 1.
But when I am in an XML file, I cannot get any contextual info on any element. Look at the image 2.
How should I enable it? Do I have to download some additional Android doc (javadoc?) file?
For Android code support in Eclipse, you might want to checkout http://android-developers.blogspot.com/2011/06/new-editing-features-in-eclipse-plug-in.html.
In particular :
XML editing has been improved with new quick fixes, code completion in more file types and many “go to declaration” enhancements.
basically a short help file describing what that element does
FYI: It's called Javadoc.
For the xml:
Go to Window, Show View, Other, General, Properties.
Then, when you have opened an Android xml, you can switch to the Graphical Layout. Clicking on an element will show you it's properties in the property view. Hovering over the elements there at least will give you contextual information.
As CrazyCoder suggested, there is no way to get such contextual info because of the lack of sources to fetch such info from. Until better times...

Categories

Resources