how to start programming android? - Android Q&A, Help & Troubleshooting

Hello guys,
I've never programmed before and I wish to learn programming android apps. I really don't know how to start, what should I learn first. I need your help on how to start, your suggestions.
Thank you very much

smartuse said:
Hello guys,
I've never programmed before and I wish to learn programming android apps. I really don't know how to start, what should I learn first. I need your help on how to start, your suggestions.
Thank you very much
Click to expand...
Click to collapse
You may want to start with learning java; See here: http://mobile.tutsplus.com/tutorials/android/java-tutorial/. You may also want to learn C/C++. Take a browse around here: http://developer.android.com/guide/basics/what-is-android.html.

smartuse said:
Hello guys,
I've never programmed before and I wish to learn programming android apps. I really don't know how to start, what should I learn first. I need your help on how to start, your suggestions.
Thank you very much
Click to expand...
Click to collapse
You'll also need the android sdk and I believe eclipse is a good bit of software.
Sent from my HTC EVO 3D X515m using XDA App

Android Application Development -o'Reilly

Don't understand!
I looked at linkes you have advised, but really don't understand some things: why used paranthesys ant other signs. I mean I understand that there is a syntax but I don't understand for example why used this type of paranthesis "()" but not this type "{}".
For example, I dont clearly understand this code
public class Cat {
private String mCatName;
Cat(String name) {
mCatName=name;
}
public String getName() {
return mCatName;
};
public void setName(String strName) {
mCatName = strName;
};
}
I can't find the explanation. I need a course from the begining. What do you recommend for me, friends?
Thank you guys!

This is the Syntax: get used to it, you can't change it anyway...
PHP:
//Make a class called Cat
public class Cat {
//set up a variable, type:String, Name:mCatName
private String mCatName;
//Constructor (method which runs by default)
Cat(String name) {
mCatName=name;
}
//Public: it can be called from everwhere (even other classes or programs)
//Method name: getName.
//It simply returns the content of the variable mCatname;
public String getName() {
return mCatName;
};
//Public: it can be called from everwhere (even other classes or programs)
//Method name: setName
//It sets the variable mCatName to the value of the input variable strName
public void setName(String strName) {
mCatName = strName;
};
}
This code is a class called Cat.
It got a String which represents the Cat's name. It is set to "name" by default.
With the methods getName and setName you can either output or change the name of the Cat.
This is very basic Java. You won't learn Java in a day, just learn bit for bit.

Thanks
But tell me please, how did you started programming? What was or from which source you started first. What were your first steps?
May be it will help me, because I don't know anything in programming.
Thank you

I started with html (not a real programing language, but helpful though)
Then we did some basics in school (Pascal, basic, .net, java) and i learned PHP and more Java in my free time.
Once you learn one programing language its rather easy to switch.
Java is a good base to start with.
There are tons of tutorials on the Internet which are great for beginners. I cant recommend you a specific, i only know German ones.

I'm going to post the book that I was taught from. I can't vouch for other books, they may be better - however the person who wrote this book was a lecturer at my university and he handed out photocopies of this book as we went through the module, while the book was still being written.
It was ammended based on feedback from a number of years of students and the parts of the language and concepts they found difficult. When I went into Uni I had no programming background beyond dabbling very lightly into HTML/PHP.
The book has exercises in it, as will any other book or online guide you find, I highly recommend attempting all of them as you progress.
http://www.amazon.co.uk/Java-Just-Time-John-Latham/dp/1848900252/ref=tmm_pap_title_0
Yes, I know it costs actual money, but for a going from complete beginner -> understand Java and its concepts well enough to begin thinking about putting it on your CV, I don't think that's such a bad thing - and he's deliberately put it at a cheap price point aimed at students, it should be 3-4x that amount to bring it in line with other university textbooks that are much less helpful.

smartuse said:
But tell me please, how did you started programming? What was or from which source you started first. What were your first steps?
May be it will help me, because I don't know anything in programming.
Thank you
Click to expand...
Click to collapse
I used a book called Object First with Java, BlueJ...
BlueJ is the IDE (where you type the code in to make programs) and its free.
Java is the language, we could talk you through the code above, but its better you learn it yourself, and then you will be able to read the code above.
Look into what Object Oriented Languages are, but tbh, that Book can be found online, its not cheap but I found it rather good!
http://www.amazon.co.uk/Objects-Fir...5628/ref=sr_1_1?ie=UTF8&qid=1325806069&sr=8-1
An amazon link to the book, hope this helps!

Thanks
Thank you, guys!
I'll try to find more information about Java online. Thank you all very much!

Also, look on you tube for "the new Boston" he makes some good programming videos

Which is difference between SDK and Oracle ?
I want to know which is difference between SDK and Oracle ?
Please help me I am new here and want to start programming.

Related

[Q] Visusal Kitchen for Android?

Just curious, but does anyone know if there's an android kitchen in the works that will be more like the EvirisVisual Kitchen for WM? I, as well as several others I'm sure, would have a little less of a learning curve if there was an android kitchen that was set up the same way as the WM kitchens. Just a thought... What does everyone else think?
PMDColeslaw said:
Just curious, but does anyone know if there's an android kitchen in the works that will be more like the EvirisVisual Kitchen for WM? I, as well as several others I'm sure, would have a little less of a learning curve if there was an android kitchen that was set up the same way as the WM kitchens. Just a thought... What does everyone else think?
Click to expand...
Click to collapse
What is there to learn though? I thought Visual Kitchen involved a learning curve.
The Bepe Platform Rebuilder (which the VK was based off of) was command-line driven and simple.
dsixda said:
What is there to learn though? I thought Visual Kitchen involved a learning curve.
The Bepe Platform Rebuilder (which the VK was based off of) was command-line driven and simple.
Click to expand...
Click to collapse
True, but what I liked about the VK was that it ran the command line stuff in the background based on options the user set on the front end UI. It was a big help to those who aren't code/script guru's by letting us set up our roms in a visual UI.
EDIT:Your Kitchen is pretty user friendly BTW. I just miss the point and click UI and not having to edit so many xml files.
PMDColeslaw said:
True, but what I liked about the VK was that it ran the command line stuff in the background based on options the user set on the front end UI. It was a big help to those who aren't code/script guru's by letting us set up our roms in a visual UI.
EDIT:Your Kitchen is pretty user friendly BTW. I just miss the point and click UI and not having to edit so many xml files.
Click to expand...
Click to collapse
I'm pretty sure a nice UI-based kitchen can be done in Windows, except the person who does it will soon realize there will be platform incompatibilities over Windows XP/7 and 32-bit and 64-bit.. but those can be fixed easily if they include all possible working binaries for all the platforms.
dsixda said:
I'm pretty sure a nice UI-based kitchen can be done in Windows, except the person who does it will soon realize there will be platform incompatibilities over Windows XP/7 and 32-bit and 64-bit.. but those can be fixed easily if they include all possible working binaries for all the platforms.
Click to expand...
Click to collapse
Not necessarily. If the UI is coded in a multi-platform language (Qt, C#-Mono, Python, even PHP) then it is possible to port to any platform. Also, if all the command line stuff from the BAT files are placed into the UI too, then only one executable (with the resources of course) and it is done.
dsixda said:
I'm pretty sure a nice UI-based kitchen can be done in Windows, except the person who does it will soon realize there will be platform incompatibilities over Windows XP/7 and 32-bit and 64-bit.. but those can be fixed easily if they include all possible working binaries for all the platforms.
Click to expand...
Click to collapse
fonix232 said:
Not necessarily. If the UI is coded in a multi-platform language (Qt, C#-Mono, Python, even PHP) then it is possible to port to any platform. Also, if all the command line stuff from the BAT files are placed into the UI too, then only one executable (with the resources of course) and it is done.
Click to expand...
Click to collapse
I would very much like that if someone could do it. I know a lot of people have Linux running on a virtual machine or whatever but I actually like Windows 7. Maybe its laziness but I'd really like to not have to learn an entirely new OS before I can continue building my own ROMs. Again just my opinion. I noticed Fresh's kitchen appears to have a nice UI but it's still not released and I have no idea if it will be completely Windows ran or not.
PMDColeslaw said:
I would very much like that if someone could do it. I know a lot of people have Linux running on a virtual machine or whatever but I actually like Windows 7. Maybe its laziness but I'd really like to not have to learn an entirely new OS before I can continue building my own ROMs. Again just my opinion. I noticed Fresh's kitchen appears to have a nice UI but it's still not released and I have no idea if it will be completely Windows ran or not.
Click to expand...
Click to collapse
You don't need a virtual machine, just use Cygwin in Windows
fonix232 said:
Not necessarily. If the UI is coded in a multi-platform language (Qt, C#-Mono, Python, even PHP) then it is possible to port to any platform. Also, if all the command line stuff from the BAT files are placed into the UI too, then only one executable (with the resources of course) and it is done.
Click to expand...
Click to collapse
The problem is, there are certain functions that cannot be handled with one language alone. So, we can have a GUI-based application, like one using Tk on top of TCL (just an example!), but we'd still need to run it from Cygwin/Linux etc. so that other commands like perl, sed, od etc. will be able to work. Or maybe there is a better solution that I am not aware of.
dsixda said:
The problem is, there are certain functions that cannot be handled with one language alone. So, we can have a GUI-based application, like one using Tk on top of TCL (just an example!), but we'd still need to run it from Cygwin/Linux etc. so that other commands like perl, sed, od etc. will be able to work. Or maybe there is a better solution that I am not aware of.
Click to expand...
Click to collapse
How about a Java front-end for the existing Kitchen? The thought has occurred to me, but my focus is already spread amongst several different projects.
Something like this code from jcase(?) might be a good start?
String txt = ((EditText) findViewById(R.id.txtCmd)).getText().toString(); // cmd line
boolean su = ((CheckBox) findViewById(R.id.asRoot)).isChecked(); // run as root
Process p;
try {
if (su) {
p = Runtime.getRuntime().exec("su");
(new DataOutputStream(p.getOutputStream())).writeBytes(txt + "; exit\n"); // you want the shell to terminate itself so you know the command has finished
} else
p = Runtime.getRuntime().exec(txt);
} catch (Exception e) {
e.printStackTrace();
return;
}
try {
StringBuilder sb = new StringBuilder();
InputStreamReader isr = new InputStreamReader(p.getInputStream());
int r;
while ((r = isr.read()) > 0)
sb.append((char) r); // you may want to use a buffer
msg = sb.toString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}​
I haven't done Java development in 10 years But I'll definitely put this in my "TO DO" list for future work. Thanks
gnarlyc said:
How about a Java front-end for the existing Kitchen? The thought has occurred to me, but my focus is already spread amongst several different projects.
Something like this code from jcase(?) might be a good start?
String txt = ((EditText) findViewById(R.id.txtCmd)).getText().toString(); // cmd line
boolean su = ((CheckBox) findViewById(R.id.asRoot)).isChecked(); // run as root
Process p;
try {
if (su) {
p = Runtime.getRuntime().exec("su");
(new DataOutputStream(p.getOutputStream())).writeBytes(txt + "; exit\n"); // you want the shell to terminate itself so you know the command has finished
} else
p = Runtime.getRuntime().exec(txt);
} catch (Exception e) {
e.printStackTrace();
return;
}
try {
StringBuilder sb = new StringBuilder();
InputStreamReader isr = new InputStreamReader(p.getInputStream());
int r;
while ((r = isr.read()) > 0)
sb.append((char) r); // you may want to use a buffer
msg = sb.toString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}​
Click to expand...
Click to collapse
dsixda said:
I haven't done Java development in 10 years But I'll definitely put this in my "TO DO" list for future work. Thanks
Click to expand...
Click to collapse
Dude. I don't know what I was smoking when I posted that. That was code that you would run on an Android phone! Should I edited it out so future generations don't think I'm a total goob?
Anyway, you get the idea. If I can get a few things cleaned up, I'll try to do it myself or get you started or help or at least cheer you on.
gnarlyc said:
How about a Java front-end for the existing Kitchen? The thought has occurred to me, but my focus is already spread amongst several different projects.
Something like this code from jcase(?) might be a good start?
String txt = ((EditText) findViewById(R.id.txtCmd)).getText().toString(); // cmd line
boolean su = ((CheckBox) findViewById(R.id.asRoot)).isChecked(); // run as root
Process p;
try {
if (su) {
p = Runtime.getRuntime().exec("su");
(new DataOutputStream(p.getOutputStream())).writeBytes(txt + "; exit\n"); // you want the shell to terminate itself so you know the command has finished
} else
p = Runtime.getRuntime().exec(txt);
} catch (Exception e) {
e.printStackTrace();
return;
}
try {
StringBuilder sb = new StringBuilder();
InputStreamReader isr = new InputStreamReader(p.getInputStream());
int r;
while ((r = isr.read()) > 0)
sb.append((char) r); // you may want to use a buffer
msg = sb.toString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}​
Click to expand...
Click to collapse
Are you SERIOUS??? After the Oracle vs. Google incident, you are suggesting we use Java??
I appreciate the help but its way easier coding for 32bit Windows since any 64bit Windows will run 32bit and use .Net for kitchen GUI.
If Linux fan boys want some love, they can use WINE
Then again, my coding skills SUCK and I wish someone would create some type of Visual kitchen
DJGonzo said:
Are you SERIOUS??? After the Oracle vs. Google incident, you are suggesting we use Java??
I appreciate the help but its way easier coding for 32bit Windows since any 64bit Windows will run 32bit and use .Net for kitchen GUI.
If Linux fan boys want some love, they can use WINE
Then again, my coding skills SUCK and I wish someone would create some type of Visual kitchen
Click to expand...
Click to collapse
Most of the apps on your phone are in Java. If something happens and we are unable to use Java anymore, I don't think people will really care about a GUI front-end to an already working and great kitchen. So, yes, I AM SERIOUS!
Creating a GUI front-end instead of a whole new kitchen should be relatively easy in most languages, depending on how fancy you want to get. (A window with buttons that launch scripts, more or less... That's simplifying it somewhat, but you get the idea.)
I'm probably making myself look like an ass which I am in the sense that I have no coding skills but isn't Fresh's Kitchen a visual kitchen or am I not understanding the concept.
The only reason I'm asking is, even though I have no knowledge of taking something like this on it seems to me that if it's already been done it should be a little easier to figure out.
Again, I might just be an ass and if so I apologize.
DJGonzo said:
Are you SERIOUS??? After the Oracle vs. Google incident, you are suggesting we use Java??
I appreciate the help but its way easier coding for 32bit Windows since any 64bit Windows will run 32bit and use .Net for kitchen GUI.
If Linux fan boys want some love, they can use WINE
Then again, my coding skills SUCK and I wish someone would create some type of Visual kitchen
Click to expand...
Click to collapse
Java is still platform independent, but has some really annoying requirements, and would run slow on big files.
.Net: We could use Mono for that. Mono libraries are to included in Windows .Net (or was to be. I heard somewhere that Mono Project will ask Windows to include their libraries too so any .Net app can run on Linux, Windows, and Mac, with the same user experience), and also installing it is easy. Yes, it is harder to develop under Mono, harder than Visual Studio, but still possible.
But .Net has some serious restrictions too. Can not use JAVA apps (only with launching a command line, so no direct inclusion), thus SignAPK is out (until you want to see some ugly black window, or use a backside process). And also it would need a lot of things to do, like update itself frequently, so someone needs to maintain a mod/firmware/rom/app/kernel/recovery server, and so much more.
It is simplyer to make a nice PHP site what does the same, write a UI for it, and upload to the site itself. Thus people can always access the latest stuff, and also, save their work (as a config file) for further modding. Oh and do not forget, the server would produce the file a lot faster than a PC
fonix232 said:
Java is still platform independent, but has some really annoying requirements, and would run slow on big files.
.Net: We could use Mono for that. Mono libraries are to included in Windows .Net (or was to be. I heard somewhere that Mono Project will ask Windows to include their libraries too so any .Net app can run on Linux, Windows, and Mac, with the same user experience), and also installing it is easy. Yes, it is harder to develop under Mono, harder than Visual Studio, but still possible.
But .Net has some serious restrictions too. Can not use JAVA apps (only with launching a command line, so no direct inclusion), thus SignAPK is out (until you want to see some ugly black window, or use a backside process). And also it would need a lot of things to do, like update itself frequently, so someone needs to maintain a mod/firmware/rom/app/kernel/recovery server, and so much more.
It is simplyer to make a nice PHP site what does the same, write a UI for it, and upload to the site itself. Thus people can always access the latest stuff, and also, save their work (as a config file) for further modding. Oh and do not forget, the server would produce the file a lot faster than a PC
Click to expand...
Click to collapse
I suggested Java for several reasons:
1) as you say, it's multiplatform - That fits really nicely with the kitchen.
2) The kitchen already uses Java, so the requirements have already been met.
3) I'm not a programmer. I say that, but I've taken three college programming courses now. Two of them were Java. So, I know Java well enough.
4) I was suggesting a front-end GUI to the existing kitchen, not something entirely new.
It basically boils down to whoever gets motivated enough to volunteer their time. If they choose Java, then it's Java. If they choose Motorola 68k assembly, then that's what it is. Who knows, dsixda may make something that ties in with his kitchen, and someone else may make something similar in their language of choice. That would be great. More choices are generally a good thing up to a point.
gnarlyc said:
Most of the apps on your phone are in Java. If something happens and we are unable to use Java anymore, I don't think people will really care about a GUI front-end to an already working and great kitchen. So, yes, I AM SERIOUS!
Click to expand...
Click to collapse
Android does NOT run Java. It runs Dalvik bytecode.
Whether the language is "very similar" p) or not is a different thing.
Google just made made a compiler that can compiler Java code, but its not Java
LOL Im just teasing.
I DO like the PHP kitchen idea. They do it for the HTC Desire and that would be a great project.
aerajan said:
I'm probably making myself look like an ass which I am in the sense that I have no coding skills but isn't Fresh's Kitchen a visual kitchen or am I not understanding the concept.
The only reason I'm asking is, even though I have no knowledge of taking something like this on it seems to me that if it's already been done it should be a little easier to figure out.
Again, I might just be an ass and if so I apologize.
Click to expand...
Click to collapse
No you're right, Fresh's Kitchen is along the lines (visually) of what I am used to coming from WM and the VK. It just doesnt seem to get updated very often so far. If I read correctly it still only works with 1.5 roms which is great... If you like 1.5, which virtually no one does, lol.
How an AIR front end combined with a PHP backend?
DJGonzo said:
Android does NOT run Java. It runs Dalvik bytecode.
Whether the language is "very similar" p) or not is a different thing.
Google just made made a compiler that can compiler Java code, but its not Java
LOL Im just teasing.
I DO like the PHP kitchen idea. They do it for the HTC Desire and that would be a great project.
Click to expand...
Click to collapse
The problem I have with an online PHP kitchen is that it goes away from the idea of giving the user full control of their files and helping them learn that way too. In addition it may be inconvenient when you want to move files around.

Programming question.

Hi I not sure if I'm posting this in the correct section.
Just have a quick question.
In my java android application code I have created and instance of MapView class like this:
MapView mapView = (MapView) findViewById(R.id.mapview);
What does the (MapView) do compared when you normally create an instance of a class like this:
MapView mapView = new MapView(parameters);
Thanks.
The second one is calling constructor with parameter.I am not sure ,but the first one seems like member function calling with casting .
HTC Sense 3.5, Android 2.3.5
Thanks for answering! I tried searching the web for an explanation but not easy to find.
Cheers!
Well... the first one won'k work unless you have a layout which contains a MapView called "mapview".
So first of all you should build your xml layout file.
You can use MapView mapView = new MapView(parameters) and do it all programmatically.. which is definitely harder.
Btw the "Android software development" section would be more appropriate for this question.
Just to elaborate a bit more...in Java, most of the data you work with are types of Objects. The blueprint for the type of Object is called a class. When developing a new Class, you could define this new type of Object from scratch or you could Extend an existing one.
For example, when they whipped up the MapView class, they probably decided that "you know what...MapView's are sort of like other Views (they have gravity, padding, an id, perhaps a position, a border, etc)" and decided to define a skeleton class called View which maps out the aspects that are common to all Views. They likely then made MapView extend View, overriding or adding any aspects that make MapView unique.
So, where am I going with this...back to your code snippet, there's a function called getViewById(id) that--if you look at its definition--is set to return a View. If you just tried to use the result of this function like a MapView, you would run into problems because the Java compiler would have no way of knowing under which circumstances (ie. with which ID) the returned view would actually be a MapView. In these situations where you want to tell the compiler that you want to treat an object like some other type of object, you need to cast the reference to the object. You do this by putting the type you want in parentheses and to the left of the reference you want to cast.
For example, if you made a variable of type View (View mv) and are using it to refer to something you known is a MapView ( View mv = new MapView() ), to treat it like a MapView you need to cast it ( (MapView mv).aMethodUniqueToMapView() ). Keep in mind that unless there's no way an reference to one type could contain an object of another, the compiler will allow the cast. This means that at run time there's a chance you'll get an Exception (a ClassCastException). In certain situations, this is something you'll need to handle but in this case, you're usually good to go as you know for certain the ids you assigned to your views.
Hope this didn't put you to sleep
- chris
Thank you both for answering! =)
Chris or ctttt. Your explanation did not but me to sleep, you explained just what I was asking about! I have done java for only 1 year, and just started programming for my phone, so when I saw the (MapView) casting when you created an instance of a class i just wanted to know how that worked since I haven't seen it being done before =).
Thanks so much for clearing that up =)
Cheers
Questions or Problems Should Not Be Posted in the Development Forum
Please Post in the Correct Forums
Moving to Q&A

[Q] Java/android not understanding oop

Hi guys,
Im really trying to learn java but think Im getting very confused. I have a class which holds names and information, In my android program I have the user enter a load of names which then are passed as an array to my next activity. Whenever I try to create objects from each name in the array it wont do it? Ialso understand that this may be a bad way of doing this as names will change so how would I reference them. Maybe using the array[pos] to create objects... so instead of name.printweight(); I could use 1.printweight();
I really hope above is readable or understandable as to what I am trying to learn
SUDO:
Take in names of competitors
Pass array of competitors to next activity
for loop to get each competitor name entered to a string
create an object using the competitors name (e.g name.birthday)
class competitor:
dob
weight
highest score
time
I think you cant use a string name or an int to create objects but how else would I create several competitors in my class?
Im reading some books I bought and I really am struggling to make sense of all this. Please be patient with me...
Kind regards
Phil
Think I got it.....
for (int i = 0; i < numcompetitors; i++)
{
String competitorName = (String) competitorsArray;
TextView txtOutput = (TextView) findViewById(R.id.edttxtOutput);
txtOutput.append(competitorName + " ");
//create an object of this competitor
competitorsArray = new Competitor();
((Competitor) competitorsArray).average();
}
I couldn't really understand what you are having a problem with. The code you posted might be working but it really doesn't seem like a good way of doing what you are trying to do.
I'd like to help you out but first can you please explain a bit more clearly? Maybe even post your code, so that its easier for me to understand.
So... if I understood you, you need something like this:
class competitor:
dob
weight
highest score
time
Names entered:
A,B,C
Result:
A.dob, A.weight, A.highestscore, A.time
B.dob, B.weight, B.highestscore, B.time
C.dob, C.weight, C.highestscore, C.time
Right? Well, it can't be achieved using Java, it doesn't allow you to assign programatically names to variables. However, you can use a little trick based on an array:
Code:
//Remember: you have a class named "competitor" containing several variables
String[] Names= {"Uno","Dos","Tres"}; //the array passed by the other activity
competitor[] playerData=new competitor[Names.length]; //an array, to store copies of the competitor class (a copy for each name entered)
for(byte i=0;i<Names.length;i++){
playerData[i]=new competitor(); //Initializing the copy...
playerData[i].dob= //Storing data for each name entered...
playerData[i].weight=
playerData[i].highestscore=
playerData[i].time=
}
And that's all... 1st player's data will be stored in the position 0 of the array, 2nd player's data in the position 1... and so on.
BTW, instead use 2 activities, is more easy to use only an activity, changing its layout when you need a GUI's change (yep, it's possible ).

Confused about how to evolve from (very) basic Android Development

Hello.
I followed all the New Boston Android videos, did everything, understood everything.
I tried to create a basic RSS feed reader, in order to better incorporate some concepts I learned in the New Boston videos (http processing, xml processing, adapting the content to lists, custom lists, etc). When I got to pass the information from the http processed data to xml parser and the list, that's when I got too much confused and knew I didn't have enough knowledge.
Then I tried to do some "Shopping List Manager" (just like OI Shopping, a bit adapted more to my taste), in order to learn.
However, again, when I neededto pass information to other objects in other classes or something like that, I get confused and don't know what to do.
So I bought CommonsWare book, "The Busy Coder's Guide to Android", which I have the latest version (5.1) and I'm reading, but I don't like to advance when I don't fully understand something. This time I'm stuck on the Action Bar part, more precisely this one:
Code:
private void configureActionItem(Menu menu) {
EditText add=
(EditText)menu.findItem(R.id.add).getActionView()
.findViewById(R.id.title);
add.setOnEditorActionListener(this);
}
I know this will seem very basic to many of you, but I get really confused on all this calls, returning results and more calls.
I don't have a background on OOP, except when I worked with PHP frameworks like Symfony, I work usually with direct task programming (scripting, automation, etc), as I am a Linux System Administrator, so my code is mainly scripting and web interface building (Python, Shell Script, PHP, Javascript, etc).
Can anybody explain what can I do to better understand this? It's just lack of practice and in time I'll understand better? Is it OOP lack of knowledge/practice? Or is it Java lack of knowledge/practice?
Thanks a lot for all your help.
Maybe the best approach is to get some face time with a person who is more experienced and have him explain to you the concept you have trouble with while focusing on the parts you don't grasp. A real human has this flexibility to do a "targeted strike" unlike a tutorial or a book that has no idea where in particular the student may get confused.
For this particular issue, the issue can be summarized as follows. Let's say you have an object call a function:
Code:
orange.peel();
This should be relatively straightforward. The next level of complexity is the fact that obj is just a variable representing an object, and in fact we can substitute anything else that evaluates to an object (i.e.: after it runs, you end up with an object). For example these all are legal ways to call the method as long as types match:
Code:
(new Orange()).peel();
(shouldEatSmallerOrange ? smallerOrange : largerOrange).peel();
retrieveOrangeFromBox().peel();
The last line illustrates calling some other function that returns the object, which is then used to call a second function. The final step from here is to recognize that instead of a single retrieveOrangeFromBox() we can have a chain of functions, each of which returns an object that is used to call the next function in line. For example:
Code:
findCar().accessCarTrunk().unloadBoxFromTrunk().retrieveOrangeFromBox().peel();
The names are unnecessarily verbose to illustrate how functions and their results relate to each other.
OOP + Android system
You're not that clear as to exactly what you are having a problem with, but in general, it sounds like you need to get a java book and learn the basic concepts of classes and interfaces. Since you say you have a background in PHP you could probably go pretty far just by following the Java tutorials on the Sun website. I say java because that's the target language here, any book on OOP in any language would be adequate but learning java would give you the added ability to read other people's android code examples more easily.
After that, you can learn the Android framework. You develop in the Java language but you work within the android framework. What that means is that here, for example, the action bar is provided to you by the android system, and this callback is called by the system, so it is all set up for you. But to understand what is happening, you need to understand when the system calls this method and what it does. That is the framework.
So more specifically, how can you understand this code? This method is called from another method, onCreateOptionsMenu(). OnCreateOptionsMenu() is a method in the Activity class that is called automatically by the system at a specific time. You need to read about the Activity class and the Activity lifecycle on the android developers site. If you want your activity to provide an options menu, you create it in OnCreateOptionsMenu and return it, the system will handle it from there. So back to configureActionItem(Menu menu), here you are passing in the menu object, which contains MenuItem objects, which the system uses to populate the menu (either on the action bar, or when you hit the menu button, depending on the android version). Each MenuItem object has a view that is associated with it (usually created in an XML file).
One thing that may be hard to understand is that all these calls are chained, so if you don't know what they are returning you don't know where to look for help. It's easier if you separate the calls out. Here, the documentation is your friend. If you look at the Menu class on the android dev site, you see that findItem() returns a MenuItem. So then you look up MenuItem, and you see that getActionView() returns a View. Look at the View class, and you can see findViewById() returns another view (a sub-view that is contained within this view). so when you look at it all together, unchained:
Code:
private void configureActionItem(Menu menu) {
MenuItem mi = menu.findItem(R.id.add);
View parentView = mi.getActionView();
EditText add = (EditText)parentView.findViewById(R.id.title);
}
findViewById returns a View, but you know that the view known by the id R.id.title is an EditText view, and you want to use it as an EditText, so you have to cast the View to an EditText (which is a subclass of View) so that the compiler knows that it is an EditText type of view. That's what the (EditText) is doing in front of the findViewById call. To understand that you need to read about subclassing and strongly-typed programming languages. PHP is weakly-typed, so that might be new to you.
finally, you call setOnEditActionListener on the EditText. OnEditActionListener is an interface that you have implemented in this class. An interface defines a common set of methods that are guaranteed to be present in whichever class has implemented it. So when you set the OnEditActionListener to this, (this means the current instance of this class), the EditText will hold on to the "this" object and it knows that it can call a certain set of methods on it. What are those methods? look up the OnEditActionListener interface in the docs:
it only has one method,
Code:
public abstract boolean onEditorAction (TextView v, int actionId, KeyEvent event);
so somewhere in this class, you will have this method defined and this is where you put code that you want to run when the EditText triggers this action. I assume this get called when the user touches the EditText.
It's really not going to be easy to work with android if you don't have a basic knowledge of OOP, specifically classes, inheritance, and interfaces. Also, knowing how java implements these concepts will help a lot. Then you can use your book to learn the Android framework.
GreenTuxer said:
Hello.
I followed all the New Boston Android videos, did everything, understood everything.
I tried to create a basic RSS feed reader, in order to better incorporate some concepts I learned in the New Boston videos (http processing, xml processing, adapting the content to lists, custom lists, etc). When I got to pass the information from the http processed data to xml parser and the list, that's when I got too much confused and knew I didn't have enough knowledge.
Then I tried to do some "Shopping List Manager" (just like OI Shopping, a bit adapted more to my taste), in order to learn.
However, again, when I neededto pass information to other objects in other classes or something like that, I get confused and don't know what to do.
So I bought CommonsWare book, "The Busy Coder's Guide to Android", which I have the latest version (5.1) and I'm reading, but I don't like to advance when I don't fully understand something. This time I'm stuck on the Action Bar part, more precisely this one:
Code:
private void configureActionItem(Menu menu) {
EditText add=
(EditText)menu.findItem(R.id.add).getActionView()
.findViewById(R.id.title);
add.setOnEditorActionListener(this);
}
I know this will seem very basic to many of you, but I get really confused on all this calls, returning results and more calls.
I don't have a background on OOP, except when I worked with PHP frameworks like Symfony, I work usually with direct task programming (scripting, automation, etc), as I am a Linux System Administrator, so my code is mainly scripting and web interface building (Python, Shell Script, PHP, Javascript, etc).
Can anybody explain what can I do to better understand this? It's just lack of practice and in time I'll understand better? Is it OOP lack of knowledge/practice? Or is it Java lack of knowledge/practice?
Thanks a lot for all your help.
Click to expand...
Click to collapse
Thanks a lot for your help. I also think my issue is with OOP, but I needed the opinion of people with more knowledge.
I understand very well what you said about onCreateOptionsMenu(), why and when is called, Activity class, lifecycle, etc.
Those things I understand without any problem. I also understand the basics of OOP, but I don't know almost nothing about Interfaces and I don't have almost any experience with inheritance, although I understand it.
I think I'm just confused because I haven't worked very long with OOP. I just don't know if I should invest in something like reading and testing something like Thinking in Java, or just practice more and more Android Development.

[GUIDE] Programming languages alternatives for Android

Note to mods: this thread might be worth pinning! Using modern languages could highly improve code quality of Android apps.
OK, if you want native code, you can stick to C or C++, as you probably know interfacing native code with Java via JNI is neither pretty nor easy. But what about languages that compile to JVM (Dalvik/ART in Android case) that allow us to use tens of thousands of available java libraries in our projects? Java is quite nice but it's missing a lot of hot concepts available in modern languages like functional programming, duck typing and pattern matching. And as soon as you start using other JVM languages you'll discover Java is also extremely... verbose! I could use 5 Scala lines of code to do the same things I did in 50 lines of Java...
Note also that almost all of below languages handle null in a way much more civilized than Java. While in Java you would do:
Code:
if(someValue != null && someValue.someProperty!=null && someValue.someProperty.interestingString != null) doSomething(someValue.someProperty.interestingText)
In modern language you would just do:
Code:
doSomething(someValue?.someProperty?.interestingString)
Nice, eh?
And do believe me, these languages mix wonderfuly with Android API. Once you start coding in them, you won't come back to Java. Note that all of them have Android Studio plugins, that make the coding experience as easy as clicking "Install Plugin"!
So, let's start with the king:
Scala
Website: link
Getting started: link
Scala for Java developers: link
Why was it invented: Technically, Scala is a blend of object-oriented and functional programming concepts in a statically typed language. The fusion of object-oriented and functional programming shows up in many different aspects of Scala; it is probably more pervasive than in any other widely used language. The two programming styles have complementary strengths when it comes to scalability. Scala’s functional programming constructs make it easy to build interesting things quickly from simple parts. Its object-oriented constructs make it easy to structure larger systems and to adapt them to new demands. The combination of both styles in Scala makes it possible to express new kinds of programming patterns and component abstractions. It also leads to a legible and concise programming style.
A real beast of a language, I'm reading a fifth book on Scala right now, I have read tens of blog posts and I have a feeling like I haven't even scratched surface of it, although I've already incorporated Scala code into my XenoAmp project. If you're familiar with functional programming, you'll feel at home with Scala.
Let me rephrase the above paragraph: you PAINLESSLY incorporate below languages into your current project, without converting a single existing line! You just create new file that has extension different than .java!
Things like function literals, tuples, pattern matching and surprisingly flexible syntax of the language allow to create... a language within language, note very nice Scala/Android UI library named Scaloid and how it maintains its own minimal animation-only sub-language called Snails!
Scaloid activity layout example
Also read a blog post here about how can you write a clean code like this (note: NO SEMICOLONS!!! ):
Code:
Scala
findView[Button](R.id.button).onClick { view : View =>
Toast.makeText(this, "You have clicked the button", Toast.LENGTH_LONG).show()
}
instead of this:
Code:
Java
Button v= (Button) findViewById(R.id.button);
v.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(this, "You have clicked the button", Toast.LENGTH_LONG).show();
}
});
Kotlin
Website: link
Getting started: link
Kotlin for Java developers: link
Why was it invented:
to create a Java-compatible language,
that compiles at least as fast as Java,
make it safer than Java, i.e. statically check for common pitfalls such as null pointer dereference,
make it more concise than Java by supporting variable type inference, higher-order functions (closures), extension functions, mixins and first-class delegation, etc;
and, keeping the useful level of expressiveness (see above), make it way simpler than the most mature competitor – Scala.
A language created by creators of IntelliJ Idea (base of AndroidStudio), with Android in mind. It's also functional, syntax based on Scala syntax, but much simplier and easier to learn. Also has a tool to convert your Java code to Kotlin! So if you want to start using all hip goodies like functional programmin, patter matching, nullable expressions and operators - this is a place to start without feeling overwhelmed (by Scala)
Read what Jake Wharton (the one behind NineOldAndroids and ActionBarSherlock) thinks about Kotlin: https://docs.google.com/document/d/...bEhCqTt29hG8P44aA9W0DM8/edit?hl=pl&forcehl=1#
Ceylon
Website: link
Getting started: couldn't find a good primer, but you could figure it yourself
Ceylon for Java developers: link
Why was it invented:
to be easy to learn and understand for Java and C# developers,
to eliminate some of Java’s verbosity, while retaining its readability,
to improve upon Java's typesafety,
to provide a declarative syntax for expressing hierarchical information like user interface definition, externalized data, and system configuration, thereby eliminating Java's dependence upon XML,
to support and encourage a more “functional” style of programming with immutable objects and higher-order functions,
to provide great support for meta-programming, thus making it easier to write frameworks, and
to provide built-in modularity.
Supposedly more powerful than Kotlin, with better syntax for lambdas (function literals).
I might write a bit more about each language as soon as I start using them, so stay tuned. And share your experience with alternatives in this thread!
Gosu
Website: http://gosu-lang.github.io
Getting started: http://gosu-lang.github.io/docs.html
Its most notable feature is its Open Type System API, which allows the language to be easily extended to provide compile-time checking for things that would typically be dynamically checked at runtime in many other languages. [Wikipedia] Doesn't seem to have functional programming capabilities.
Groovy
to be continued

Categories

Resources