it seems like I can create an Activity thus:
Code:
public class MainActivity extends Activity
and then create other activities like this:
Code:
public class SplashActivity extends MainActivity
and the SplashActivity should inherit the "extend Activity".
Fine.
But what is the point in this? It seems that even if I extend a master java file I still don't get any of the other imports inherited, like os.Bundle.
If I could inherit all the imports from a master java file I'd understand extending the MainActivity.
Can anyone shed light on this?
extends has absolutely nothing to do with imports. It allows the subclass to inherit public and protected methods and variables of the superclass and also allows you to override the methods if you choose to.
To fully understand the concept behind this, i suggest read a java book.. xD But without going into a whole lot of confusing stuff ill give it a go...
The example you gave you are essentially saying SplashActivity is a grandchild of Activity. the Activity is unique to the android platform. here is a basic java example:
Class Car extends Vehicle or Class truck extends Vehicle.. extends does not just give you methods, you inherit methods that override previous ones. if you are looking for a quick way to import packages like "EditText" after typing that use: Cntl + Shift +O.
extends is a java term for inheritance. you can think about it like if you had a Fruit class with certain functionality, then you have an Apple class that extends Fruit. Apple gains all the functionality from Fruit but you can then add new functionality specific to an Apple class.
and if you think this is done for import reasons then you are thinking on a whole different level than the makers of java. try getting an ide that auto imports Classes as you use them in your code, like IntelliJ
Thanks for the information guys.
I knew extends would allow method inheritance, I just though it would also allow package import inheritance.
One of the hurdles I come up against in learning Android development is that even the Google tutorials don't tell you that you need to import different packages for different things - e.g. the Tabs tutorial doesn't explain that you need to import all sorts of stuff and the only way I got it working was that Eclipse is smart and will suggest solutions to your errors.
Thanks again.
Subjective Effect said:
Thanks for the information guys.
I knew extends would allow method inheritance, I just though it would also allow package import inheritance.
One of the hurdles I come up against in learning Android development is that even the Google tutorials don't tell you that you need to import different packages for different things - e.g. the Tabs tutorial doesn't explain that you need to import all sorts of stuff and the only way I got it working was that Eclipse is smart and will suggest solutions to your errors.
Thanks again.
Click to expand...
Click to collapse
The import statement in java is kind of misleading. It's not really importing anything. It just allow you to refer to a class in that java file by using it's short class name (Activity) instead of it's fully qualified name (android.app.Activity). You never really have to use an import statement in your code but if you don't you just wind up having to type the full package name in front every time you use a class name.
So say for instance you didn't have an
import android.app.Activity;
statement in your java file. In order to get the compiler to know what Activity you are talking about you would need to write android.app.Activity everywhere in your code instead of the much simpler way of importing it once and referring to it as Activity.
Hope that made sense.
Also, say for example you had another class in a different package with the same name...
com.example.Activity
If you wanted to use that class in the same java file that you are using android.app.Activity an import is not even going to help you. You would always need to use the fully qualitied name to refer to each Activity class because if you just say "Activity" the compiler don't know if you mean android.app.Activity or com.example.Activity.
Related
So I've been reading a book and have a question about a setting that hasn't been covered. I search googler, but couldn't find anything that explained it.
Under Properties, you have Application name, Package name, and Create Activity.
Where can I find more info about this? (Activity class' that apply to droid and how to use them). I've been able to make the example activities but they don't use that field. So I'm a little lost as to what it is.
Thanks!
GhettoBSD said:
So I've been reading a book and have a question about a setting that hasn't been covered. I search googler, but couldn't find anything that explained it.
Under Properties, you have Application name, Package name, and Create Activity.
Where can I find more info about this? (Activity class' that apply to droid and how to use them). I've been able to make the example activities but they don't use that field. So I'm a little lost as to what it is.
Thanks!
Click to expand...
Click to collapse
Have you been to http://developer.android.com/index.html read up there iam sure it explains everything to get you started
Application Name
This is the human-readable title for your application — the name that will appear on the Android device.
Package Name
This is the package namespace (following the same rules as for packages in the Java programming language) that you want all your source code to reside under. This also sets the package name under which the stub Activity will be generated.
Your package name must be unique across all packages installed on the Android system; for this reason, it's important to use a standard domain-style package for your applications. The example above uses the "com.example" namespace, which is a namespace reserved for example documentation — when you develop your own applications, you should use a namespace that's appropriate to your organization or entity.
Create Activity
This is the name for the class stub that will be generated by the plugin. This will be a subclass of Android's Activity class. An Activity is simply a class that can run and do work. It can create a UI if it chooses, but it doesn't need to. As the checkbox suggests, this is optional, but an Activity is almost always used as the basis for an application.
I have been there, but I'd like more information.
Create Activity
This is the name for the class stub that will be generated by the plugin. This will be a subclass of Android's Activity class. An Activity is simply a class that can run and do work. It can create a UI if it chooses, but it doesn't need to. As the checkbox suggests, this is optional, but an Activity is almost always used as the basis for an application.
Click to expand...
Click to collapse
Trying to get more info as to which classes are available and what they're for in general.
Guess I'll keep reading my book "Beginning Android" lol
Thanks
Just started working on an Android project. I've messed before but never anything too serious. But now its game time.
Code:
import org.appache.http.*;
now HttpClient, DefaultHttpClient, and HttpPost are all telling me I need to import their specific package.
Does android not allow me to .*; and pull in the entire lib?
Eclipse corrects it by importing the specific
Code:
import org.apache.http.impl.client.DefaultHttpClient;
That's Java 101.
"import a.b.*" only imports the classes directly within the "a.b" package, not any of the sub-packages.
Just use Eclipse's "Organize Imports". Ctrl+shift+O. It'll ask you to clarify which class you mean sometimes (when there's more than one class in the classpath with identical names), but in general It Just Works.
mstorer3772 said:
That's Java 101.
"import a.b.*" only imports the classes directly within the "a.b" package, not any of the sub-packages.
Just use Eclipse's "Organize Imports". Ctrl+shift+O. It'll ask you to clarify which class you mean sometimes (when there's more than one class in the classpath with identical names), but in general It Just Works.
Click to expand...
Click to collapse
I knew this in the back of my head but its been a while since i touched java...
Kcarpenter failed his java classes Or just walked by them without actually trying to understand it.
But I get that now. Thanks for the clarification. ctrl+shift+o has been my friend so far, but it was getting annoying.
Thanks for the answer.
Hey guys, where can I find information about how to change one of my java programs such that it runs for android (eg MouseListener ---> onTouchListener) I know the the android website has info but can someone give me a direct link as to where it is because as we all know, its a huge site.
Basically, I have a ready program that I need to change(UI's etc..) so that it can work for android.
Ok this is a sample program. Can u guys gimme an editted version so i can base my real program off of it
import java.awt.event.*;
import javax.swing.*;
public class Box extends JApplet {
public void init() {
DisplayBox display = new DisplayBox();
setContentPane(display);
}
}
class DisplayBox extends JPanel {
DisplayBox() {
setBackground(Color.red);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.green);
g.fillRect (10,130, 80, 140);
g.setColor(Color.black);
g.drawString("Box", 15 , 85);
}
}
When you say Java program, do you mean written for a desktop OS or actually written for Android? Because I'm sure that while the Android SDK uses Java, many of the APIs are different, so it may not work properly if you did change it.
But then again, I have never attempted this before, so I may be wrong
I mean a program written to run the computer yes Like any basic program such as hello world.And i am using eclipse to run an android simulator so it will run with only with the android code and not just basic java.....
android doesn't have swing.
you are going to need to create the ui using xml.
res > layout > main.xml
there are tons of tutorial on how to make ui's for android all over the web.
http://developer.android.com/guide/topics/ui/index.html
Well you are not forced to create your layout using XML you could define it via Java too but the fact remains that you have to rebuild and redesign your user-interface and probably your user-interaction mechanism too.
hi everybody!
over the course of 30 years, i've developed software on a lot of hardware platforms and operating systems. so i'm not a noob by any means to programming.
BUT, with that said, my last years of development were on the windows mobile platform. i jumped to the android about 8 months ago with a Droid X. it's been great, but the developer bug in me wants to create something for the droid...
i've spent 2-3 months reading about android development and working my way through several great development books ("Hello Android," The Pragmatic Programmers).
Do any of you have any examples of implementing a timer? i want to create a unique clock as a live wallpaper and i need to setup a 1 second timer that upon completion asynchronously informs another thread to begin processing. there is just so much development data on the web that i'm drowning in it!!!!
any help, pointers, and/or web-site references would be greatly appreciated.
thanks a lot,
marvin
Hi. I'm like you; I've been a C/C++ developer and worked with various hardware (mostly microcontrollers) for 20+ years. I've only recently gotten into java.
Here's a sample I wrote for someone a while back to demonstrate a timer. It isn't quite what you're looking for, but it should be adaptable to your situation.
It creates an activity, then uses a timer task to countdown from 10 to 0 and sends messages to a handler to updates the title of the app to reflect this.
Code:
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
public class MyTestTimerActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Timer t=new Timer();
t.scheduleAtFixedRate(mTimerTask,1,1000);
}
Handler mHandler=new Handler(){
public void handleMessage(Message msg){
switch(msg.what){
case 0:
setTitle("hello_"+msg.arg1);
break;
case 1:
setTitle("timer_"+msg.arg1);
break;
}
super.handleMessage(msg);
}
};
protected TimerTask mTimerTask=new TimerTask(){
private int count=10;
private Message msg;
@Override
public void run(){
msg=mHandler.obtainMessage(0);
msg.arg1=count;
msg.sendToTarget();
count--;
if(count==-1){
cancel();
}
}
};
Thanks Gene. i'll study what you wrote and work it in as i can.
i spent 15 years writing asynchronous driver software on various VAX/VMS computers. it was so easy to do this task (sigh).
the problem i'm having with learning java, android SDK, and all is that is seems like i'm going around in circles reading about the various ways to do this task. and then i get confused with doing it on java or via xml.....whew. reminds me of the early days of my career.
thanks again.
Hello,
I've recently taken to learning Java in an effort to create a game I have an idea for. I'm following this tutorial/guide at kilobolt . com and I've run into a little problem trying to understand something. I'm familiar with a few programming languages (C++, Python, VB) and I've been running through the Java basics just to get to grips with the syntax.
Before you start blasting me I did have a quick search. Couldn't find what I was looking for:
In regards to class inheritance and implementation, lets say I want to design a few different characters for a game that all share some generic aspects. As I learnt about interfaces first this seemed to be a simple way for new characters to acquire these shared aspects, but then I learnt about superclasses, and I'm at a bit of a loss as to which would ultimately benefit my application more as a whole. Of course I want it to run efficiently as possible, so would anyone have any hints or recommendations for me? forgive me if there's something i'm missing.
thanks in advance.
@Lacanau
You can take inheritance as follows supposingly your game chaters have some common attributes so you can have a class
GameCharecter
with broader specs which are commom to all
class GameCharecter{
.............
assign common attributes like
hasEyes
hasLegs
bla bla.........
}
now narrowing it to inheritance
Now lets say you are to have a Charecter named CR7 that will share the attributes common to other charecters like hasLegs.....
class CR7 extends GameCharecter
{
.....
extending the GameCharecter class(superclass) will assing this charecter with basic attributes common to all as declared in (super)GameCharecter
here in this class you will provide attributes specific to this charecter
like its cothing etc.
}
now coming to your app as you said you need to share attributes so that is best achieved by Inheritance aka extending Classes
as far as implementation is considered it is best suited if you want your Classes to share common behaviour
Sent from my GT-S5302 using Tapatalk 2