finding resources of view - android

I'm trying to add to my project the source code of SlidingDrawer, I could find it easily in grepCode.
The problem is that the code is not compiling, there's references to native resources, for Ex.
R.styleable.SlidingDrawer_orientation
that I'm cannot find, neither in GrepCode nor in Android repositories at GitHub.
does anyone have encountered in such a scenario and was managed to solved it?
Thanks.

So I just tried doing this, I had compile errors related to the resources as well. Those resources are not publically available to android.R. As the previous answerwer said, some resources are declared internal/hidden.
Once I imported my own frameworks_all.jar file I was able to see them and even build my own little SlidingDrawer class, with no more work than just copying the file into my own project.
This is an excellent guide to get you started on understanding the concepts of / getting used to using Android's hidden and internal classes:
Amazing Super Awesome Guide to Android's Internal / Hidden API which will totally work and make your life better
Note The attributes appear to be hidden, not internal, so you just need to worry about getting access to hidden components.
A bit of background if you are interested though, the resource file for android (android.R) gets compiled specially in frameworks/base. The attribute you want doesn't appear to exist on it's own, I.E. there is no xml file in frameworks base where R.styleable.SlidingDrawer_orientation exists. That's why you couldn't just find it. There IS however, an attribute file: frameworks/base/core/res/res/values/attrs.xml which you can find the orientation listed as an attribute for:
<!-- SlidingDrawer specific attributes. These attributes are used to configure
a SlidingDrawer from XML. -->
<declare-styleable name="SlidingDrawer">
<!-- Identifier for the child that represents the drawer's handle. -->
<attr name="handle" format="reference" />
<!-- Identifier for the child that represents the drawer's content. -->
<attr name="content" format="reference" />
<!-- Orientation of the SlidingDrawer. -->
<attr name="orientation" />
...
Seems like android marks all the styleable attributes hidden because the entire section is obscured from public code. I'm not sure of the specifics in how this R file gets constructed, I'd have to dig more into make files to find out. Either way, end fluff, to reiterate all you need to do is get access to the internal/hidden components.

The R file is auto generated based on the resource files you have under the folder /res
Read more about it: http://developer.android.com/guide/topics/resources/providing-resources.html
So in this case, you don't have a resource defined for SlidingDrawer_orientation...try to look for the corresponding resource under the /res folder from the project you are copying from. Some resources from the Android OS library are declared internal/private...therefore you can't really access them in your code, what you could do is replicate if you have the proper resources.

Related

Do attribute names in an Android library need to be prefixed?

I've read (and confirmed with testing) that resource value names (Strings, Drawables, dimensions, etc.) should have a prefix at the beginning (generally the library's name) to avoid name conflicts, because a project that uses the library and declares a resource with a matching name will overwrite the library's resource.
What I'm unclear on is whether the names of attr attributes inside a <declare-styleable> should also be prefixed. Since they are wrapped within the <declare-styleable>, are they protected from over-writing? Within Java code, their resource names are automatically prefixed with the name of the <declare-styleable>, but when used within XML files, they are not.
I'm guessing that their usage is context sensitive. That in my custom Preference's code, when I call context.obtainStyledAttributes() with a specific styleable, it is only at that point that the XML attributes are interpretted as specific types. If I declare a Preference styleable attribute named "min" of type float and use it in my project, it will not matter that there is an attribute named "min" in the v7.preference library's SeekBarPreference styleable of type int, because SeekBar doesn't use my styleable when calling obtainStyledAttributes().
So if my assumption is correct, there's no reason the compiler would be consolidating attributes by name like that. But custom attribute styling is a complex beast in Android, and I'm not sure if I'm missing something in my testing. It would be nice to omit the prefixes in my library's attribute names for ease of use/documentation.
If the <declare-styleable> is defining attributes for a Style, then I think it's still safe from conflicts, because Styles don't merge. They only reference each other from their own attributes. If I understand them correctly--they are somewhat convoluted to me.
On a related note, is there any reason view IDs should be prefixed? I'm thinking yes, because my library view exists within a project's view structure and a user of the library calls findViewById() on an ID that matches one of mine, I could envision scenarios where the search turns up my view first and trips them up. But Google's own appcompat libraries take no such precaution, so I'm unsure.
After studying documentation and AOSP source code, I've come to the conclusion that yes, you should prefix attribute names.
Resource attributes are not <declare-styleable> local. They are all global. When you declare a resource attribute (which can be done inside or outside of a <declare-styleable>), you name it and give it a format type:
<attr name="my_attribute" format="string" /> <!-- This is a declaration -->
When you reference a resource attribute (which is only done from within a <declare-styleable>), you list its name but not its format.
<attr name="my_attribute" /> <!-- This is a reference -->
Lint shows an error if you try to declare two attributes with the same name.
The fact that you can declare an attribute from within a <declare-styleable> is merely a convenience for code conciseness. It does not imply a limited scope. If you are planning to use the same attribute in multiple styleables, it's probably good practice to put its declaration outside of any <declare-styleable>, so it can be found easily.
One oddity I've noticed is that Lint doesn't show an error if two different libraries have overlapping attribute declarations. I suppose it doesn't cause an issue if they both have the same format type. I haven't checked to see what happens if they don't.
As for why Google doesn't prefix attribute names in AppCompat, I figure they want it to be very easy to use, and almost interchangeable with AOSP xml code, so they want the attribute names to match similar attribute names in AOSP. And so they deemed the simplicity of use outweighs the risk. What I suspect they overlook is how disjointed and lacking their documentation is on setting up and using styleables and attributes, so it's not obvious what to watch out for when setting up our own custom views and preferences.

Override layout xml from android framework

Problem
I want to override a layout file from android namespace, e.g. R.layout.popup_menu_item_layout (which is referenced from code as com.android.internal.R.layout.popup_menu_item_layout). By saying override, I assume declaring an xml file in the project which would be prioritized over the layout that framework owns.
Note, this is just an example layout, so the question concerns to each layout that's present in sdk/platforms/android-XX/data/res/layout directory.
What I've tried
tools:override
There's an undocumented tools:override tag available, which overrides specific resources. See this answer for an example, which overrides values from Design Support Library, not from Android framework.
Applying tools:override="true" to the root tag of the layout won't take effect.
XML layout references - refs.xml
As described in this post, declaring a refs.xml file in /values/ directory with following content:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="layout" name="activity_main">#layout/activity_second</item>
</resources>
will refer to activity_second.xml once activity_main.xml is used. There's an answer that suggests using this technique in order to substitute Snackbar's layout.
This also won't take effect.
Question
Is there any legitimate way to override/substitute a layout file from android package?
I know this is an old question but I also wanted to override a library layout with my own, here's how I did it.
The layout in question was called design_bottom_navigation_item
In refs.xml I added the following:
<resources xmlns:tools="http://schemas.android.com/tools">
<item name="design_bottom_navigation_item" type="layout" tools:override="true">#layout/bottom_navigation_item</item>
</resources>
There are 4 parts to this which I'll explain.
Name: This is the name of the layout you want to override
Type: The type of resource you are trying to override, in this case a layout.
tools:override: This is how you tell Android Studio to override the library layout with your own.
Value: This is where you specify what resource you want to use instead.
You can do this with any resource type this way.
What is that you're trying to do?
If the idea to only replace how the menu-item will look like, you can try the following:
Create a custom MyMenuAdapter extends MenuAdapter
Override the getView method to return the view from your adapter.
You are trying to customise your sdk on the application itself, at runtime.
That's just not how it works.
If you use an SDK on your project(on any technologies), and you need to modify some behavior, you will tweak this SDK and after that, compile your project with this news customized version.
Trying to modify it at runtime is not a good idea.
You will face multiple issues (retro compatibility, security trigger, TREBLE incompatibility , dependency issue, etc)
You have 4 possibilities to do what you want:
Make your own android rom where you will apply your modification
Copy the resources you need to modify on a fake xmlObject with the tag, after the onPostCreate of your application, you will be able to modify the when inflation. You can generalize this behavior and it will simulate an sdk overlay.
Make your own sdk :)
Multi-level reflection, but, no way you succeed with a stable version
Of course, none of this solutions is applicable for a public app.
don't know your issue have fixed or not but simple solution for this is create new layout that is same layout name of framework (in this case is popup_menu_item_layout). Then go to android google source to copy xml content popup_menu_item_layout
So you can custom anything u want. But remember don't change any id of views.

Android Layout - when to use app: vs android:?

I've been writing some Android apps but I don't really understand when to use app: and when to use android:. When styles are not being applied the way they're supposed to, I use trial and error and sometimes find that using app: instead of android: solves the issue but I don't understand why. It'd be great if someone could point me in the right direction. Thanks!
You can use the app namespace to have app compatibility with older API versions.
For example
app:srcCompat="#drawable/customborder" has the same effects with
android:background="#drawable/customborder"
The difference is that the first will work correctly with older API's and the second will not display what you would like.
You are talking about custom namespace.In android we can create custom views in additional to already available views.
As per in Google developer docs..
To add a built-in View to your user interface, you specify it in an XML element and control its appearance and behavior with element attributes. Well-written custom views can also be added and styled via XML. To enable this behavior in your custom view, you must:
Define custom attributes for your view in a resource element
Specify values for the attributes in your XML layout
Retrieve attribute values at runtime
Apply the retrieved attribute values to your view
Once you define the custom attributes, you can use them in layout XML files just like built-in attributes. The only difference is that your custom attributes belong to a different namespace. Instead of belonging to the http://schemas.android.com/apk/res/android namespace, they belong to http://schemas.android.com/apk/res/[your package name]
So for if you use default views you can use android namespace and if you want to set and use attributes for custom view you can define your own name.
Refer this
If you take a look at the beginning of the your layout xml files (in which you used app:) you will (probably) find lines like this:
<?xml version="1.0" encoding="utf-8"?>
<SOME_LAYOUT xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
in this case app: namespace will be used for custom attributes, specified by you inside attrs.xml file or by someone else in one of used libraries.
Sometime the property with android only available in new Android version like
In this case, you should use app:... to make it work with older version.
moreover you will find two variants
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/[packagename]"
the difference between xmlns lines is res-auto take care of resolving our package as sometime we will add .debug or .test in our package and we already provided the packageid of the app Ex:
xmlns:app="http://schemas.android.com/apk/com.test.io.debug"
xmlns:app="http://schemas.android.com/apk/com.test.io.test"

How does "?android:attr/activatedBackgroundIndicator" work?

I was looking for how to highlight a selected item in a list when displaying a contextual action bar for the selection, and the solution I found was to set the android:background attribute of my row layout xml to "?android:attr/activatedBackgroundIndicator".
How does setting this work though?
what is the mechanism involved?
what do the syntax elements like "?", "attr", "activatedBackgroundIndicator" mean?
where is the meaning of "activatedBackgroundIndicator" defined?
If you are in a forensic mood here is how to dig and find out what is going on.
android:background="?android:attr/activatedBackgroundIndicator"?
Intuitively this means set the background to some drawable.
But lets decompose this further to see how we get to our mysterious drawable.
To be precise it means "set the background attribute to what the attribute "activatedBackgroundIndicator" refers to in the current theme.
If you understand "refers to in the current theme" part, you have basically understood everything that is going on behind the covers.
Basically, activatedBackgroundIndicator is not an actual drawable but a reference to a drawable. So where is "activateBackgroundIndictor" attribute actually defined?
Its defined in your sdk directory in a file name attrs.xml. For example:
path_to_android_sdk/platforms/android-17/data/res/values/attrs.xml
If you open that file, you will the declaration as follows:
<attr name="activatedBackgroundIndicator" format="reference" />
attrs.xml is where you declare all the attributes that you are later going to use in your view xml. Note we are declaring the attribute and its type and not actually assigning a value here.
The actual value is assigned in themes.xml. This file is located at:
path_to_android_sdk/platforms/android-17/data/res/values/themes.xml
If you open that file, you will see the multiple definitions depending on what theme you are using. For example, here are the definitions for themes name Theme, Theme.Light, Theme.Holo, Theme.Holo.Light respectively:
<item name="activatedBackgroundIndicator">#android:drawable/activated_background</item>
<item name="activatedBackgroundIndicator">#android:drawable/activated_background_light</item>
<item name="activatedBackgroundIndicator">#android:drawable/activated_background_holo_dark</item>
<item name="activatedBackgroundIndicator">#android:drawable/activated_background_holo_light</item>
Now we have our mysterious drawables. If you pick the first one, it is defined in the drawable folder at:
path_to_android_sdk/platforms/android-17/data/res/drawable/activated_background.xml
If you open that file you will see the definition of the drawable which is important to understanding what is going on.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:drawable="#android:drawable/list_selector_background_selected" />
<item android:drawable="#color/transparent" />
</selector>
Here we are defining a drawable with two states - default state is just transparent background and if the state is "state_activated" then our drawable is "list_selector_background_selected".
see this link for background information on on drawables and states.
"list_selector_background_selected" is a 9 patch png file that is located in the drawable-hdpi folder.
Now you can see why we defined activatedBackgroundIndicator as a reference rather than linking directly to the drawable file - it allows you to pick the right drawable depending on your theme.
I wondered this as well at one point. A large amount of the Android resources seem to be like a black-box and can't see them directly. I may be missing them someplace, but I can't find them in the SDK source code. Here is what I do know.
android:background will take a drawable.
The syntax is in the style
Must be a reference to another resource, in the form "#[+][package:]type:name" or to a theme attribute in the form "?[package:][type:]name"
In this case the ? signifies to look at a theme in package android and it is of type attr where the name is activatedBackgroundIndicator.
You should be able to access this in the code-behind with android.R.attr.activatedBackgroundIndicator as well.
A list of Android attr properties can be found at R.attr
activatedBackgroundIndicator is a defined drawable in Android 3.0+ as
Drawable used as a background for activated items.
It's basically just a standard item defined in the OS. I can't seem to find in in the Android source, but here is a link to the documentation. activatedBackgroundIndicator
This is a form of attaching a value from a theme. The value is technically not known during resource compilation because the theme values may not be known at that point. Instead the value is resolved at runtime based on the actual theme taken from (most commonly) ContextThemeWrapper.
This provides a way of reusing resource values. I'm not talking performance-wise here, but rather organization and maintenance-wise. The attribute acts as it were a variable with the promise that it will hold an actual value at runtime.
This approach also allows for greater customization - instead of hardcoding the value of e.g. window background drawable it gets the actual drawable from a theme, supplying a chosen attribute as the key. This lets you override the value for that attribute. You simply need to:
Create your own theme (which is just a fancy name for a "style" resource), most commonly deriving from one of default themes.
Supply your own value for the attribute in question.
The platform will automatically use your value provided that you have specified your theme for an activity or application. You do this like described in the question. The general syntax of theme-attribute references is described here: Referencing style attributes. You will also find an example and description of the whole mechanism there.
Edit
One thing that should be noted is the actual attribute names and their existence in various platform versions. It's fairly common for new attributes to be introduced in next platform versions - for example some were added in version 3.0 for the purpose of ActionBar styling.
You should treat attribute names as part of the API - in other words, they are part of the contract you are allowed to use. This is very similar to classes and their signatures - you use LocationManager class for the purpose of obtaining last device location because you know from some source (tutorials, reference, official guides, etc.) what's the purpose of this class. Similarly, the attribute names and their purpose are (sometimes well, sometimes miserably) defined in the Android Platform documentation.
Update: There is a more detailed version available from the API Guide so I'd like to quote it.
A style attribute resource allows you to reference the value of an attribute in the currently-applied theme. Referencing a style attribute allows you to customize the look of UI elements by styling them to match standard variations supplied by the current theme, instead of supplying a hard-coded value. Referencing a style attribute essentially says, "use the style that is defined by this attribute, in the current theme."
To reference a style attribute, the name syntax is almost identical to the normal resource format, but instead of the at-symbol (#), use a question-mark (?), and the resource type portion is optional. For instance:`
Original Answer:
numan salati already offered an perfect answer but it have not addressed the "?" syntax. Here's a quote from API Guide Accessing Resources
To reference a style attribute, the name syntax is almost identical to the normal resource format, but instead of the at-symbol (#), use a question-mark (?), and the resource type portion is optional. For instance:
?[<package_name>:][<resource_type>/]<resource_name>

When should you use `#+id` instead of `#id`?

I have a bunch of Views in a <merge>, and I included that <merge> into a RelativeLayout. I try to refer to the IDs of those included Views to act as anchors for my other Views, but Eclipse complains that the IDs are not resolving. I found a workaround by using #+id rather than #id when I first refer to them rather than when I actually define the objects they refer to. I've already defined the two IDs in a Style and in the included <merge> where they are declared, so it feels a bit inefficient if I keep repeating the definition of the ID.
Is this the correct way of doing it? I'm assuming it's bad cause the '+' is another initialization. My current hypothesis is that you should use #+id when you first use the ID rather than when you initialize the object that the ID is going to represent, a bit like C/C++ and how they require at least a function prototype in the lines prior to the actual code that uses the function.
Another question I have is when you use the GUI-based UI builder of Eclipse, I noticed that they always use #+id rather than #id. Is this acceptable, cause it seems inefficient to me; it's as if the application will be spending more time determining whether or not the ID has been declared in R.id.
Using #+id format tells the Android asset compiler to assign an ID to your element, it isn't actually an id itself. So if I use #+id/myNewId the asset compiler will create a new id named myNewId and provide a number for it. The actual number can be accessed from your code as R.id.myNewId.
If you use an #id, the compiler will look for R.id.id. You can define your own id's in XML files, as explained here: http://developer.android.com/guide/topics/resources/more-resources.html#Id. You could create your own file in res/values/[your_filename].xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item
type="id"
name="id_name" />
</resources>
and then refer to #id_name, for e.g.
You can also use the Id's defined in the Android namespace: #android:id/empty
This is well explained in the Android documentation: http://developer.android.com/guide/topics/ui/declaring-layout.html#id
There's also some further discussion here: android:id what is the plus sign for

Categories

Resources