Android Too many ID - android

I'm building an Android app and I begin to have too many Id resources.
I wondered if there would be a way to have for example
TextView text = (TextView) findViewById(R.id.activity_name.item)
Thank you

This is not possible to my knowledge as the R class is generated and should not be edited.
But I think you can do the next best thing and make your naming convention match that format with underscores.
for example for all of your id's on the main page, this could be the text on a list element. MainPage_List_Text
Here is another so page you might find useful. Are there conventions on how to name resources?

Related

Error while uploading image in drawable in android studio

I uploaded local photos in drawable files. The photos are approximately 3 mb in size. Its showing this error. However The problem panel shows analizing for 15 minutes till now and it is still showing it. What could be the possible reason for such errors.
Resource name must start with a small case letter or an underscore('_').
For more rules regarding resource naming convention, you can refer to the below-mentioned medium article.
https://medium.com/#AkhilDad/a-designers-guide-for-naming-android-assets-f790359d11e5
There are a few conventions used in resources:
For resources that exist as separate files, they must be
lower_case_underscore_separated. The appt tool makes sure that your
files are only lower-case, because using mixed case can cause issues
on case-insensitive filesystems.
For resources declared only in values/... (attributes, strings, etc)
the convention is generally mixedCase.
There is a convention used sometimes to tag names with a
"classification" to have simple namespaces. This is for example
where you see things like layout_width and layout_alignLeft. In a
layout file the attributes for both the View and the parent layout
management are mixed together, even though they are different
owners. The "layout_*" convention ensures that there are no
conflicts between these names and it is easy to understand which
entity the name impacts.
For more information here is the complete discussion.
Are there conventions on how to name resources?
Resource name must start with letter. You can't start image name with digits.
The reason you can't have a resource with a numeric name is because variable names cannot start with numbers.
The error is actually because the resource name cannot start with a number. Resource name should start with a letter or underscore(_).

Code Obfuscation in android

I found somewhere this type of coding. Which obfuscation technique is apply on it.
Let's say If someone use this type
(DatePicker) dialog.findViewById(2131428239);
then i know this is id format of decimal value and it store inside public.xml and it refrence is in id.xml file. but class I can't found the Original name of class of my upper screen shot.What is the name of this Technique.and How to perform it. Any Help be appreciated.
How to use this Technique and where are the original name is save.

Get resources by int number

I sometimes see this declarations in Android source code:
mContext.getString(2131361954);
Notification n = new Notification(2130837696, "123", System.currentTimeMillis());
// Example code - does not match together
I think the numbers are some resources from the project, right? Why sometimes people work with this numbers instead of using the R class? Is it faster or something else?
And how can I check which resource is assigned to that numbers? Is it possible to get number which is used if I only have the file or is this number random? Maybe with the file name or the MD5 hash of the file or something else?
There is no performance difference, as all members of the R class are static and final, and are directly swapped in during compile time. This is equivalent to any code that uses R.x.y, so the performance is the same.
I would strongly recommend against using the numbers directly in your project as they may change during the addition, removal and modification of resources.
You can check the resource to which that number corresponds by converting it to hex, opening up the R.java file and searching for that hex number and seeing what it is assigned to.
You can also use getResources().getResourceEntryName(int resid); and pass it the ID at runtime to retrieve the file name.

Storing strings in android XML resource file and referencing them

I'm wondering if I should use a resource XML file for to store all the possible recipes for me rather than using an ArrayList that is hardcoded, the problem is that I don't know how to call from a resource file using the method i have...
Here is a cut down version of what I want:
int recipeNumber = b.getInt("RECIPE"); //This is taken from another activity
final TextView rowTextView = new TextView(this); //Create a textview
rowTextView.setText(R.string.recipeNumber); //This is what i am struggling with
howToLinearLayout.addView(rowTextView); //Add textview to linearlayout
I don't know what to put in the part that references my resource file. I know I need:
rowTextView.setText(R. but I'm not sure what would come after that.
Im storing strings that would be for example:
<string name="1">One part Vodka, One part Coke</string>
<string name="2">One part Vodka, One part Lemonade</string>
This list will be quite long so id appreciate any other suggestions on storage, bare in mind I'm pretty new to this.
The recipeNumber int is what string will be called in to the textview.
Thanks for any help
I recommend storing the values outside your code, for example in XML or JSON (quicker and easier to work with). I would go with JSON. Really easy to load a list into an array, or other collection, and work with it anyway you want.
http://www.vogella.com/articles/AndroidJSON/article.html
You can then maintain your recipes, add new ones, maintain it etc without having to touch the code. Simply replace the XML or JSON (or whatever you choose) and rebuild your app.
Strings.xml is intended for pieces of text in your UI and code which you might want to localise and to avoid hard coding strings. It's not really intended for storing data.
To get string resources in code, use getResources().
rowTextView.setText(getResources().getString(R.string.recipeNumber));
To get an unknown resource identifier, but a known resource name, you can use the following method.
int identifier = getResources().getIdentifier("" + recipeNumber, "string", "com.your.package.name");
rowTextView.setText(getResources().getString(identifier));

putting hyperlinks in TextViews - but not knowing the address in advance

I need to localize an Android app in many languages. But the text also contains a local web link like www.theLink.com, www.LinkForOtherLanguage1.it, www.yetAnotherLinkForOtherLanguage2.fr,... you get the idea :)
I know this way to linkify...
Pattern pattern = Pattern.compile("www.theLink.com");
Linkify.addLinks(textView, pattern, "http://");
But here I need to know the addresses and put them in the code. Is there any way without changing the code for each language?
Many thanks
You could put the links in a string array and get them by position according to what language it is

Categories

Resources