NetBeans and Android image - android

New to Android development and have decided to use NetBeans 6.9.1 as my IDE. So far the process has been somewhat painful, but I'm getting things rolling. However, I am creating an ImageView subclass for my first custom View and I can't figure out how to add my Box.png file to the project. Drag and Drop doesn't work, there are no right-click options to add a file to the Resources folder, no dropdown menus to add images, no way to add the image to a package. Could use some insight, thanks!

Just go to the project folder and copy the images you want into the res/drawable folder. The IDE helps you a lot with code completion, error checking, etc... but that simple task can be done by hand.
Then, you can reference your resources by using something like: R.drawable.image Notice that I'm not using the image extension. If you wonder what R is, let me give you a brief explanation:
Each resource that is saved in the resources directory is referenced in the R class. That's a file that is autogenerated by Android and it's used to reference those resources from your code. In this case, it will be in R.drawable.* since it's a drawable resource. There are other kind of resources, like layouts: R.layout.something or strings R.string.whatever. That's essential for the android development, so you better read some tutorials (or buy books) in order for you to get started.
So, in your case will be something like setImageDrawable(R.layout.wood); However, I highly recommend to read first a couple of tutorials. Google about it, you will find tons of them.

Related

Android Studio - What is the Java Resources Folder?

I know there are two well-known folders where one can put resources.
The first is the /assets folder, the documentation says:
Contains file that should be compiled into an .apk file as-is. You can navigate this directory in the same way as a typical file system using URIs and read files as a stream of bytes using the AssetManager. For example, this is a good location for textures and game data.
The second is the /res folder, the documentation says:
Contains application resources, such as drawable files, layout files, and UI string. See Application Resources for more information.
Now i was wondering, there is a third option to create a resource folder "New > Folder > Java Resources Folder"
To me i looks like some part of the Android Plugin in IntelliJ because it has a little Android symbol in front of it .
Any ideas what the use of it could be? I couldn't find any documentation about it.
My first guess would be to use it in situations where you want to supply resources to a JVM Test.
In standard java world
Resources can be embedded directly in "your source tree" and used with Class's method getResource (see java documentation for a more precise description).
In android world
This practice is not recommanded (do not work at all, because such resources are removed from generated APK). You can still declare Java resources folders (see build.gradle :
sourceSets {
main {
resources.srcDirs = ['src/main/java/yourresourcesfolder']
}
}
And the result in the apk :
Conclusion
As resources's folder tree is removed,using java's resource folder become from my point of view, useless (in an android projet). Using android's asset folder is a better choice (also avoid resource's name conflict, but it's an another story :D )
This post is a bit old, but I want to bring an answer that explain one use case for the java resources directory on Android. If this folder exists in the options of a project it's because something can be done with it...
Enters the Service Loader, that helps to connect other services (aka libraries) into your main app, it can be used as a Gateway for your Android library to extend the functionality of a feature, exposing only the interfaces or abstract classes, with the Service Loader providing the implementation instead of your project having direct access to them.
Here is an example of how it's implemented. So, in summary the folder (META-INF/services) and files you have to create in order to use the Service Loader on an Android App, NEED to be inside this Java Resources Folder, otherwise your provider won't be able to see any implementation.
You can see it working in my sample here if you want to check it out: https://github.com/difereto-globant/test-library-feature/tree/1.0.9.

How to create buttons dynamicly based on file/folder structure in android

im new to android and im trying to make a "simple" app.
However i would like to know if, what i have planned, is possible in android.
Basicly it's a crude database reader/navigator.
I made a folder structure in the assets folder with a few directories and *.txt files.
Now the plan is to read/scan the folder structure on startup of the program and dynamicly create buttons labeld based on the folder,- and file names.
The buttons have to link to the files and folders so the user can navigate the folder structure and open a *.txt file and read it. Also the idea is that the buttons reflect the folder structure as it is in the assets folder.
The idea is that the user can easly retrieve info from the app and i can add info to the app trough *.txt files and adjust the structure just by adding/removing directories and files in the assets folder.
i am using android studio but like i already say'd: i am a beginner with programming and would like to know if this is even possible with android.
And if it is: could someone please tell me how i should start this project or link me to some example code on how this could be done?
Thanks in advance.
If i read your idea correctly, what you are trying to achieve is very much possible. I would start looking at the AssetManager class and I believe what you are looking for is the list() command which should give you what you need.
https://developer.android.com/reference/android/content/res/AssetManager.html
You can obtain the AssetManager from everywhere within your app through the Context object which you will find to be everywhere around. ;)
From there on you can build your hierarchy and open files.
There are some useful wizard projects in Android studio, which will basically setup a general structure of an Android App. I would recommend starting with a Single Activity application and move on from there.
It sounds like ListView is something that could be beneficial for you here. ListView allows you to dynamically create buttons, or whatever you need. ListView has the added advantage that you can use an XML file to inform the ListView of how to display the buttons, images or whatever you want to display for each element of the list. There are loads of videos demonstrating how to use ListView, but here is a link to its documentation: https://developer.android.com/guide/topics/ui/layout/listview.html
Also, if you'd like a structured way of learning how to develop apps, I'd recommend the Android courses on Udacity to help you out.
Best of luck!

How do I move files in Android IDE

I'm using Android IDE to develop, and I need to move source files from one folder to another, but I can't seem to find a way to actually MOVE a file (even copy and delete in two steps would work).
Am I blind? This seems like a required feature of any IDE, and given how good Android IDE is, I find it shocking if this feature was overlooked.
NOTE: This question is NOT about Android Studio!
You are right. The (essential) feature to move files around is missing from Android IDE.
Workaround:
You can manipulate the files in any explorer. Your project files are placed in /sdcard/AppProjects/YourProject.
You can count on the fact that such a feature will not be missing for long.
I would download a file manager (I like root browser) and switch between the two apps as a temporary work around. That app is updated fairly often so I would also just shoot the developers a feature request. I am sure they would consider it essential as you do.
PREFACE: Use a file manager; this hack renames Java classes.
Rename, with the relative path as the new name.
Let's say I wanted to move file from /sdcard/A to /sdcard/B. I'd long-press on file, chose rename, and type the new destination as ../B/file.
This doesn't seem like deliberate feature, rather a side effect of blindly passing path strings to the underlying framework. Can't say I don't like it, although, I'd recommend using a proper file explorer app.
Right-mouse click on any element (folder, Class, or XML file) the click = 'cut'.
Right-mouse click on the folder where you would like the files to be moved to, and then select 'paste'.
This works the same as moving files in the file-system.
For the most part (depending on where you are moving to and from) you can just drag-and-drop your files to move them as well.
I often achieve this goal by simply drag and drop. And the IDE also takes care of all the refactoring stuff.

Android R.java - "overloading" values

I've searched for information about this but it's hard when I don't really know what to search for.
I'm working on a few android projects which share a common base (that I put in a few libs). The libs works with both classes and xml-files, and some other resources too. The most common is that I have a drawable xml which gets color information from an xml-file which defines which colors to use, called something like "foobar_colors.xml" or so. The "foobar_colors.xml" in the lib itself defines default colors.
When I use the lib in the projects these colors needs to change (to use the client company colors or so), and I'm not sure how the best way is to accomplish this... The libs uses allot of resorces, their own xml-layouts and such, so I really can't always "declare things stylable".
The way I do it now is include the "foobar" lib in my project, copy the "foobar_colors.xml" to my project resources and change their values there. This works since R.java seems to "overload" values like the lib itself sets "foobar_color1" to be black and the project sets it again to be red (and the projects defines has higher priority).
Now I'm just wondering, is this safe? Is there any better way to do it? Possible problems that might come up in the future?

organizing android apps in eclipse

I have an app on the iPhone and need to port it to android. For this I would like to group screen related files like classes and xml per screen in one "screen group" per screen somehow, ideally also strings and other value files
if I use folders I can only group res files separately and src files separately.
what would be the best way?
Thanks very much!
EDIT:
If that should not be possible, how to best then solve this issue? Do you create a subfolder in the src and another in the res for each screen?
The way you group files for the iphone is not possible for an android project. Android has pre determined folders which hold specific files, if you break this structure, your building process will fail. Its not ideal but that just how it it.
When it comes to source java files, they follow the concept of packages which are basically folders. The 'src' folder is the part where you can create sub folders as you desire. If you are adamant about keeping the files related to a screen in one place, you should create the layouts with java code and not use layout xml files.
But using xml layout files make development much easier and faster. Consider that as the presentation and java files as the logic+data. So group java files as you want and leave xml files in the layout folder with easy to identify names.
android uses certain directory layout for project structures (i.e. convention over configuration). Basically you will want to put your XML layout files in res/layout directory. Please read http://developer.android.com/guide/developing/projects/index.html#ApplicationProjects for further information.
Unfortunately, there's no easy way to do this in Eclipse. You can't create custom directories in your Android app's /res directory, you can only use permitted dir-names. E.g. you can't have a /res/layout-myscreen1 and /res/layout-myscreen2. You also must put your resources in /res, and your code files in packages, so they're at separate places in your project.
You can use Working Sets to group related files together however, but they're quite painful to use IMHO. Check the eclipse docs and tutorials out on them.

Categories

Resources