- My First App: How to Create Your First Android App Step by Step
- Prerequisites
- 1. Create a New Project
- 2. Create an Activity
- 3. Create a Layout
- 4. Implement Event Handlers
- Conclusion
- Android App Templates and UI Kits From CodeCanyon
- Open Source Your Android Code — The Complete Guide
- ADA | Adam Deconstructs Android
- Implementation
- Place code inside an Android Archive Library (AAR) — Step 1 of 6
- About
- Implementation
- Publish library publicly on GitHub with licensing and Docs — Step 2 of 6
- Bintray and Sonatype Setup — Step 3 of 6
- Bintray Implementation
- Sonatype Implementation
- Prepare Project for Upload — Step 4 of 6
- Prepare Library Module With Bintray
- Upload to jcenter— Step 5 of 6
- Why is jcenter better than maven central?
- Implementation
- Use In Project — Step 6 of 6
- Resources
My First App: How to Create Your First Android App Step by Step
To create a native Android app, one that can directly use all the features and functionality available on an Android phone or tablet, you need to use the Android platform’s Java API framework. This is the API that allows you to perform common tasks such as drawing text, shapes, and colors on the screen, playing sounds or videos, and interacting with a device’s hardware sensors. Over the years, the Android API framework has evolved to become more stable, intuitive, and concise. As a result, being an Android developer today is easier than ever—even more so if you use Android Studio, the official tool for working with the framework.
In this tutorial, I’ll show you how to create your first Android app. While doing so, I’ll also introduce you to important Android-specific concepts such as views, layouts, and activities.
We’ll be starting from scratch to create a very simple app in this tutorial. If you prefer writing less code or need to develop your app as quickly as possible, however, consider using one of the native Android app templates available on CodeCanyon.
Using an app template, you can have a polished, ready-to-publish app in just a matter of hours. You can learn how to use an Android app template by referring to the following tutorial:
Prerequisites
To be able to follow along, you’ll need:
- the latest version of Android Studio
- a device or emulator running Android Marshmallow or higher
If you don’t have Android Studio, do refer to the following tutorial to learn how to install and configure it:
1. Create a New Project
You’ll need an Android Studio project to design, develop, and build your app. So launch Android Studio and click on the Start a new Android Studio project button.
On the next screen, choose Add No Activity because we don’t want to use any of the templates offered by Android Studio. Then press Next to proceed.
You’ll now see a form where you can enter important details about your app, such as its name and package name. The name is, of course, the name your users will see on their phones when they install your app.
The package name, on the other hand, is a unique identifier for your app on Google Play. You must follow the Java package naming conventions while specifying it. For example, if your app’s name is MyFirstApp and you work for an organization whose website address is example.com, the package name would ideally be «com.example.myfirstapp».
Next, you must decide the programming language you want to use while coding the app. For now, select Java and press Finish.
Android Studio will now take a minute or two to generate and configure the project.
2. Create an Activity
An activity is one of the most important components of an Android app. It is what allows you to create and display a user interface to your users. An app can have one or more activities, each allowing the user to perform an action. For example, an email client app can have three activities: one for the user to sign up, one to sign in, and one to compose an email.
To keep this tutorial simple, we’ll be creating an app with just one activity. To create the activity, in the Project panel of Android Studio, right-click on app and select New > Activity > Empty Activity.
In the dialog that pops up, type in MainActivity as the name of the activity, check the Launcher Activity option, and press Finish.
Checking the Launcher Activity option is important because it is what allows your users to open the activity using an Android launcher. As such, a launcher activity serves as an entry point to your app.
3. Create a Layout
Each activity usually has at least one layout associated with it. When you created your activity in the previous step, you also generated an empty layout for it. To take a look at it, open the activity_main.xml file.
An activity’s layout primarily consists of views and view groups. A view, sometimes referred to as a widget, is an individual component of your user interface. Buttons, text fields, labels, and progress bars are common examples of views. A view group is a component that can serve as a container for views. Usually, view groups also help you position and set the dimensions of your views.
ConstraintLayout is one of the most powerful and flexible view groups available today. By default, it is the root node of your activity’s layout XML file. It looks like this:
We’ll be creating a simple clock app in this tutorial. In addition to the local time, it will be able to show the current time in two different countries: India and Germany.
To allow the user to choose the country they’re interested in, our layout will have two Button views, one for Germany, and one for India. And to actually show the time, our layout will have a TextClock view.
Accordingly, add the following code inside the ConstraintLayout :
Note that each view must have the layout_width and layout_height properties. They decide how large the view is. Other properties such as layout_constraintBottom_toBottomOf and layout_constraintLeft_toLeftOf are necessary to position the view. With the above code, the TextClock view will be placed at the center of the screen, and both the Button views towards the bottom of the screen.
By default, the TextClock view only shows the hours and minutes. The format12Hour property, however, allows you to change that. In the above code, its value is set to h:mm:ss a . This tells the TextClock view that it should display the hours, minutes, seconds, and also an AM/PM suffix.
Also note that each Button view has an onClick property. This property is used to assign click event handlers to the buttons.
The handlers don’t exist yet, but you can ask Android Studio to generate them for you. To do so, hover over the name of the handler until you see a red light bulb appear beside it. Then click on the light bulb and select the second option, the one with the yellow light bulb.
At this point, you can try pressing Shift-F10 to run the app. If there are no errors in your XML code, you should see something like this on your phone or emulator:
Although the buttons don’t work yet, the TextClock view should show the local time, updating itself every second.
4. Implement Event Handlers
When you generated event handlers for the two buttons, Android Studio added two methods to your activity’s Java file, MainActivity.java. If you open it, you should find the following code in it:
Inside the event handlers, all we need to do is change the time zone of the TextClock view. But how do you reference a view that’s in your layout XML file from inside your Java file? Well, you just use the findViewById() method.
Once you have a reference to the TextClock view, you can call its setTimeZone() method to change its time zone. So add the following code inside the onClickGermany() method:
Similarly, add the following code inside the onClickIndia() method:
If you’re wondering what R is, it’s an auto-generated class that contains, among other things, the IDs of all the views you have in your layouts. The findViewById() method expects you to use this class while passing an ID to it.
At this point, you can press Shift-F10 again to re-run the app. You should now be able to click on the buttons to change the clock’s time zone.
Conclusion
You just created your first fully functional, native app for Android! I encourage you to make a few changes to it. For instance, you could try using other time formats or time zones. You could also try changing the positions of the buttons and the clock view.
There are dozens of views and view groups you can use to create your apps. Do refer to the official documentation to learn about them.
Android App Templates and UI Kits From CodeCanyon
You may have noticed that our app looks very plain and simple. That’s because we’re using the default theme, without applying any styles to our views. CodeCanyon is full of Android UI kits that offer beautiful, hand-crafted styles you can apply to your views.
The kits generally also have several custom views and layouts. You can refer to the following articles to learn more about them:
Источник
Open Source Your Android Code — The Complete Guide
ADA | Adam Deconstructs Android
Aug 29, 2017 · 8 min read
9/17/17 Update: JitPack.io appears to be a fast and easy alternative to open source Android code vs. the method below by integrating directly with GitHub. I have not tested JitPack so please share your feedback in the comments if you’ve implemented it.
You’ve spent hours building cool shit. What’s the next step? By open sourcing your work you’ll (hopefully) provide valuable code to the Android community, receive constructive feedback, and collaborate on building something better than what you originally had.
The current state of open sourcing for Android is unintuitive, involving integrating multiple services, waiting for manual approvals, and before this post, spending hours Googling obscure steps. That’s why I created a beginning-to-end guide to expedite the process.
The more open sourced code, the better.
Implementation
I will walk through each step of how I open sourced a CustomRippleView library for Android.
Place code inside an Android Archive Library (AAR) — Step 1 of 6
Besides open sourcing, AARs are useful when building multiple apps or versions with the same components.
About
- Structurally the same as an Android app module
- Includes source code, resource files, manifest (unlike JAR)
- Compiles into Android Archive (AAR) rather than into APK
- Post to some maven repository where devs can pull it as a dependency through Gradle (can also convert an app to a module)
- Code Overlap — The app module will take precedence over a library if a resource ID is defined in both, library defined first will take precedence between libraries.
Implementation
If you’re creating a standalone library outside an existing app you’ll want to both create a new project to host the library module as well as test the library module in an existing app.
1. Build the open sourced library module in an existing project so that you can test the code as you go.
a) Create library module
Click the plus or File > New > Module > Android Library > provide unique Library Module Name ( customrippleview)
b) Ensure local library module shows in project and compile local library in the app module.
build.gradle ( app module)
Add tools:replace=”android:name” to the app module’s Manifest file.
2. Create a new Android project to host the open source code by itself so that it can be uploaded to bintray
Create the default app module with app following the name ( customrippleviewapp) to differentiate the app module name from the open source library module we’ll create in the next step.
- Application name: CustomRippleViewApp
- Company domain: com.ebay.customrippleviewapp ( needs to be a domain you own in order to get approved for open sourcing)
- Package name: com.ebay.customrippleviewapp
3. Add your library module (refer to step 1A above)
4. Place the open source code inside new library module created
5. Remove original app module
Right-click on app module > Open Module Settings > remove original app module.
6. Choose resources to make public (Optional)
All resources default to public: By declaring at least one resource public it makes the rest private
res > values > public.xml
Publish library publicly on GitHub with licensing and Docs — Step 2 of 6
Apache License 2.0 is one of the most popular, similar to the MIT License, but provides grant of patent rights from contributors to users. Apache 2.0 is commonly found in Android, Apache, and Swift.
Make sure library module ( customrippleview/) and build.gradle are not in the ignore list list and edit .gitignore to only contain library module files added.
Bintray and Sonatype Setup — Step 3 of 6
You only need to go through this painful steps once to setup your bintray account. Praise the lord! As this isn’t difficult, but the most annoying step.
Bintray Implementation
2. Create new repository
a) Add New Repository → Type: Maven → Default Licenses: Apache 2.0
b) Use lowercase naming convention: customrippleview
3. Enable auto signing
Enter Repository → Edit → General Settings → select GPG sign uploaded files automatically
a) Generate keys (Only done once for bintray account)
In terminal for project:
Fill in Real name, Email address, and passphrase. If command does not work, run following command to install gpg and retry the command above.
View keys created
Upload the public key to keyservers. Call the following command and replace PUBLIC_KEY_ID with value after 2048 in the pub line.
Export both public and private key.
Enter your passphrase when prompted for private key.
Copy and paste public and private keys into bintray: Under profile Edit > GPG Signing. Make sure to copy and paste from beginning and end tags or else bintray will not accept the keys.
Sonatype Implementation
This step requires filling a Jira ticket. If you thought you could escape Jira in your free coding time, you’re mistaken. It’s not too bad, as both times I’ve submitted a ticket they’ve approved it within the same day.
3. Provide bintray your Sonatype OSS username
In your bintray profile Edit > Accounts > Sonatype OSS User: _____________
Prepare Project for Upload — Step 4 of 6
Prepare Library Module With Bintray
1. Add Jcenter and Maven dependency
Add to project’s build.gradle (not app or library build.gradle)
2. Define your bintray username, api key, and GPG Passphrase.
This info should be secure, which is why we’re adding it to local.properties which should not be tracked in GitHub as it is commonly ignored at the start of an Android project in the .gitignore file.
3. Add repository information and build scripts
Update library’s build.gradle with repository information and add scripts for building library files and uploading the built files to bintray.
4. If using Kotlin in your code, disable Javadocs in library’s build.gradle
Upload to jcenter— Step 5 of 6
Why is jcenter better than maven central?
- Delivers library through CDN → faster loading
- Largest Java Repository on earth
- “Friendly” UI (perhaps in comparison)
Implementation
1. Upload to bintray/jcenter (Once Sonatype Open Source Project Repository Hosting request is approved)
Expected Result: BUILD SUCCESSFUL
Expected Result: BUILD SUCCESSFUL
I kept getting the BUILD FAILED response when attempting to upload. After many hours cursing at my terminal I realized even with this message, the package was being uploaded, so check the bintray package UI.
2. Sync to Jcenter for easy one line implementation in Android
3 hrs — How is this not automated too?!
a) Under the uploaded package settings select Add to JCenter
b) Select Host my snapshot…, fill in group id for package, and Select Send
Once approved, you’ll receive an email.
In the meantime you can check by searching on bintray which will also show when your package is hosted.
3. Maintaining library
Linking to jcenter only needs to be done once. Moving forward, any package changes (updates, deletes), will be reflected in jcenter 2–3 min later.
- Updates: Change the libraryVersion in library module and re-upload using Step 5, part 1.
- Deleting: Remove each version from bintray before removing the entire package.
Use In Project — Step 6 of 6
Declare the library in gradle and call the desired files.
build.gradle ( app module)
- Group_Id — com.ebay.customrippleview(package name followed by group name)
- Artifact_Id: customrippleview
- Version: 1.0
Resources
- JitPack.io — JitPack is an Android specific open sourcing solution that appears to work seamlessly with GitHub repositories. For a premium, JitPack also provides private library hosting. I haven’t given it a try, but thanks to Rakshak and Vikarti for calling it out in the comments below!
- The Cheese Factory Blog — How to distribute your own Android library through jCenter and Maven Central from Android Studio. BIG thanks! There were details that changed since this was published in 2015, but it was a great place to start.
- Android Studio — Create an Android Library
- bintray
- sonatype
- sonatype — request to host new open source project ( only works when logged in to sonatype)
- bintray documentation — Including your Package in JCenter
- CustomRippleView GitHub sample and JCenter hosting
I’m Adam Hurwitz — hit the clapping hands icon and check out the rest of my writing if you enjoyed the above | Thanks!
Источник