I have multiple activities. For organization purposes I want to to put them in folders without affecting the project. How can this be done?
Just create a new folder.
In fact, you can organize your project and separate the class as you want. This way, you keep your project organized and you can hide some methods and let them to be used only for classes in same package, for example.
I use do:
...\app\src\main\java\com\example\myapp\activities
...\app\src\main\java\com\example\myapp\service
...\app\src\main\java\com\example\myapp\dataprovider
...\app\src\main\java\com\example\myapp\adapters
So, your activities classes would start with:
package com.example.myapp.activities;
And your services would start with:
package com.example.myapp.service;
Also, you may need to adjust your imports:
import com.example.myapp.dataprovider.Class1;
import com.example.myapp.dataprovider.Class2;
However, this should be done automatically by Android Studio.
Just remember that doing that, you are creating a different packages. This way, you have to take a special attention with method accessbility (private, protected, public)
However, since we usually use private and public modifiers, we should not have any problem.
This may sound like a question asked by another user, but the solution does not work for me so I would like to try asking about my code.
I have created a folder under the res directory that contains some java classes I would like to reference/ call from one of my Activities but the import does not work and the classes do not seem to be recognised.
Calling code in the Activity file:
// Method to populate the screen with images
public void initializeWithCGImages(){
ArrayList<src.cercia.batik.app.DesignUnitViewer> mapviewers,viewerList;
ArrayList<src.cercia.batik.geometry.Population> populations;
src.cercia.batik.app.InitializePopulation initialize=new src.cercia.batik.app.InitializePopulation();
populations = initialize.getInitialGenes();
mapviewers=new ArrayList<cercia.batik.app.DesignUnitViewer>();
...
}
Code for the reference in folder: "res/src/cercia/batik/app":
package cercia.batik.app;
import cercia.batik.geometry.Population;
public class InitializePopulation {
public InitializePopulation(){
}
...
}
The res directory is for resources. Things like imaged and layouts. The compiler isn't going to look there for Java files. Move them up into your java folder.
I just start to study android, so have some problem
i have a file res/xml/settings.xml - settings from menu
create class Prefs and try to use settings from file above
package org.example.sudoku;
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class Prefs extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}
Problem, that addPreferencesFromResource cant be used and i need to use preferencesResId (according to comments), but if i write something like this
preferencesResId(R.xml.settings);
its not good.
Where is error in code? Could any1 help?
Also i try to learn android by using book - Hello Android.
preferencesResId() is not a method, it is a placeholder in conversation for your resource id (R.xml.settings).
So although
addPreferencesFromResource(R.xml.settings);
Is deprecated, if you're using this approach, this is still the most correct way - there isn't another way using the deprecated approach.
I suggest you look at this SO question - it tells you what you should be using instead (PreferenceFragments). If you need a code sample, fire up Eclipse and the ADT plugin and make a SettingsActivity via the new Activity Wizard.
And as usual, here is the full JavaDoc for PreferenceActivity.
I've got a few projects Im trying to combine into one and Ive already included all the other projects as library ones through the settings. What Im sort of stuck on is how to use them in my main activity.
Ive messed around and feel like Ive almost got it but Im just stuck. I know I have to declare all the new activities in the main manifest but Im not sure about what to include in the main .java activity inorder to call from the newly included stuff.
Is this something that I could figure out by looking at the android api demos that come with the sdk?
Can some one point me to an open source project somewhere and explain the process used to include the library projects.
Any help would be much appreciated.
Are you using eclipse to manage your settings? It doesnt really matter, but its easier to make sure your library paths are set correctly and are accessible by the calling project.
Assuming that they are accessible, you just access the classes in the library like any other class: Import the package and instantiate the class as you would normally. For an android activity, this means that you would likely create an activity from the library based on some response from your main activity. It doesn't matter if that Activity is local to this project or imported from a library. eg:
// import the activity/package/class from your library
import com.mylibrary.activities.ImportedActivity;
public class LocalActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
// Button Code
button = (ImageView) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// create a new intent based on your library activity
Intent myIntent = new Intent(v.getContext(), ImportedActivity.class);
startActivityForResult(myIntent, 0);
}
});
}
note, I have not tried to compile the code above, its just for purposes of demonstration.
If your libraries are correctly referenced in eclipse, this should work. If not you will get errors on either the import of the external libraries (package not found) or build errors when the actual library is needed.
I just started playing around with android development, and already with just an attempt at making a button, I have encountered a problem.
The error I'm given in the following code is right on "R.id.button1".
It says id cannot be resolved or is not a field.
Do I need to manually reference every single object I make in the layout xml file? I found that this did work, but it does seem to be a bit much for every button I want to make...
package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
private Button button1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
finish();
}
});
}
}
I've been wasting a lot of time (two weeks) because of the same problem until I discovered the problem wasn't mine but Eclipse's.
I guess there's a lot of people with the same problem.
Just try this: Save your project, close Eclipse and then open it again. So simple.
Do I need to manually reference every single object I make in the layout xml file
Yes, otherwise you won't be able to do anything with those views. It's not that bad actually. So, each time you create a view in your XML, and you want to reference it, put an ID:
<View
android:id="#+id/the_id"/>
And then, from your code you can reference it using the R class. You can type, in the example, R.id.the_id and then Ctrl+Shift+O to make Eclipse auto import the needed files.
You can speed up your productivity by using frameworks like Roboguice; I think it's for lazy people, though.
This answer is not applicable to this question (looking at code you have provided). Just adding it if someone else stumbles here and above mentioned answers do not help.
If cleaning (Project --> clean) doesn't helps or saving and restarting eclipse doesn't help either, check for the following incorrect import.
import android.R;
Which Eclipse sometimes add by mistake on auto-import (Ctrl+Shift+O).
Remove that line (import) and it's done :D
Following this EXCELLENT tutorial , I encountered the same problem. After reading Carmello's answer (Sept 17, 2011. 07:23) I simply clicked File->Save All, and voila, 'button0' was automagically defined, and even syntax highlighted.
If "R.id.button1" is not defined, then you'll get a compile error, just as you saw. If you don't define this in the layout, then it won't be defined.
You don't have to specify every object you create in the layout, but you do if you try to reference it from "R.*". You can manually create buttons and other objects that are not specified in the layout.
I ran through the same issues for time being. Plz, do not forget to define as follows:
<View
android:id="#+id/button1" />
if you are using the id in your .java class.
Button b =(Button) findViewById(R.id.button1);
Being said that, the id defined in xml file must match with the id in findViewById().
Go to the 'R.java' file under the 'gen' folder and check whether your 'button1' is present under the class 'id'.If not,then this could be the reason you got that error.When you use the statement " R.id. " make sure that the is present under the appropriate class,in this case under the 'id' class.
R.id is a generated object that assigns int numbers to resources. Try this go to your gen/mypackage/R.java and delete the file. As you can see it is re-generated. This file provides static references where as the context is more of the dynamic state of your app. If you have syntax errors that will prevent automatic re-generation of that R.java file so you will get lots or R. errors. As everyone else has said you can click save all icon or ctl+shift+s on windows. You can clean the project project/clean and that will clean up 95% of those exceptions. Yes eclipse is buggy that way but netbeans does not support android that well. this link may help
Good luck
Do these things,anyone of this will help you
Project -> Clean,
Right click -> Fix Project Properties
Restart Eclipse
make some fake modification in manifest and save
check your console for any error message
check your drawable folder, check the image names satisfy the rules