Using Android internal drawables - android

I'm trying to write a tristate switch. But I'm failing to access the default com.android.internal.R.styleable.Switch_track drawable to use as background.
How to get that drawable?
Is there another approach for getting the default background?

You should not access private resources, you can't be sure that the resource will be available on all devices. And even if it is available, you can't be sure it will be the same.
If you want to use a private resource, you have to copy it into your project resources from SDK or Android sources.

Just for reference:
While it is not advised to access internal resources and you should be aware, that they can change or could be removed by a vendor or with an OS update, it is possible to access them at runtime:
int id = Resources.getSystem().getIdentifier("Switch_track", "styleable", "android");

Related

Resources.getSystem().openRawResource gives resource not found exception

In my android app I want to access my text file in static method but using this code:
InputStream is = Resources.getSystem().openRawResource(R.raw.adv_types);
gives me run time exception: resource Not Found Exception
while I can use this code to access the file in non static method:
InputStream is = getResources().openRawResource(R.raw.adv_types);
though its not usable in static method.
Do you have any idea why the first code does not work?
And what is the solution to access text file (or resources in general) in static method?
because Resources.getSystem() give you access only to the system resources not to the application resources. From the documentation:
Return a global shared Resources object that provides access to only
system resources (no application resources), and is not configured for
the current screen (can not use dimension units, does not change based
on orientation, etc).

How to access checkmark drawable in Android OS?

I want to use Android OS drawables instead of providing my own.
How to access check mark icon?
I'm looking for check mark without rectagnle arround.
To access system provided resource, just access R.java file of Android.
for example:
android.R.drawable.checkbox_on_background // To access drawable resource
android.R.color.black // To access color resources
android.R.dimen.colorstrip_height; // To acess dimension resources
Update:
If you are using Android 2.3 Gingerbread then you can access below in-built drawables:
btn_check_buttonless_off.png
btn_check_buttonless_on.png

Getting resources of another Application

Assume I've got 2 Application A and B.
I want to access resources (drawables, images, strings) of an B application from A application. How would I do that?
Edit:
You can also designate an Android project as a library project, which allows it to be shared with other projects that depend on it. Once an Android project is designated as a library project, it cannot be installed onto a device.
Does it mean That I can not use my library on android market?
My aim is to use default resources of Application A, but also if someone what he can download Application B and use it resources.
If you are the developer of both applications then the right approach would be, as othes noted, to create an Android library project and share it between apps.
Nevertheles, you can still access resources from another app, even if this is not your app:
Resources res = context.getPackageManager().getResourcesForApplication("com.example.foo")
If both apps are your own and you want to bundle these resources at build-time:
Create an Android Library Project, put all the needed resources into it and reference it from both your apps.
Edit to your edit:
You can use the library in the android market. You just can not use it alone (compile it). You have to build an app that uses the library. When you build that app, all the needed content gets basically grabbed from the library and copied to your apk. Which you can put on the market as usual.
This does not work when you want to access resources from an app that gets downloaded onto the device at runtime. A library project bundles all resources when you build the app that uses it. Peter Knego's response is the correct one when accessing at runtime.
It is possible if those both application are yours. Or you can used any Android Library for both these application. If you want you also create your own Android Library for this kind of work. Thnx.
Peter's answer above is great, but I'd like to elaborate a little more...
When you control appA and appB and want to get a drawable from appB, while in appA, then do the following:
Get the identifier of the drawable when in appB (for example, R.drawable.iconInAppB)
Then, in appA
Resources res = context.getPackageManager().getResourcesForApplication("com.example.appB");
Drawable d = res.getDrawable(integer);
//integer was what appB assigned to R.drawable.iconInAppB
*Note: The integer id assigned to R.drawable.iconInAppB will change over time so don't rely on hardcoding. I save the key-value pairs (String resourceName, int resourceId) in a database in appB and access them using a content provider.
For example, let's get the string-array with the ID of entryvalues_font_size from the package com.android.settings
String[] enries = null;
try {
// You can get PackageManager from the Context
Resources res = getPackageManager().getResourcesForApplication("com.android.settings");
int resId = res.getIdentifier("entryvalues_font_size", "array", "com.android.settings");
if (resId != 0) {
enries = res.getStringArray(resId);
}
}
catch (NameNotFoundException e) {
e.printStackTrace();
}

Where can I find the android.R.* files?

I am looking for the preset android files that come with the Android os. Can someone direct me to the source?
(Android install path)\android-sdk-windows\platforms\android-version\data\res
Hope this is what you mean.
Call the following as an example:
android.R.*
You can access built-in animations, layouts, text fields, whatever you need.
So if you wanted to find the default android black color, call the following:
View.setBackGroundColor(android.R.color.black);
This returns the INTEGER containing the pointer to the appropriate resource files.
All R files are generated as integer pointers, so this is the proper way to access these resources.
Hope this helped!
Try codesearch from google:
http://google.com/codesearch (choose android on the left side) or directly:
http://google.com/codesearch#cZwlSNS7aEw/frameworks/base/core/res/&exact_package=android
go to gen-> android.support.v7.appcompat ->R.java
Android generated a special called the
‘R’ file. This is a file of constants that allow you to
get Java references to the TextView you defined
in main.xml In fact, you can get references to all
kinds of in app resources you define! But remember
the String resources you defined in XML? You can
get references to those too.
You mean R.java? It will be in %project root%/res/android/%your project name%/

Is it possible to access resources in another APK without using content providers?

I thought that it is impossible to access resources contained in other APK unless content providers are used. However, I stumbled upon an app called Better Keyboard. It uses skins, so I decided to download an example of a skin.
An example skin really surprised me. It contains almost no code, declares only an activity and no content providers. Nontheless it contains resources and they are apparently somehow accessed from Better Keyboard app if the APK with the skin gets installed. So how is it done?
I believe you're looking for
Resources res = mPackageManager.getResourcesForApplication("org.example.foo");
http://developer.android.com/reference/android/content/pm/PackageManager.html#getResourcesForApplication%28java.lang.String%29
Try this
PathClassLoader loader =
new PathClassLoader ("/data/app/com.skin.apk", null,PathClassLoader.getSystemClassLoader());
InputStream f = loader.getResourceAsStream("res/drawable/icon.png");
Bitmap bitmap=BitmapFactory.decodeStream(f);
BitmapDrawable b=new BitmapDrawable(bitmap);
mButton.setBackgroundDrawable(b);
It is possible by using getresoucesforapplication and you need res.getidentifier to retrieve the resource Id. However, It seem not work when retrieving style resource.

Categories

Resources