4

I'm a beginner in Android development but not in programming itself. Anyway, this question might be a bit dumb.

The question is: Are all classes in Android activities related to a UI element? I want to have a "regular" Java class from which i can normally create objects and i'm not figuring out how to "define it" and how to "call" it.

Any help would be much appreciated. Thanks

tyb
  • 4,749
  • 11
  • 31
  • 38
  • Create a Java source file with a class in it. Once it's deployed, instantiate the class with a "new" the same as any other Java class. Not sure what the issue is; are you having issues with your IDE or build environment? – Dave Newton Jan 13 '12 at 00:23
  • I guess I'm not sure what the issue is myself. I'm just struggling to write a class and create objects from it without it having to do with a UI. – tyb Jan 13 '12 at 00:29

2 Answers2

9

Yes you can have regular classes and no they are not all related to a UI element. This works pretty much like normal Java. So in Eclipse you can create a new class like in the image and follow the one page wizard.

Creating a new class in Eclipse

You will end up with some code like the below (I added a few bits for the example):

package this.is.your.package;

public class Person{

    private int age;

    public void setAge(int _age)
    {
        age = _age;
    }

}

You can then build methods and other stuff as normal. As for instantiating or accessing your class you will probably have to make it public for the activities to get it. However there are a many number of different ways of doing this but for the example above I could do.

public class MyActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Person me = new Person();
        me.setAge(22); //feels old
    }

As you can see this is all pretty normal.

Graham Smith
  • 25,627
  • 10
  • 46
  • 69
  • glad to be of help - I can see you are new to SO - can you mark an answer as correct via the tick symbol so that other users may see if there is an answer that was useful. Also thanks to vitakot's answer which is useful too. – Graham Smith Jan 13 '12 at 00:42
  • One more question: Can I use java libraries like java.util.Random or whatever or do I need a specific android library? Thanks – tyb Jan 13 '12 at 00:47
  • Most are - you will have to see. As for your example - in the activity add 'import java.util.Random;'. Eclipse will help you as you go, if you type Random random = new Random(); - the 'Random' will be underlined and Eclipse will offer to import the java.Util.Random. – Graham Smith Jan 13 '12 at 00:50
  • Here is how to add libraries/JARs to your project http://stackoverflow.com/questions/3642928/adding-a-library-jar-to-an-eclipse-android-project – Graham Smith Jan 13 '12 at 00:50
  • Sorry according to my view it has no tick on any answer. – Graham Smith Jan 13 '12 at 00:52
  • Is it ticked now? I keep clicking it but it doesn't seem to stick. – tyb Jan 13 '12 at 00:56
  • refresh the page - sadly no but if it doesnt work dont worry. – Graham Smith Jan 13 '12 at 00:59
  • sorry man, tried a hundred times in two different browsers. Don't know what is the problem :\ – tyb Jan 13 '12 at 01:14
2

The answer is NO. All activities are regular Java classes and you can - of course, have many non-UI classes like Application, you can have helpers etc... If I understand you question correctly, you are confused by the fact the Activity doesn't have user defined constructor and can be created only indirectly by calling startActivity method, but in other acpects it is a common Java class.

vitakot
  • 3,786
  • 4
  • 27
  • 59