android-resources

How can I get a resource content from a static context?

一个人想着一个人 提交于 2019-11-26 00:43:52
问题 I want to read strings from an xml file before I do much of anything else like setText on widgets, so how can I do that without an activity object to call getResources() on? 回答1: Create a subclass of Application, for instance public class App extends Application { Set the android:name attribute of your <application> tag in the AndroidManifest.xml to point to your new class, e.g. android:name=".App" In the onCreate() method of your app instance, save your context (e.g. this ) to a static field

getColor(int id) deprecated on Android 6.0 Marshmallow (API 23)

纵饮孤独 提交于 2019-11-25 23:43:26
问题 The Resources.getColor(int id) method has been deprecated. @ColorInt @Deprecated public int getColor(@ColorRes int id) throws NotFoundException { return getColor(id, null); } What should I do? 回答1: Starting from Android Support Library 23, a new getColor() method has been added to ContextCompat . Its description from the official JavaDoc: Returns a color associated with a particular resource ID Starting in M, the returned color will be styled for the specified Context's theme. So, just call :

Android getResources().getDrawable() deprecated API 22

我是研究僧i 提交于 2019-11-25 23:15:20
问题 With new android API 22 getResources().getDrawable() is now deprecated. Now the best approach is to use only getDrawable() . What changed? 回答1: You have some options to handle this deprecation the right (and future proof ) way, depending on which kind of drawable you are loading: A) drawables with theme attributes ContextCompat.getDrawable(getActivity(), R.drawable.name); You'll obtain a styled Drawable as your Activity theme instructs. This is probably what you need. B) drawables without

Can the Android drawable directory contain subdirectories?

流过昼夜 提交于 2019-11-25 23:08:21
问题 In the Android SDK documentation, all of the examples used with the @drawable/my_image xml syntax directly address images that are stored in the res/drawable directory in my project. I am wondering if it is explicitly not okay to create a sub directory within the drawable directory. For example, if I had the following directory layout: res/drawable -- sandwiches -- tunaOnRye.png -- hamAndSwiss.png -- drinks -- coldOne.png -- hotTea.png Could I reference the image of a tuna salad sandwich as

Defining custom attrs

走远了吗. 提交于 2019-11-25 23:07:56
问题 I need to implement my own attributes like in com.android.R.attr Found nothing in official documentation so I need information about how to define these attrs and how to use them from my code. 回答1: Currently the best documentation is the source. You can take a look at it here (attrs.xml). You can define attributes in the top <resources> element or inside of a <declare-styleable> element. If I'm going to use an attr in more than one place I put it in the root element. Note, all attributes

How can I assign an ID to a view programmatically?

别来无恙 提交于 2019-11-25 22:48:13
问题 In an XML file, we can assign an ID to a view like android:id=\"@+id/something\" and then call findViewById() , but when creating a view programmatically, how do I assign an ID? I think setId() is not the same as default assignment. setId() is extra. Can anybody correct me? 回答1: Android id overview An Android id is an integer commonly used to identify views; this id can be assigned via XML (when possible) and via code (programmatically.) The id is most useful for getting references for XML

“R cannot be resolved to a variable”? [duplicate]

萝らか妹 提交于 2019-11-25 22:35:17
问题 This question already has answers here : Developing for Android in Eclipse: R.java not regenerating (64 answers) Closed 5 years ago . In Eclipse, I\'ve created a project from a source and now it shows errors - \"R cannot be resolved to a variable\". From what I found here, I had cleared and rebuilt the project, but still the R file doesn\'t appear in the /gen folder. Any ideas? 回答1: Dont worry. First you may clean the project, then run the project. If this does not work then follow the

Android, getting resource ID from string?

雨燕双飞 提交于 2019-11-25 22:24:50
问题 I need to pass a resource ID to a method in one of my classes. It needs to use both the id that the reference points to and also it needs the string. How should I best achieve this? For example: R.drawable.icon I need to get the integer ID of this, but I also need access to the string \"icon\". It would be preferable if all I had to pass to the method is the \"icon\" string. 回答1: @EboMike: I didn't know that Resources.getIdentifier() existed. In my projects I used the following code to do

How to get a resource id with a known resource name?

▼魔方 西西 提交于 2019-11-25 22:22:49
问题 I want to access a resource like a String or a Drawable by its name and not its int id. Which method would I use for this? 回答1: It will be something like: R.drawable.resourcename Make sure you don't have the Android.R namespace imported as it can confuse Eclipse (if that's what you're using). If that doesn't work, you can always use a context's getResources method ... Drawable resImg = this.context.getResources().getDrawable(R.drawable.resource); Where this.context is intialised as an

Is there a way to get the source code from an APK file?

╄→гoц情女王★ 提交于 2019-11-25 22:17:21
问题 The hard drive on my laptop just crashed and I lost all the source code for an app that I have been working on for the past two months. All I have is the APK file that is stored in my email from when I sent it to a friend. Is there any way to extract my source code from this APK file? 回答1: Simple way: use online tool http://www.javadecompilers.com/apk, upload apk and get source code. Procedure for decoding .apk files, step-by-step method: Step 1: Make a new folder and copy over the .apk file