Hello to everyone.
I am new comer in the world of Android App Development.
I am completely new in android app development.
I want someone to teach me some basics so that I can learn that and them implement my own imagination to build a desire app.
Firstly, I have installed the adt_bundel which include SDK and eclipse.
When I first started using android, I learned that it is open source that means we can easily free the source code of it.
when I started using apps and games I always thought to make an android app for me.
So for that reason I m posting this.
I want to create a very simple app in which there are 3 buttons and when click on that button the background should change.
Eg: There are 3 button red,blue,green. so if I press the red button the background should change to red color and vise versa.
So can anyone please help me in building my very 1st own Android App.
What about video tutorials on youtube? For me the tutorials from 'thenewboston' helped a lot
tschmid said:
What about video tutorials on youtube? For me the tutorials from 'thenewboston' helped a lot
Click to expand...
Click to collapse
I tried mate and I got many tutorials on google with code but I didnt got clear idea. Mostly the example they all use in the tutorial is making calculator in which there are 2 text input field and 1 button. by pressing that both number will add.
I don't want that.
I have created many app in VB.Net in my college. Infact created a mini notepad and mini calculator.
Actually I have created the app in vb.net in which if we press the desire button it will change color. So I thought why can't I make for the Android too.
It's easy
Set OnClickListener for ur buttons to listen for click events.
e.g For changing color to red
red.setOnClickListener(new View.OnClickListener({
// use the reference to ur background layout and set the color
urBackgroundLayout.setBackgroundColor(Color.Red);
}));
Do the same for the other two buttons.
I hope this was helpful!
P.S : braces in the code snippet might b incorrect.
red.setOnClickListener(new View.OnClickListener{
public void onClick(View v) {
urBackgroundLayout.setBackgroundColor(Color.Red);
}
});
Yeah the braces are indeed not quite correct. Also i think you must put the onClick method inside the listener.
OkieKokie said:
Set OnClickListener for ur buttons to listen for click events.
e.g For changing color to red
red.setOnClickListener(new View.OnClickListener({
// use the reference to ur background layout and set the color
urBackgroundLayout.setBackgroundColor(Color.Red);
}));
Do the same for the other two buttons.
I hope this was helpful!
P.S : braces in the code snippet might b incorrect.
Click to expand...
Click to collapse
Thanks bro for replying.
Actually I a complete noob here.
I have created a graphical layout of my app with 3 bottons red blue and green.
Now the noob question is where I have to do the coding.
I have seen in many tutorial that the coding is done under src->package->.java file.
please correct me if I am wrong....
Masrepus said:
red.setOnClickListener(new View.OnClickListener{
public void onClick(View v) {
urBackgroundLayout.setBackgroundColor(Color.Red);
}
});
Yeah the braces are indeed not quite correct. Also i think you must put the onClick method inside the listener.
Click to expand...
Click to collapse
The same question goes to you bro.
Can you please explain in detail
what are the things I need to do.
Thanks
Mustakim_456 said:
The same question goes to you bro.
Can you please explain in detail
what are the things I need to do.
Thanks
Click to expand...
Click to collapse
Start a new Android app project in eclipse and it will generate a MainActivity.java file for you, do the coding there.
Also u will need to add views to ur graphical layout. After creating a new project go to the 'res' folder, then go to 'layouts' and open ur activity_main.xml. Add views to this file by dragging from the left pane onto the view.
Inside the MainActivity.java you will find a method called onCreate()
Inside it you put the code we gave you. This method is called when the app is started up, so all the initialising etc like setting button onClickListeners you have to do, is normally being put inside this onCreate()
You will also have to create a layout.xml for your app in which you put your buttons and one background layout, for example a linear layout, which will later change its colour.
All about how to make such a layout can be found in every video tutorial series you will find. I could recommend the mybringback youtube channel.
Now inside the code you got for the button, the button is being referred to as 'red' (see red.setOnClickListener(...) )
So now to be able to do that, before that you will have to declare that specific button:
Button red = (Button) findViewById(R.id.yourButtonId);
Click to expand...
Click to collapse
Same thing goes for the background layout, i will give you the example for a LinearLayout:
LinearLayout urBackgroundLayout = (LinearLayout) findViewById(R.id.yourLayoutId);
Click to expand...
Click to collapse
I called it urBackgroundLayout because in the code posted above it is being referred to as this. You can change it to whatever name you desire.
Now android knows which button the button 'red' actually is, when you replace the 'yourButtonId' with the id of that specific button. (Read about view ids in android on google or inside a tutorial video)
Same thing for the linear layout
Now after declaring all of your buttons that way, maybe Button green and Button blue,... You just copy/paste the code above for each button you have, e.g. 'blue.setOnClickListener' and 'green.setOnClickListener' and change the 'Color.Red' to each desired color, voila you are done!
Masrepus said:
Inside the MainActivity.java you will find a method called onCreate()
Inside it you put the code we gave you. This method is called when the app is started up, so all the initialising etc like setting button onClickListeners you have to do, is normally being put inside this onCreate()
You will also have to create a layout.xml for your app in which you put your buttons and one background layout, for example a linear layout, which will later change its colour.
All about how to make such a layout can be found in every video tutorial series you will find. I could recommend the mybringback youtube channel.
Now inside the code you got for the button, the button is being referred to as 'red' (see red.setOnClickListener(...) )
So now to be able to do that, before that you will have to declare that specific button:
Same thing goes for the background layout, i will give you the example for a LinearLayout:
I called it urBackgroundLayout because in the code posted above it is being referred to as this. You can change it to whatever name you desire.
Now android knows which button the button 'red' actually is, when you replace the 'yourButtonId' with the id of that specific button. (Read about view ids in android on google or inside a tutorial video)
Same thing for the linear layout
Now after declaring all of your buttons that way, maybe Button green and Button blue,... You just copy/paste the code above for each button you have, e.g. 'blue.setOnClickListener' and 'green.setOnClickListener' and change the 'Color.Red' to each desired color, voila you are done!
Click to expand...
Click to collapse
Thank you soo much... Learned a lot..
Mate I will tell you what I have done in detail ok. you correct me if I a doing something wrong.
1. Opened eclipse and selected new -> android project -> and created my app.
2. By default there is hello world text. so I deleted the text.
3. By default there is a layout i.e relative layout
4. So I dragged linear layout from the layout section. but got an warning message of something like there there is no id, no style etc. So I selected style from property windows and the warning message got dissappear
5. Then I inserted a large text. pressed F2 to change text of it. created a new string -> inserted name as My First App
6. After that I dragged 3 buttons. Again pressed F2 to change text and id off all the button by creating new string -> name as Red, Blue and Green and ID as button1, button2 and button3.
7. After that my app graphical layout is finished.
8. After that as you said I have to write code in MainActivity.java file. So in src folder and my package I opened up MainActivity.java file. By default there were 2 onCreate() Functions. When I try to write any code inside the onCreate() it gives me an error.
Please correct me.
If you provide me with a zip of the whole project folder, i would have a look at it if you would like
Mustakim_456 said:
Hello to everyone.
I am new comer in the world of Android App Development.
I am completely new in android app development.
I want someone to teach me some basics so that I can learn that and them implement my own imagination to build a desire app.
Firstly, I have installed the adt_bundel which include SDK and eclipse.
When I first started using android, I learned that it is open source that means we can easily free the source code of it.
when I started using apps and games I always thought to make an android app for me.
So for that reason I m posting this.
I want to create a very simple app in which there are 3 buttons and when click on that button the background should change.
Eg: There are 3 button red,blue,green. so if I press the red button the background should change to red color and vise versa.
So can anyone please help me in building my very 1st own Android App.
Click to expand...
Click to collapse
1)The first question is whether you know how to use the Java programming language fluently. That's a prerequisite.
2)If that's a yes then do you understand how the Android activity and fragment life cycle works and looks like? Probably not. Android is a lot about java APIs.
3)Goto https://developer.android.com and start learning Android APIs!
Maybe I'll write a startup guide to Android app programming. Give this a plus one if you'd like that! :thumbup:
Sent from my Nexus 4 using XDA Premium 4 mobile app
Masrepus said:
If you provide me with a zip of the whole project folder, i would have a look at it if you would like
Click to expand...
Click to collapse
sure I will provide you when I go back home. now I am in college.
boggartfly said:
1)The first question is whether you know how to use the Java programming language fluently. That's a prerequisite.
2)If that's a yes then do you understand how the Android activity and fragment life cycle works and looks like? Probably not. Android is a lot about java APIs.
3)Goto https://developer.android.com and start learning Android APIs!
Maybe I'll write a startup guide to Android app programming. Give this a plus one if you'd like that! :thumbup:
Sent from my Nexus 4 using XDA Premium 4 mobile app
Click to expand...
Click to collapse
+1
bro I don't know any thing much in programming.
I have just learned very basic java programming in my college as tere was a subject. what I learned in my college and what I see in tutorials of android is completely different. so in short I am completely new to android and its java programming.
if you don't mind could you make a step by step guide of android app development as my app as an example. it would be very useful too.
Yeah the thing with android and java that you learn in school is this: if you learn java, you got i would call it the ability to use the basic operations you later definitely need for programming android, as android is obviously a java api. But in many cases android has different ways of approachal, one of the biggest being the activity system.
So learning java is important, but you have to know that some of the stuff you learn there you wont ever use with android.
Masrepus said:
Yeah the thing with android and java that you learn in school is this: if you learn java, you got i would call it the ability to use the basic operations you later definitely need for programming android, as android is obviously a java api. But in many cases android has different ways of approachal, one of the biggest being the activity system.
So learning java is important, but you have to know that some of the stuff you learn there you wont ever use with android.
Click to expand...
Click to collapse
I think thats the thing. Because there is no such thing which I learned in college goes for android programming. I learned different types of constructor and inheritance etc .
but in android its totally different.
Mustakim_456 said:
I think thats the thing. Because there is no such thing which I learned in college goes for android programming. I learned different types of constructor and inheritance etc .
but in android its totally different.
Click to expand...
Click to collapse
Yes constructor is such an example, because with android you only use it if you got a more complex project with more classes that are no activities. In any other case you put initialising things in onCreate() that you would normally put in the constructor
Same thing with inheritance, it is only needed if you use more complex projects. But you should anyways at least know what it is and how it works
Masrepus said:
Yes constructor is such an example, because with android you only use it if you got a more complex project with more classes that are no activities. In any other case you put initialising things in onCreate() that you would normally put in the constructor
Same thing with inheritance, it is only needed if you use more complex projects. But you should anyways at least know what it is and how it works
Click to expand...
Click to collapse
yeah bro I know how to do inheritance and initialize construtor but here its of no use na...
Here is the zip of my project.
No coding is done. its only graphical layout of my app.
Related
Is there anyway in Eclipse to have it update the R.java file without having to have a clean build. I am creating the XML files as I go, and am tired of having to comment out code just to have the file rebuild itself.
Also on my one project, the project can't seem to even get the R. Layout. Main to even be read. I noticed it when created the project, in the Main. Java file it had an error. Why does Eclipse have to be so confusing lol.
Thanks for help in advance
Sent from my HTC Desire CDMA using XDA App
You can try turning the automatic building of the application off, then building it when you're ready.
The only problems I ever have is when I go to run a program after a lot of xml changes and it puts a layout_name.out.xml. Then you delete it, close the project, open the project, and clean build.
There is also the saving the xml file to be able to use the view, whatever it may be in the source files.
Thanks for quick reply, what I would like is as soon as I save the XML files it updates the R. Java
Sent from my HTC Desire CDMA using XDA App
this is a problem with eclipse. search these boards for R.java and you will find many people complaining about how Eclipse messes with their project because of some issue with R.java. if you havent tried IntelliJ before you might try that. there are no issues using it with R.java
and the Community Android Edition is free.
So if I understand you correctly, you want to be able to create something in the xml, and have it create it in your class file, which will create it in your R.java file by itself?
If this is the case, I don't think it would work like this. Everything with an id created for you. I know some of my apps, I don't directly interact with some of the views, so I don't need them created. For example, I have a label like textview that stays the same. I don't want it created and throwing up the unused variable exclamation point, nor do I want to waste memory doing it. I think this was the simplest of options, instead of deleting everything you don't use, you just create what you do.
I don't know your level of programming knowledge, so I guess I'll just leave it at that. No disrespect intended, if I have somehow offended you.
j0hnZ said:
So if I understand you correctly, you want to be able to create something in the xml, and have it create it in your class file, which will create it in your R.java file by itself?
If this is the case, I don't think it would work like this. Everything with an id created for you. I know some of my apps, I don't directly interact with some of the views, so I don't need them created. For example, I have a label like textview that stays the same. I don't want it created and throwing up the unused variable exclamation point, nor do I want to waste memory doing it. I think this was the simplest of options, instead of deleting everything you don't use, you just create what you do.
I don't know your level of programming knowledge, so I guess I'll just leave it at that. No disrespect intended, if I have somehow offended you.
Click to expand...
Click to collapse
i believe he means that he has created something in xml with an @+id/something and then when he goes to use it in one of his activities/etc it throws an error because R.java has not been updated with the id of 'something'..
killersnowman said:
i believe he means that he has created something in xml with an @+id/something and then when he goes to use it in one of his activities/etc it throws an error because R.java has not been updated with the id of 'something'..
Click to expand...
Click to collapse
Weird, I've never had that problem.
killersnowman said:
i believe he means that he has created something in xml with an @+id/something and then when he goes to use it in one of his activities/etc it throws an error because R.java has not been updated with the id of 'something'..
Click to expand...
Click to collapse
Basically, it doesn't even recognize it basically. I guess I should just recreate the project and copy paste my stuff
Sent from my HTC Desire CDMA using XDA App
I know some people have already discussed this in the noRefresh App thread.
But I got a new idea
I don't wanna create a module and embed that to applications
Instead, I created a new Input Device in the kernel
After that, I set the device ( dev/input/event3 ) permission to allow read / write for others.
Then, write an app using JNI to catch the Input Device and set the mode of refreshing.
The prototype proved that this idea works, as i worked out it already, but not very perfect, still in experiment
I am just in some problems of threading, as I am not a professional programmer
This is wt i have now:
http://www.youtube.com/watch?v=GkFyvRR6In8
NEW!! : http://www.youtube.com/watch?v=cucG03rg3tg
I know that my method is a bit dirty,
but at least it works XD
I hope that someone would like to help
Upload those code soon
By the way, why can't i change content of init.rc ?
It removes my changes after reboot..... How to solve?
wheilitjohnny said:
By the way, why can't i change content of init.rc ?
It removes my changes after reboot..... How to solve?
Click to expand...
Click to collapse
You need to modify uRamdisk to change init.rc.
Would you like to tell me how to unpack the uRamdisk? I use both Windows and Ubuntu, any methods on these two platform is ok. Thanks!
Try the script from this post http://forum.xda-developers.com/showpost.php?p=24135886&postcount=72
This is simply amazing. Since you already have it working, polishing it shouldn't take too long (I think, I'm still a beginner programmer).
I don't think /dev/input is a good place to park that.
There are any number of things that could show up there and affect order.
Why not put it in its own directory?
Maybe I'm missing something, but if we use your method you still expect applications to have to be modified to read/write from event3 to control display mode?
As it is now, you only need a single function call to switch display modes. Yes, there is a little bit of housework to do before that, but what could be simpler than a single function?
I think no place else is more suitable than /dev/input.
As /dev/input is the linux kernel's input system.
In the driver, I would only use the input report system but not use the I/O system.
Maybe even setup a input/event searching function to solve the problem later.
The case now is that, the touch screen originally just provide event2,
but if we need to extract move and fingerup information on upper level, it may waste some time.
In order to make the whole algorithm easier and faster, I added 1 more event in the zForce driver,
to only output FingerMove and FingerUp state.
As the reporting system is starting from the kernel, this hack would need to change uImage + uRamdisk + Add an App. The project is quite huge.
My final target is to make the application remembering your choice on what mode u need for the focusing application.
AlwaysOn / OnlyWhenDragging / AlwaysOff, so on.
Of coz the click-to-call function still work.
Isn't it a better workflow and more intuitive, isn't it the thing that we would expect?
I already have an dev/input/event3 on my system and sometimes event4, 5, 6.
That's why I don't think that whatever it is you are doing belongs there.
Is the point to allow coders for user applications to interface with your driver?
Is this supposed to work without modifying user applications?
What information would be going in/out of event3?
Clearly I am missing something here.
Actually not necessary to be event3, the system will auto-ly create a new eventx, just in normal case, without any extra USB devices, a nook should only have up to event2. So, I use event3 as an example here.
We can later make the program auto-ly search back which is the needed eventx.
Yes, u r right, we no need to modify any other applications and this is exactly the point y i am creating this!
Anyone know how to call a FullScreen Refresh in a service?
always on?
Look really great. Since blinking after scrolling is incomfortable is it possible to have also "always on " mode using your new ideas?
My final target is to make the application remembering your choice on what mode u need for the focusing application.
AlwaysOn / OnlyWhenDragging / AlwaysOff, so on.
Of coz the click-to-call function still work.
Also an Over-Ride mode, for more flexible using.
But the most basic part need to be handled well now, as it is not very perfect now.
I still don't figure out how to call the e-ink driver to refresh the screen =-=
wheilitjohnny said:
I still don't figure out how to call the e-ink driver to refresh the screen =-=
Click to expand...
Click to collapse
Have you tried
Code:
echo 1 > /sys/class/graphics/fb0/epd_refresh
?
wheilitjohnny said:
Yes, u r right, we no need to modify any other applications and this is exactly the point y i am creating this!
Click to expand...
Click to collapse
Are you saying that your idea does not require modifying user applications?
If it doesn't, then there is no need to have a public interface.
It will be only your code talking to your code.
What is the point of this /dev/input/event3? You say that it will be writable. What's going in and out?
Some apps will be using gestures, some dragging. How are you going to keep track of that all?
I have one application that works perfectly fine now, one activity uses swipe gestures to page up/down while another activity uses drag with a user choice of A2 and display while dragging or else only panning at ACTION_UP.
All this requires less than 10 lines of code.
With multitouch, many applications don't even need A2. Even normal panning in Opera Mobile works much better now that Opera doesn't try to display while panning.
Maybe my english is too bad, cannot express the idea well.
I know, we can make such an application with noRefreshDrag working on its own well.
But how about other applications, it is impossible for us to change all applications.
So, my idea is making it system based.
My prototype is very good now, after several adjustment.
Not limited to only 1 application, but the whole system.
The approach is like this:
1. zForce driver provide extra information to InputEvent
2. A JNI catch the InputEvent
3. A service get the data and set the update mode
We only need to write 1 application to handle the setting of this chain.
This is what i mean, hope that u get what i mean now.
mali100 said:
Have you tried
Code:
echo 1 > /sys/class/graphics/fb0/epd_refresh
?
Click to expand...
Click to collapse
Let me try it now!
wow, it works great
wheilitjohnny said:
zForce driver provide extra information to InputEvent
Click to expand...
Click to collapse
I guess that this is the part that I don't understand.
What is this extra information?
Renate NST said:
I guess that this is the part that I don't understand.
What is this extra information?
Click to expand...
Click to collapse
It created an extra Event, l called it Event3 before.
Driver reports only move and finger_up to Event3.
Just providing a channel to pass an information from driver to user-space.
You may ask why not directly using the existing Event, need to create another one.
That is because, the original one only have touch and position information, parsing them back to move information need a bit of work. As the hardware provided the move information, then don't waste it.
Code:
public boolean dispatchTouchEvent(MotionEvent event)
{
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
break;
}
return(true);
}
}
Isn't that everything that you could ask for?
Hi,
I am new to the Android development scene and I need to pull off a math game project in less then a week. I am using Eclipse to do this, but is there any other easier way such as Visual basic GUI way that you can pull off 100% raw code from it?
Can someone help me through this? . I need basic knowledge on text label, input box, button, perform math operation , if statement, loops, and method. If anyone can quickly guide me through this would be great.
Basically I am programming a math game with subtraction and division. I need to have difficulty mode easy medium hard which is basically randomized numbers in bigger range. Then there is level from 1-4 which is increasement of number of questions. And there are lives also.
Thanks In advance if someone can help me
This sounds like a class
Have you tried to even do the layout in eclipse yet? That is a pretty simple step that shouldn't take longer than 15 minutes.
joshtrader said:
Hi,
I am new to the Android development scene and I need to pull off a math game project in less then a week. I am using Eclipse to do this, but is there any other easier way such as Visual basic GUI way that you can pull off 100% raw code from it?
Can someone help me through this? . I need basic knowledge on text label, input box, button, perform math operation , if statement, loops, and method. If anyone can quickly guide me through this would be great.
Basically I am programming a math game with subtraction and division. I need to have difficulty mode easy medium hard which is basically randomized numbers in bigger range. Then there is level from 1-4 which is increasement of number of questions. And there are lives also.
Thanks In advance if someone can help me
Click to expand...
Click to collapse
It will be really simple to make.. I can code it if you want.. but I am really bad in making the GUI.
zalez said:
This sounds like a class
Have you tried to even do the layout in eclipse yet? That is a pretty simple step that shouldn't take longer than 15 minutes.
Click to expand...
Click to collapse
yes i did try to fiddle with the gui layout in eclipse, i was able to make a text box, input box and button that display a message.
prototype-U said:
It will be really simple to make.. I can code it if you want.. but I am really bad in making the GUI.
Click to expand...
Click to collapse
Thanks for offering to help but i would like to understand the fundemental of coding in this android field. I have indeed done Java before, and i made the exact math game that i am attempting to do on this project, my java game was consisting with loops/if/while statements (for # of questions), randomizing numbers, menu options. I do know that android uses java language but when i went through the official guide on the developer website, it seems a bit too challenging to follow.
it would be great if someone could guide me with an example statements of using methods, loops such as if and while/for how to interpret info from input box and or interpret from user click button and such.
If it would be any help, i can post my original java math game
Hello, I am developing my very first app using the latest Android bundle (SDK + Eclipse included). I also installed LibGDx since it will be a game. Upon following some tutorials online it dawned on me, how the hell can I SEE what I'm doing? Meaning I have no room/screen view. So when I render an object onto the screen I just have to wing it when it comes to where it should start on the screen. This is obviously not a good way to make games, its kinda tedious. Precise placement of objects as in "Angry Birds" will be very hard. I'm well aware of emulation but that only shows things after the fact. There has to be a way to do it, maybe a plugin or something? Being able to drag/drop and interact with objects on this screen w/out code would also help a lot. Much thanks!
Eclipse shows the layouts at runtime. You don't have to run it on the emulator each time to manage layouts. Double click on the layout file and it will open in a side view. At the bottom, there will be 2 options, viz Layout.xml and Graphical Layout. Choose Graphical Layout.
EatHeat said:
Eclipse shows the layouts at runtime. You don't have to run it on the emulator each time to manage layouts. Double click on the layout file and it will open in a side view. At the bottom, there will be 2 options, viz Layout.xml and Graphical Layout. Choose Graphical Layout.
Click to expand...
Click to collapse
Thanks for replying, but I should've mentioned that I'm aware of this. This view only shows things I drag and drop onto from the same window. It doesn't show anything rendered in the code from other classes. So anything important like the main characters aren't there. Is there perhaps some way to make them show up in here?
Android3000 said:
Thanks for replying, but I should've mentioned that I'm aware of this. This view only shows things I drag and drop onto from the same window. It doesn't show anything rendered in the code from other classes. So anything important like the main characters aren't there. Is there perhaps some way to make them show up in here?
Click to expand...
Click to collapse
Layouts managed by code isn't showed. Only the xml layouts are show up.
EatHeat said:
Layouts managed by code isn't showed. Only the xml layouts are show up.
Click to expand...
Click to collapse
This is what bothers me. How in the world am I suppose to develop complex levels and what not without being able to see placement of things live? I can't imagine trying to make angry birds or something like that by only having coordinates and no visuals. So there isn't any add-on or plugin that can provide such capabilities?
There isn't any official visual level editor by LibGDX.
But maybe you find an Editor that saves your level in xml or sth. and you can parse that xml in ur LibGDX-App. I wrote my own Level-Editor specially for my LibGDX-Game and it wasn't so hard to code.
Regards
Android3000 said:
This is what bothers me. How in the world am I suppose to develop complex levels and what not without being able to see placement of things live? I can't imagine trying to make angry birds or something like that by only having coordinates and no visuals. So there isn't any add-on or plugin that can provide such capabilities?
Click to expand...
Click to collapse
Not that I know of. You can just create the whole layout in xml to get it correct first. Then use those coordinates in your code accordingly.
Apparently various types of 3rd party apps do exist for this task . Thanks for the help guys.
just try another game engine
Hello,
I create that thread to share with you a tutorial showing you how to access the Java 8 language features with Android N. You can find the tutorial here on my blog : http://www.ssaurel.com/blog/accessing-to-the-java-8-language-features-with-android-n/ . There are also others great tutorials for Android development.
If you prefer to read the tutorial here, this is the complete content :
Accessing to the Java 8 language features with Android N
Google has unveiled developer preview of Android N last month with a new exciting developer program. Official release is planned for Q3 2016. Amongst the new features of Android N, the support of several Java 8 language features is a great addition for developers.
Supported features include :
Lambda expressions
Default and static interface methods
Repeatable annotations
Method References
Additionally, some new APIs introduced with Java 8 are available like reflection and language-related APIs and Utility APIs (java.util.function and java.util.stream).
Set up your development environment
To support Android N new features, you need to set up correctly your development environment. You need to get last version of Android N developer preview and to download Android Studio 2.1. You can make these installations via the Android SDK Manager on your computer. To get more details about the installation process, don’t hesitate to read the official documentation from Android Team : http://developer.android.com/preview/setup-sdk.html
Enable support for Java 8
In order to use the new Java 8 language features, you need to use the new Jack toolchain. Jack, for Java Android Compiler Kit, replaces javac compiler. It compiles Java language source code into Android-readable dex bytecode with its own .jack library format. Furthermore, it provides other great toolchain features inside a single tool like repackaging, shrinking, obfuscation and multidex. Before Jack, you needed to use ProGuard to achieve these tasks for example.
To enable support for Java 8 in your Android project, you need to configure your build.gradle file like that :
Code:
android {
...
defaultConfig {
...
jackOptions {
enabled true
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
Enjoy your first Lambda Expression on Android
Now, you can enjoy your first Lambda expression on Android. Basically, a Lambda expression is a block of code that can be passed around to be executed later. In fact, it is very similar to anonymous classes. However, Lambda expressions are succinct with less verbose code. Let’s use a Lambda to set a click listener on a Button :
Code:
button.setOnClickListener(view -> Log.d(TAG, "Button Clicked."));
You can make the same with an item click listener on a ListView :
Code:
listView.setOnItemClickListener((parent, view, position, id) -> {
// …
}
It works great with a Runnable interface too :
Code:
Runnable runnable = () -> Log.d(TAG, "Log from a Runnable with a Lambda");
newThread(runnable).start();
Clearly, you can see the code is shorter and more readable with Lambdas expressions. It will be great for the productivity of Android developers.
Create your first Functional Interface on Android
To create our first Functional on Android, let’s take classic example of a Calculator with an addition feature :
Code:
@FunctionalInterface
public interface Calculator {
int calculate(int a, int b);
}
Now, you can add a default method to the Calculator interface that will be used to display a result for example :
Code:
@FunctionalInterface
public interface Calculator {
int calculate(int a, int b);
default void print(String result){
// … print result …
}
}
Finally, we can create a Lambda expression with that interface :
Code:
Calculator c = (a, b) -> a + b;
Like you can see with those simple examples, Java 8 language features will offer a new way to Android developers to improve their productivity. Thanks to the developer preview of Android N, you can try these features right now.
Don't hesitate to share your feedbacks with me.
Thanks.
Sylvain
Is it worth going to Jack right now, though?
I've read that it's slower in build time than the normal configuration, and as some apps already take a huge amount of time to compile, I'm not sure if it's worth it.
Also, lambdas are only syntactic sugar, right? Behind the scenes, everything is still the same.
AndroidDeveloperLB said:
Is it worth going to Jack right now, though?
I've read that it's slower in build time than the normal configuration, and as some apps already take a huge amount of time to compile, I'm not sure if it's worth it.
Also, lambdas are only syntactic sugar, right? Behind the scenes, everything is still the same.
Click to expand...
Click to collapse
For the moment, I just tried on small projects. So, it was not so slow during build time. May be on big projects ?
These new features are not a revolution for sure, but it's interesting to have the choice to use it in the future when you develop Android apps. Be able to have some functional programming features is a good thing.
For Lambdas, it's not really the same thing on the bytecode. On Java 8 desktop apps, some benchmarks showed that using Lambdas is very often faster.
sylsau said:
For the moment, I just tried on small projects. So, it was not so slow during build time. May be on big projects ?
These new features are not a revolution for sure, but it's interesting to have the choice to use it in the future when you develop Android apps. Be able to have some functional programming features is a good thing.
For Lambdas, it's not really the same thing on the bytecode. On Java 8 desktop apps, some benchmarks showed that using Lambdas is very often faster.
Click to expand...
Click to collapse
I see.
For Lambdas, I don't know. It's just my guess that whatever you write is converted to normal code that you probably should have written without lambdas.
Perhaps the JDK has some optimizations for it.
EDIT: it seems there is a difference:
http://www.oracle.com/technetwork/java/jvmls2013kuksen-2014088.pdf
http://blog.takipi.com/benchmark-how-java-8-lambdas-and-streams-can-make-your-code-5-times-slower/
Nice. Wonder if it's this way on Android too.
AndroidDeveloperLB said:
I see.
For Lambdas, I don't know. It's just my guess that whatever you write is converted to normal code that you probably should have written without lambdas.
Perhaps the JDK has some optimizations for it.
EDIT: it seems there is a difference:
http://www.oracle.com/technetwork/java/jvmls2013kuksen-2014088.pdf
http://blog.takipi.com/benchmark-how-java-8-lambdas-and-streams-can-make-your-code-5-times-slower/
Nice. Wonder if it's this way on Android too.
Click to expand...
Click to collapse
Thanks for the links. Really interesting.
It seems you were right. Lambdas and Stream can make execution slower.
sylsau said:
Thanks for the links. Really interesting.
It seems you were right. Lambdas and Stream can make execution slower.
Click to expand...
Click to collapse
They do? I thought the data and graphs show Lambdas are faster. Are you sure?
AndroidDeveloperLB said:
They do? I thought the data and graphs show Lambdas are faster. Are you sure?
Click to expand...
Click to collapse
It depends the way you use it like said on takipi blog. If they are well used, it can make your code run faster however. It's always the same thing, it depends from your implementation and the way you use it.
sylsau said:
It depends the way you use it like said on takipi blog. If they are well used, it can make your code run faster however. It's always the same thing, it depends from your implementation and the way you use it.
Click to expand...
Click to collapse
So there is no definite conclusion? So my guess is that for Android it's too early to see difference between normal code and Lambda.
Pretty Information
such a nice post, i am studying the java but it is totally different why can you please explain what the java in book is different the java in applications??
Regards Junaid Shahid
---------- Post added at 02:47 PM ---------- Previous post was at 02:42 PM ----------
AndroidDeveloperLB said:
So there is no definite conclusion? So my guess is that for Android it's too early to see difference between normal code and Lambda.
Click to expand...
Click to collapse
:good:
@sylsau thanks bro nice guide :good:
junaidshahidcom said:
such a nice post, i am studying the java but it is totally different why can you please explain what the java in book is different the java in applications??
Regards Junaid Shahid
---------- Post added at 02:47 PM ---------- Previous post was at 02:42 PM ----------
:good:
Click to expand...
Click to collapse
They are almost the same. The pure Java is still there, but some classes do not exist or have less functions, because Android is a mobile OS.
There is just another layer of the framework itself, that you need to learn, when developing for Android.
Thanks For Elaburation
AndroidDeveloperLB said:
They are almost the same. The pure Java is still there, but some classes do not exist or have less functions, because Android is a mobile OS.
There is just another layer of the framework itself, that you need to learn, when developing for Android.
Click to expand...
Click to collapse
Can you suggest me how to become a good developed of Java Along Android?
junaidshahidcom said:
Can you suggest me how to become a good developed of Java Along Android?
Click to expand...
Click to collapse
Learn both (first Java of course).
You can watch "the new boston" videos to get started:
https://thenewboston.com/videos.php
You can also read a lot on Google's website, but only after you are good with Java.
awesome
AndroidDeveloperLB said:
Learn both (first Java of course).
You can watch "the new boston" videos to get started:
https://thenewboston.com/videos.php
You can also read a lot on Google's website, but only after you are good with Java.
Click to expand...
Click to collapse
the website you referred is awesome, i will try to learn all programming languages from here <3
regards Junaid Shaid
princevirus said:
@sylsau thanks bro nice guide :good:
Click to expand...
Click to collapse
No problem. I'm going to publish more tutorials very soon
Thanks.
Android小楼阁-QQ群 439637151
来自搭载Android 2.3 GingerBread的华为Y220-T10
Hello,
I made a new tutorial available on XDA Forum. It shows you how to implement a Navigation Drawer with a Toolbar on Android.
Don't hesitate to discover it : http://forum.xda-developers.com/android/software/tutorial-implement-navigation-drawer-t3388990
Sylvain
sylsau said:
Hello,
I made a new tutorial available on XDA Forum. It shows you how to implement a Navigation Drawer with a Toolbar on Android.
Don't hesitate to discover it : http://forum.xda-developers.com/android/software/tutorial-implement-navigation-drawer-t3388990
Sylvain
Click to expand...
Click to collapse
hi, can that commit can help to use java 8 on M?
https://github.com/kogone/android_build/commit/ac67b54797f06b67a1ce0e39aa8528dbffac3b92