Getting Resources from SherlockFragment - android

I am trying to get a color from colors.xml, using something along the lines of
view.setBackgroundColor(getResources().getColor(R.color.bgcolor_view));
Although there is a R.color.bgcolor_view in my colors.xml file, the R.color part only shows colors that are available in the built-in android settings. So I'm assuming that getResources() is not getting the resources from my project.
So, my question is, how do I obtain resources from within a SherlockFragment?

Look at your imports, you probably imported android.R, so remove this import and double check that you import your own R from your own project.

Related

Android Studio Kotlin Class doesn't find resources

I have a problem where every single resource isn't found in my kotlin class, like strings, layouts, menus, drawables, and so on. They do exist.
To solve this, make sure Android Studio hasn't auto-imported android.R. This will change the meaning of R from my resources to the library.

R.drawable being replaced with android.R.drawable

I am following a video tutorial for Android Studio that tells me to enter this line:
public class customswipeadapter extends PagerAdapter {
private int[] image_resources = {R.drawable.picture};
}
Everything is good until I try to enter the R.drawable Android Studio only offers the suggestion android.R.drawable which is then flagged as an error.
How can I solve this?
This happens sometimes when your android studio has Imports on the fly :P. To configure your Android studio use this link. And also do this steps,
Go to settings->Editors->General->Auto Import
In JAVA uncheck Optimize import on fly, and Add unambiguous imports on fly.
You can use both R.drawable.picture and android.R.drawable. Android Studio support both. So if you want to use R.drawable.picture just simply type but remember in your drawable folder image name picture must be save.
In you project directory, there should be a file called 'res'. If you right click on that and select new Android Resource Directory, it'll take you to another window. From here, change resource type to show drawable, and click OK. Now, put whatever image you would like to draw into drawable, and you should be good to reference it in your code like you were trying to do.
I've had some issues with this in the past, and once or twice I just had to restart Android Studio. Rebuilding the project may also work, but I've had better luck with a simple restart.

How to import multiple vector drawables to Android Studio project

I used https://inloop.github.io/svg2android/ to convert 50+ SVG's to XML as suggested in this answer. Now I have a .zip containing all the XML files. Where do I put them to be able to use them in Android Studio?
I know I can import one by one but that's what I'm trying to avoid in the first place.
Just put the xml files in drawable folder, and you will be able to retrieve them using R.drawable.names

When would I use android.R over my local R.?

If I want to set content view then I use:
setContentView(R.layout.test);
But when I set a theme, why do I have to add android to the beginning of R.style.x?
setTheme(android.R.style.Theme_Light);
setContentView(R.layout.test);
The android API define a huge amount of standard resources like theme, drawable, attributes, ...
When you need to refer to one of those resources : you must use android.R
android.R is a reference to the Android R.java file. It is not custom to your app. It contains a reference to every style, resource, anything that you would ever use in an Android app.
On the other hand, R.x (without the android prefix) refers to your local app. It contains specific styles, resources, etc. that have been defined for your app, and is much more specific.
So as to your question of which to use:
Use android.R if you want to reference a standard style. This would be something like android.R.style.holo. It could also be animations, but keep in mind these are not custom, these are the building blocks.
Use R.x when you are declaring something local. This will be something that you have definded, such as R.style.MyTheme or R.layout.MyLayout.
You are not forced to use android.R.style.Theme_Light you can create your theme and use R.style.myTheme.
You use android.R.* when you want to use a layout/image/attribute/etc. already built inside Android without the need to rewrite it again.
You can use android.R.* like R.x everywhere you want (they are the same thing, the difference that R.x is build with your application while android.R is already here). You can remake things, but why do more work when Google already did it for you?

R.layout shows only Android default template proposals.

I have a couple of xml files in my layout folder but when I use them in my class the template proposals doesn't show my layout files but shows android default proposals. There seem to be no error in any of my files at all. I tried deleting the R.java file and rebuilding and restarting eclipse but nothing seems to be working. PLease help. I need to get this done asap :(
Thanks,
Sowmya.
This may be because, when you try to use your xml layouts, the default android R file is imported.
import com.android.R
Check that. If it is there, delete that import and import the correct R file which starts with your package name.
First look are you setting xml name fine like
e.g
you have your.xml
Then in onCreate
super.onCreate(savedInstanceState);
setContentView(R.layout.your);
Then clean your project.
if you have design multiple xml with same name then you must have to place them in appropriate folder using hdpi etc
Further details available here
or you can write
import.R.layout
It will work for sure

Categories

Resources