[Tutorial] Accessing to the Java 8 language features with Android N - Android Software Development

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

Related

Absuluut newbie, help on eMbedded C++ 4.0 please

Hello,
I'm a Visual Basic programmer for some time now.
Made some useful stuff they tell me.
Used C++ (Borland) about 10 years ago, so that’s a bit rusty.
Now I've ordered a Qtek 9090, and I would want to develop some software for it to. So I downloaded and installed the SDK and eMbedded C++ 4.0.
I thought, I start out on the emulator......
I can't even get my own "Hello world" program to work........
I have downloaded a "Hello World" program which I stepped through in debug mode. I have NO idea what they are doing there.
It contains about 20 files, hundreds lines of code, just to put "Hello World" on the screen of the emulator.
And I hoped that:
Code:
#include <stdio.h>
main()
{
printf("Hello, world!\n");
return 0;
}
pleased in a form, would do the trick………
The help in the IDE does not work.
Re-install it tell's me. I have done so, but the help function does not work.
Where is the "visual" part in the embedded Visual C++?
How do I place forms and buttons e.g.?
I have no idea where to start now.
Searched a lot of forum's for starters-help, but I can not find anything that helps me on my way. I just hope I've not become stupid.....
Would somebody please help me on my way?
Can I wholeheartedly recommend the book "Programming Windows CE", by Douglas Boling? It's normally cheap (or the second edition is) on ebay and it really is good.
Failing that, you're writing a console based application for something that uses a windowing environment by default, so you'll either have to change what you're linking to, or have a winmain that makes use of a graphical UI rather than stdout. Call MessageBox perhaps? This is all made *so* much easier with a working help system that you need to get that working.
You can manage resource files visually, so it really is Visual development. Plus, for free, it's an excellent development tool. Well, it isn't bad.
The best advice I can give is to get your machine set up correctly with EVC2002, or EVC 4 with SP4 and the appropriate SDKs, and take it from there.
Good luck starting out.
Cheers,
Nick.
chiark said:
Can I wholeheartedly recommend the book "Programming Windows CE", by Douglas Boling?
Click to expand...
Click to collapse
Thanx. I'm going to order that book.
Failing that, you're writing a console based application for something that uses a windowing environment by default, so you'll either have to change what you're linking to, or have a winmain that makes use of a graphical UI rather than stdout. Call MessageBox perhaps?
Click to expand...
Click to collapse
Well, yes. I know. Like I sayed, I usualy work with VB. It was just me, trying to oversimplify things.
What I ment to express is that I'm pretty supprised I still have to write the message-loop and the main-loop and the jsadgkh-loop and....
I just hoped to put up some forms and attach code to it. But maybe I'm missing the clue on this.
This is all made *so* much easier with a working help system that you need to get that working.
Click to expand...
Click to collapse
I have set it up on an other machine yesterday, and there the help works.
Today I'm going to make use of it.
You can manage resource files visually, so it really is Visual development. Plus, for free, it's an excellent development tool. Well, it isn't bad.
Click to expand...
Click to collapse
Well.... That part, the visual part, I don't see yet, but like I sayed, I'm going to work throuhg the help, now that I've got that working.
And a 'free' tool. Well, that's allways nice. I think it is good for us, AND good for them. The more software there will be on the market, the better the devices sell.
The best advice I can give is to get your machine set up correctly with EVC2002, or EVC 4 with SP4 and the appropriate SDKs, and take it from there.
Good luck starting out.
Cheers,
Nick.
Click to expand...
Click to collapse
Thanks for you tips.
Rens
Dox, drop me a PM, I've got an old copy of the book you can have if it would help
Re the message loop stuff, you've indeed got options. You can either use the message loop approach hitting the API directly, or you can opt for using MFC to abstract the stuff away from you.
Personally, I prefer the straightforwardness of using the API rather than MFC. By the time you've written one application, you've got the bulk of the next . I also write for older machines, and the overhead of MFC is a consideration, but on the XDA it really isn't.
If the application is simple, it can all be handled by a DialogBox. You will need to write a DialogProc to handle the appropriate messages, but the need to register a window class, get messages off the queue etc is removed from you. Similarly, you can have multiple pages on a single dialog box using propertypages.
If you did want to write a console application, you can do this but you need to change the linker options within EVC.
Have a good look at the samples, too, there's some real good stuff in there.
Cheers, and good luck
Nick.

[DEV] AndroidLib - .NET Android Device Communication and Management Library 01.20.13

Description:
AndroidLib is a .NET assembly written in C# (C-Sharp) that easily handles communication between a connected Android device and your program. Currently, there is a large amount of automated controls, eliminating thousands of lines of code the programmer has to write themselves. The class AndroidController is a semi-wrapper of the ADB (Android Debug Bridge) binary. The other class you will be working with the most is the Device class. This class contains useful information about the device (for example: software/hardware info, memory info, battery stats, mount points for partitions, root status, busybox information, and much more), as well as exposes many instance methods to control your phone such as Rebooting, Mounting Filesystems, Push/Pull/Install Files, and much more to come. AndroidLib contains all of the Android binaries necessary to work properly. AndroidLib also assumes that the phone's USB drivers are already installed correctly on the target machine, or that your program will take care of it on it's own.
This is perfect for any developer who would like to create, for example, an auto-rooter or any other application that needs to connect with Android devices through a .NET application. AndroidLib provides all the methods needed to communicate with the Android device. This will cut back on the code you have to come up with and write yourself by a HUGE amount!
What it does:
Provides easy-to-use code for communicating with Android devices in .NET
Provides easy access to information about the connected Android devices
Has a large list (and growing...!) of methods that control connected Android devices
Please credit the work here by me in your own projects; not only to give thanks to me and the many hours I am putting in to this project, but so others know where to find it if they need to!
Usage:
Add a reference to AndroidLib.dll in your .NET project and begin using this great API. Please refer to the "Getting Started.txt" guide and full documentation included in the zip.
Requirements:
.NET 3.5 or Higher
Changelog (Only most recent version displayed, full Changelog in download)
Version 1.5.1.0 | 01.21.13
Fixed Device.InstallApk() bug
Download Latest Release
GitHub
Online Documentation
Sample Solutions Using AndroidLib:
C# (C Sharp)
Visual Basic (VB)
AndroidLib Featured Projects by XDA Users:
RegawMOD Evo 4G LTE Rooter - XDA
RegawMOD CDMA Hero Rooter - XDA
RegawMOD Rebooter - XDA
Droid Manager by DeepUnknown - XDA - Google
Android SMS - XDA - Home Page
Quick ADB Pusher by Goatshocker - XDA
reserved just in case
It's very useful, thank you very much, im planning to code a Filemanager like qtadb, because qtadb is sucking too often
In the process of completely redesigning the library (due to coding stupidity), basically from the base class up. I should have a beta1 out by this weekend for testing. All that are interested in beta testing this library for their Android .NET projects, post here and I'll add you to the list of testers!
It would be great!
Can you add something like adb forward? So we can connect to an android service without using ADB, that as we all know sucks!
Mrc527 said:
It would be great!
Can you add something like adb forward? So we can connect to an android service without using ADB, that as we all know sucks!
Click to expand...
Click to collapse
Yeah, I'll throw in a method to create a port forward. What I have now uses the bridge, which is included in the assembly, but handles all of it silently and very well. I should have a build out soon (most likely this weekend). As long as you don't dispose the AndroidController object, that port forward will be good, so you can use your own Socket code
regaw_leinad said:
Yeah, I'll throw in a method to create a port forward. What I have now uses the bridge, which is included in the assembly, but handles all of it silently and very well. I should have a build out soon (most likely this weekend). As long as you don't dispose the AndroidController object, that port forward will be good, so you can use your own Socket code
Click to expand...
Click to collapse
Great work! really, great idea!
You can change the .NET to 3.5? I too code in .NET, and I try to keep the .NEt version as low as possible!
SimranSingh said:
You can change the .NET to 3.5? I too code in .NET, and I try to keep the .NEt version as low as possible!
Click to expand...
Click to collapse
Yeah, I actually did that a few days ago, forgot to update the OP.
Where is it possible to download?
Mrc527 said:
Where is it possible to download?
Click to expand...
Click to collapse
I'm just writing the documentation for this. I'm pretty sure I'll have it done today.
Yeah! Just a joke. When finished I'm sure will be a success!
Inviato dal mio Galaxy Nexus usando Tapatalk
Making some last minute changes to the Device class, then I'm going to finish the documentation and release it. Just keeping you updated.
Ok everyone, the new documentation is up (Online) (Offline). For the beginning of this product, I would like developers to pm me, or reply here in the thread if they would like to try the library out for their project, and I'll send it to them. It's still under development, and there will be updates coming out regularly. Shoot me a pm or post here and I'll send you a link right away.
regaw_leinad said:
Ok everyone, the new documentation is up (Online) (Offline). For the beginning of this product, I would like developers to pm me, or reply here in the thread if they would like to try the library out for their project, and I'll send it to them. It's still under development, and there will be updates coming out regularly. Shoot me a pm or post here and I'll send you a link right away.
Click to expand...
Click to collapse
Meeeeeeeeeeeeeeeeeeeeee!!
Hi,
I'm C# developer and i would like to try your lib, can you send it please?
Thanks in advance, and great work.
Hey guys, check the first post to download the library. It is in a zip which includes the dll, "Getting Started.txt" and the documentation. Please read the getting started guide before diving into it! And please give me feedback on it. That would be much appreciated in order for me to deliver a better product.
Dan
Great work!
It works without any problem to me!
Next update will have these features internally implemented:
Package Manager (inside the phone's shell)
Ability to install/uninstall apks
Ability to freeze/unfreeze apks
Ability to backup/restore apks
A class that will handle signing of update zips
More internal information about connected device (cpu, environment, etc)
Possible wrapper of AAPT
That seems like a good amount for the next update. Please post anything you wish to share about the library after using it for these few days.

[Q] Do I need to write drivers?

I have fair number of years of programming behind me. But I haven't tried anything for Android, as I dislike Java.
But I want to try. I want to make something that works at low level, say, like a firewall. It acts as a filter between two communicating parties/devices.
To write anything like that, can someone suggest which is the best approach - code in Java (if it can perform such a feast) or code driver in C?
Thanks much!
Regards,
Nayan
Sent from my Micromax A117 using xda app-developers app
The interesting thing in such low level projects is the entry point: So for a firewall you only have to acces iptables because android has linux kernel. So no C-part, no drivers, only plain java. See AFWall, it's an open source firewall.
EmptinessFiller said:
The interesting thing in such low level projects is the entry point: So for a firewall you only have to acces iptables because android has linux kernel. So no C-part, no drivers, only plain java. See AFWall, it's an open source firewall.
Click to expand...
Click to collapse
Excellent! I would certainly study AFWall. Thanks for the reference.
But actually, my quest doesn't stop here. I am exploring Android, and Google's restriction of "UI to be built only via Java". (I am not interested in scripting and widgets for now, unless they are absolutely needed.)
I want to know the answers for same question (driver or app) for the following:
* Network Filter [EmptinessFiller, you already answered this as Java]
* Disk (SD cards) (for many various purposes) - file system should not block the intention, hopefully.
* USB filter
* SMS filter
I am still thinking of other categories. Will write more later.
Please suggest and refer. Thanks again!
General answer: Your app is always built in java. (It's UI components, it's LifeCycle (Activity, Service, Broadcastreceiver))
You may include native code, but that does not have more possibilities. It's only a little bit quicker.
Forgotten: If you have root, you may want to change some binaries, because you can't change things in an app. There you need native code of course.
EmptinessFiller said:
You may include native code, but that does not have more possibilities. It's only a little bit quicker.
Click to expand...
Click to collapse
Shame, isn't it? Too much power in Java's hand
EmptinessFiller said:
Forgotten: If you have root, you may want to change some binaries, because you can't change things in an app. There you need native code of course.
Click to expand...
Click to collapse
My point exactly! Low level stuff is best written in native code.
But right now, I am learning how to.
cnayan said:
Shame, isn't it? Too much power in Java's hand
My point exactly! Low level stuff is best written in native code.
But right now, I am learning how to.
Click to expand...
Click to collapse
Have a look at the Android NDK and this guide about the development of root apps.
nikwen said:
Have a look at the Android NDK and this guide about the development of root apps.
Click to expand...
Click to collapse
Thanks for the link. Good stuff, but won't help in my targets... unless an example is seen.

[ help needed ]

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.

[Q] [DEV][Gradle] How to make a modular Application ?

Hi guys,
I'm currently working on an app for a company that asked me to make it modular.
Actually the app contains different features and the company would like to be able to compile the differents flavors of the App via Gradle.
If I understood correctly the current flavor solution offered in Android Studio doesn't fit my needs as if I have like 5 differents modules I'd have to make a flavor for : feature 1, feature 1 + 2, feature 1 + 2 + 3, feature 1 + 2 + 3 + 4, feature 1 + 2 + 3 + 4 + 5, feature 2, feature 2 + 3, ...... and so on.
So how can I say to gradle I just want to compile this part of my code ?
And how can I manage my code in a clean way for my app to understand which modules are enabled or not ?
Thanks a lot for your answers !
Not sure I'm understanding the problem. Create the flavors and in each of the src/<flavor>/ directories you can specify a build.gradle file and in that gradle file you can add dependencies for other modules. So this modular code would exist in <project root>/libprojects and the separate flavors would then conditionally include that code as a dependency. Make sense?
.
libprojects/module1
libprojects/module2
libprojects/module3
src/flavor1/build.gradle
src/flavor2/build.gradle
/src/flavor3/build.gradle
Each of those build.gradle files in the separate flavors could include dependencies for modules that it needs. Remember, code in a flavor takes precedence over code in src/main/java. So use that to your advantage to creating modular code.
Thanks a lot for your answer
It kinda give me an answer for the concept of isolating code on libraries. But the code isn't the biggest issue for me. Is it possible to put the resources (drawable and raw) in the libs so that I can grab them only on the required flavor of my app ? And do this via gradle dependency as I would have too much flavors otherwise.
Thanks again
joplayer said:
Thanks a lot for your answer
It kinda give me an answer for the concept of isolating code on libraries. But the code isn't the biggest issue for me. Is it possible to put the resources (drawable and raw) in the libs so that I can grab them only on the required flavor of my app ? And do this via gradle dependency as I would have too much flavors otherwise.
Thanks again
Click to expand...
Click to collapse
FYI, make sure you tap "reply" in the future so a person's post is "quoted" otherwise that person, me, doesn't get notified you responded from XDA. I just happened to click back on this post. Didn't receive a notification.
When you say libs do you mean "libprojects" folder or do you mean libs as in creating a jar file with those resources? Don't forget you can copy the dependencies from another flavor if you need to at compile time. Not sure if that would help in your scenario.
shafty023 said:
FYI, make sure you tap "reply" in the future so a person's post is "quoted" otherwise that person, me, doesn't get notified you responded from XDA. I just happened to click back on this post. Didn't receive a notification.
When you say libs do you mean "libprojects" folder or do you mean libs as in creating a jar file with those resources? Don't forget you can copy the dependencies from another flavor if you need to at compile time. Not sure if that would help in your scenario.
Click to expand...
Click to collapse
Sorry, bad old reflex
Actually that is my problem. How am I supposed to isolate my features ? Should I make an Android library ? Should I brutally use gradle to isolate some files ?
Thanks again
joplayer said:
Sorry, bad old reflex
Actually that is my problem. How am I supposed to isolate my features ? Should I make an Android library ? Should I brutally use gradle to isolate some files ?
Thanks again
Click to expand...
Click to collapse
It really depends on what you mean by isolate features. If you mean paid vs free app then you should undoubtedly isolate those features with a gradle variable that is set at compile time and read from at runtime via the BuildConfig class.
Code:
if (BuildConfig.IS_PRO) {
... some code
}
It's more of a mess to exclude code then to just make a single code source where you conditionally include/exclude based on compile time variables.
shafty023 said:
It really depends on what you mean by isolate features. If you mean paid vs free app then you should undoubtedly isolate those features with a gradle variable that is set at compile time and read from at runtime via the BuildConfig class.
Code:
if (BuildConfig.IS_PRO) {
... some code
}
It's more of a mess to exclude code then to just make a single code source where you conditionally include/exclude based on compile time variables.
Click to expand...
Click to collapse
Thanks a lot I think this kind of syntax might help me.
To make things clear the product will be compilable for different end products. One might want geolocalization, another not. As features end up taking space in the APK I need to exclude them from compilation. And finally this app must be compilable out of Android Studio so Gradle is my only savior ! And I'm far from beeing a Gradle expert so thanks for your answer I'll look at it

Categories

Resources