Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,366 members, 7,808,278 topics. Date: Thursday, 25 April 2024 at 09:46 AM

For Beginners: Learn How To Create A Simple Android Native App - Programming (4) - Nairaland

Nairaland Forum / Science/Technology / Programming / For Beginners: Learn How To Create A Simple Android Native App (18353 Views)

Java Tutorial For Beginners / Programming Challenge For Beginners N20000 / How To Make A Simple Calculator In Notepad Using .bat Format (2) (3) (4)

(1) (2) (3) (4) (Reply) (Go Down)

Re: For Beginners: Learn How To Create A Simple Android Native App by Untrollable: 11:47am On Dec 12, 2014
What about Material design? No one uses holo to develop no more
Re: For Beginners: Learn How To Create A Simple Android Native App by Tamunofiri(m): 12:14pm On Dec 12, 2014
Programmers needed to tutor and be trained in ERP in Port Harcourt. Send CV to joejobs14@gmail.com
Re: For Beginners: Learn How To Create A Simple Android Native App by timnah: 6:51am On Dec 13, 2014
please I need help from the house. I downloaded the ADT bundle from the official website but whenever I try to install the eclipse, it says "A java runtime environment (JRE) or java development kit (JDK) must be available in order to run eclipse. No virtual machine was found after searching the following locations: ...........javaw.exe in your current PATH". Please I really really need help!!! I'm losing my mind. lolz.
Re: For Beginners: Learn How To Create A Simple Android Native App by Idrisenagi: 8:10am On Dec 13, 2014
timnah:
please I need help from the house. I downloaded the ADT bundle from the official website but whenever I try to install the eclipse, it says "A java runtime environment (JRE) or java development kit (JDK) must be available in order to run eclipse. No virtual machine was found after searching the following locations: ...........javaw.exe in your current PATH". Please I really really need help!!! I'm losing my mind. lolz.
You need to install jre (Java Runtime Environment ) download the latest version , that will fix your problems.
Re: For Beginners: Learn How To Create A Simple Android Native App by timnah: 9:55am On Dec 13, 2014
Idrisenagi:

You need to install jre (Java Runtime Environment ) download the latest version , that will fix your problems.
Can you post a link please?
Re: For Beginners: Learn How To Create A Simple Android Native App by francoflex: 11:11am On Dec 13, 2014
Chaiii evil prophecy coming to pass, d thread has been derailed, and I was greatly learnin o *tears* wakka to all derail-ers. Mr Nolly pls induce smile on my face by continuein dis program and if dia s new location... My email fmomoh52@yahoo.com
Re: For Beginners: Learn How To Create A Simple Android Native App by nollyj: 1:42pm On Dec 13, 2014
francoflex:
Chaiii evil prophecy coming to pass, d thread has been derailed, and I was greatly learnin o *tears* wakka to all derail-ers. Mr Nolly pls induce smile on my face by continuein dis program and if dia s new location... My email fmomoh52@yahoo.com
You will find more tutorials http://inducesmile.com/blog/
Re: For Beginners: Learn How To Create A Simple Android Native App by nollyj: 1:48pm On Dec 13, 2014
I finished with the part three of this series of android tutorial with android application project folder. I went ahead to explain what each folder and file is used for and their functions.

Today we will take a quick look on android and important basic information that will help you understand in detail how to develop android application. I know that most people are more interested in the code than the theories behind how it works. But I want to remind you that this tutorial is a beginner’s android development tutorial so it is important for newbies in android development to understand how the system works

What is android?
I know that this question might sound fun but I also know that not everybody understand what android means. So I will go ahead and give a little excerpt from Wikipedia http://en.wikipedia.org/wiki/Android_(operating_system).

“Android is a mobile operating system (OS) based on the Linux kernel and currently developed by Google. With a user interface based on direct manipulation, Android is designed primarily for touchscreen mobile devices such as smartphones and tablet computers, with specialized user interfaces for televisions (Android TV), cars (Android Auto), and wrist watches (Android Wear). The OS uses touch inputs that loosely correspond to real-world actions, like swiping, tapping, pinching, and reverse pinching to manipulate on-screen objects, and a virtual keyboard. Despite being primarily designed for touchscreen input, it also has been used in game consoles, digital cameras, regular PCs (e.g. the HP Slate 21) and other electronics.”

Android Components
Since we have an idea on what android is, we will go further to explain components in android that help in android application development. They are the essential or rather the building blocks of android application. A full detailed about android component s is lift from google android page http://developer.android.com/guide/components/fundamentals.html

“Each component is a different point through which the system can enter your app. Not all components are actual entry points for the user and some depend on each other, but each one exists as its own entity and plays a specific role—each one is a unique building block that helps define your app's overall behaviour.

There are four different types of app components. Each type serves a distinct purpose and has a distinct lifecycle that defines how the component is created and destroyed.

Here are the four types of app components:

Activities
An activity represents a single screen with a user interface. For example, an email app might have one activity that shows a list of new emails, another activity to compose an email, and another activity for reading emails. Although the activities work together to form a cohesive user experience in the email app, each one is independent of the others. As such, a different app can start any one of these activities (if the email app allows it). For example, a camera app can start the activity in the email app that composes new mail, in order for the user to share a picture.
An activity is implemented as a subclass of Activity and you can learn more about it in the Activities developer guide.

Services
A service is a component that runs in the background to perform long-running operations or to perform work for remote processes. A service does not provide a user interface. For example, a service might play music in the background while the user is in a different app, or it might fetch data over the network without blocking user interaction with an activity. Another component, such as an activity, can start the service and let it run or bind to it in order to interact with it. A service is implemented as a subclass of Service and you can learn more about it in the Services developer guide.
Re: For Beginners: Learn How To Create A Simple Android Native App by nollyj: 1:53pm On Dec 13, 2014
Broadcast receivers
A broadcast receiver is a component that responds to system-wide broadcast announcements. Many broadcasts originate from the system—for example, a broadcast announcing that the screen has turned off, the battery is low, or a picture was captured. Apps can also initiate broadcasts—for example, to let other apps know that some data has been downloaded to the device and is available for them to use. Although broadcast receivers don't display a user interface, they may create a status bar notification to alert the user when a broadcast event occurs. More commonly, though, a broadcast receiver is just a "gateway" to other components and is intended to do a very minimal amount of work. For instance, it might initiate a service to perform some work based on the event. A broadcast receiver is implemented as a subclass of BroadcastReceiver and each broadcast is delivered as an Intent object. For more information, see the BroadcastReceiver class.”

There is one more thing we will look into before will end this tutorial. It is the android life cycle

Android Activity Lifecycle
You can find the information below on Google android manual pagehttp://developer.android.com/reference/android/app/Activity.html
“Activities in the system are managed as an activity stack. When a new activity is started, it is placed on the top of the stack and becomes the running activity - the previous activity always remains below it in the stack, and will not come to the foreground again until the new activity exits.

An activity has essentially four states:

If an activity in the foreground of the screen (at the top of the stack), it is active or running.

If an activity has lost focus but is still visible (that is, a new non-full-sized or transparent activity has focus on top of your activity), it is paused.

A paused activity is completely alive (it maintains all state and member information and remains attached to the window manager), but can be killed by the system in extreme low memory situations.

If an activity is completely obscured by another activity, it is stopped. It still retains all state and member information, however, it is no longer visible to the user so its window is hidden and it will often be killed by the system when memory is needed elsewhere.

If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or simply killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state.”



What next
There is no better time than now. We are going to pull up our sleeve and get our mind ready to start with the coding process. But before then, you can go outside and take some fresh air. Meet you in tutorial 5
Re: For Beginners: Learn How To Create A Simple Android Native App by trytillmake(m): 9:28pm On Dec 13, 2014
Next, following u real good, pls continue to the end and do not mind the derailers, "he that runs a race does not look back lest he falls"
Re: For Beginners: Learn How To Create A Simple Android Native App by nollyj: 1:07am On Dec 14, 2014
The time we are waiting for has come. This part 5 of our android tutorial will focus on programming our intro activity page for our app. If you have not read the previous post where we have discussed everything about android and went ahead to create a new android project you can do so by using this link ( http://inducesmile.com/android/android-tutorial-part-4-african-hairstyle-app/)

It is time to fire up your IDE. Once your IDE is done loading, open the android project you have created. We will start with the interface design for our android app.

There are different ways to create android application interface. You can use an external xml file that links to your activity page or through Java code.

In this tutorial we are going to use xml file. There are many advantages of using xml file over Java code. We will not go into details. Using xml file will prevent our activity page from being clustered with UI codes.

Open activity_main.xml
Remember that when we discussed about the android application folder structure, we emphasised what folders and files are used for. If you are using eclipse, your IDE has already created this file for you since it is the default activity automatically created by your project.

To open this file, go to your project res folder, open the res folder and then click on layout folder. You will see your activity_main.xml file when the layout folder opens. Double click on the file to open it in your IDE. If you have successfully followed this processes, by now you will see an image like this if you are using Eclipse IDE



Eclipse provides two interfaces when working with xml files. You can use the design view or the code view. The design view has all the components and controls used for interface design. You can drag and drop any control you want to use in the layout. The root layout is the parent contain that contains other layouts and controls. The image below shows the design interface.



Before we start with writing codes you should add the project images you downloaded in the previous lesson to your project drawable folders. I will also suggest you read up on android drawable if you do not have an idea on how android handles image responsiveness.
The first thing we will do in our xml file is to add a background image on the parent layout. I will use the code view in Eclipse for this tutorial. So to add a background image we simply add this line of xml code to the root layout

android:background="@drawable/intro"

Remember that our background image is named intro and it is present in the drawable folder in our project. We will add a button in front of our background image. When this button is click, it will take the use inside our application. We will use the button control provided by android to add a button in our project.

Copy and paste this code in your root layout.

<Button
android:id="@+id/start_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="20dp"
android:layout_marginRight="15dp"
android:background="@drawable/startbutton"/>

That is all the xml code we need on this page. By now your activity_main.xml file will look like the picture below



We will now proceed to the MainActivty page which will use our xml file for its interface display. Move to the src folder and double click on the MainActivity.java file. Delete all the code on the page except your package name. Copy and paste the following code below.


import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

public class MainActivity extends ActionBarActivity {
private static Button startButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);

startButton = (Button)rootView.findViewById(R.id.start_button);
startButton.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View view) {
// TODO Auto-generated method stub
Intent intent = new Intent(getActivity(), BraidsActivity.class);
startActivity(intent);
}
});
return rootView;
}
}
}


I assumed that you have a basic knowledge of Java language so I will not explain what import is and package name in Java application. Please not that I am using Fragment in this application. If your project does not include a Fragment then you should copy all the code in the Fragment onCreateView to your Activity onCreate callback function.

I will focus on the codes in onCreateView callback method of the inner Fragment class. Our interest is to add an event listener interface to the button we created so that when someone clicks on it something will happen. We will use the Activity class method findViewById and pass the reference id of our view to the method as shown here

startButton = (Button)rootView.findViewById(R.id.start_button);

The startButton is an instance of our Button class. So we will attach an event listener using the setOnClickListener method.
This is all the code for our intro Activity page. You can run your application now. If everything went well, you will see the following image in your device or android emulator.



This brings us to the end of our tutorial five. We will continue where we stop in our tutorial six which will be posted soon. But before then if you have any question or suggestion just bring it forward.
Re: For Beginners: Learn How To Create A Simple Android Native App by Ezyp(m): 5:47am On Dec 14, 2014
Nollyj its like what you wrote ^^ is hidden
Re: For Beginners: Learn How To Create A Simple Android Native App by paulenokels: 9:53am On Dec 14, 2014
here is a little chat app i created. Check out "Hookup Chat" - https://play.google.com/store/apps/details?id=com.softenokels.Hookup
Re: For Beginners: Learn How To Create A Simple Android Native App by Idrisenagi: 9:57pm On Dec 14, 2014
Re: For Beginners: Learn How To Create A Simple Android Native App by nollyj: 1:44am On Dec 15, 2014
Ezyp:
Nollyj its like what you wrote ^^ is hidden
Yes, 24 hours ban
Re: For Beginners: Learn How To Create A Simple Android Native App by nollyj: 9:13pm On Dec 15, 2014
The time we are waiting for has come. This part 5 of our android tutorial will focus on programming our intro activity page for our app. If you have not read the previous post where we have discussed everything about android and went ahead to create a new android project you can do so by using this link

It is time to fire up your IDE. Once your IDE is done loading, open the android project you have created. We will start with the interface design for our android app.

There are different ways to create android application interface. You can use an external xml file that links to your activity page or Java code.

In this tutorial we are going to use xml file. There are many advantages of using xml file over Java code. We will not go into details. Using xml file will prevent our activity page from being clustered with UI codes.


Open activity_main.xml
Remember that when we discussed about the android application folder structure, we emphasised what folders and files are used for. If you are using eclipse, your IDE has already created this file for you automatically when your project is created.

To open this file, go to your project res folder, open the res folder and then click on layout folder. You will see your activity_main.xml file when the layout folder opens. Double click on the file to open it in your IDE.

If you have successfully followed this processes, by now you will see an image like this if you are using Eclipse IDE



Eclipse provides two interfaces when working with xml files. You can use the design view or the code view. The design view has all the components and controls used for interface design. You can drag and drop any control you want to use in the layout.
The root layout is the parent contain that contains other layouts and controls. The image below shows the deign interface.



Before we start with writing codes you should add the project images you downloaded in the previous lesson to your project drawable folders. I will also suggest you read up on android drawable if you do not have an idea on how android handles image responsiveness.
The first thing we will do in our xml file is to add a background image on the parent layout. I will use the code view in Eclipse for this tutorial. So to add a background image simplyadd this line of xml code to the root layout

android:background=”@drawable/intro”

Remember that our background image is named intro and it is present in the drawable folder in our project.
We will add a button in front of our background image. When this button is click, it will take the use inside our application.
We will use the button control provided by android to add a button in our project.
Copy and paste this code in your root layout.

<Button
android:id=”@+id/start_button”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentBottom=”true”
android:layout_alignParentRight=”true”
android:layout_marginBottom=”20dp”
android:layout_marginRight=”15dp”
android:background=”@drawable/startbutton”/>

That is all the xml code we need on this page. By now your activity_main.xml file will look like the picture below



We will now proceed to the MainActivty page which will use our xml file for its interface display. Move to the src folder and double click on the MainActivity.java file. Delete all the code on the page except your package name. Copy and paste the following code below.
<code>
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

public class MainActivity extends ActionBarActivity {

private static Button startButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
startButton = (Button)rootView.findViewById(R.id.start_button);
startButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
Intent intent = new Intent(getActivity(), BraidsActivity.class);
startActivity(intent);
}
});
return rootView;
}
}
}
</code>
I assumed that you have a basic knowledge of Java language so I will not explain what import is and package name in Java application. Please not that I am using Fragment in this application. If your project does not include a Fragment then you should copy all the code in the Fragment onCreateView to your Activity onCreate callback function.


I will focus on the codes in onCreateView callback method of the inner Fragment class. Our interest is to add an event listener interface to the button we created so that when someone clicks on it something will happen.

We will use the Activity class method findViewById and pass the reference id of our view to the method as shown here

startButton = (Button)rootView.findViewById(R.id.start_button);

The startButton is an instance of our Button class. So we will attach an event listener using the setOnClickListener method.
This is all the code for our intro Activity page. You can run your application now. If everything went well, you will see the following image in your device or android emulator.



This brings us to the end of our tutorial five. We will continue where we stop in our tutorial six which will be posted soon. But before then if you have any question or suggestion just bring it forward.
Re: For Beginners: Learn How To Create A Simple Android Native App by trytillmake(m): 5:51pm On Dec 17, 2014
Oga this tuturial has been on since september, by nw you should have gone past this
Re: For Beginners: Learn How To Create A Simple Android Native App by Nobody: 9:54am On Jan 03, 2015
This tutorial don enter new year and nothing much as been covered.

Nollyj, we still dey wait
Re: For Beginners: Learn How To Create A Simple Android Native App by nollyj: 10:32am On Jan 03, 2015
phantonce:
This tutorial don enter new year and nothing much as been covered.

Nollyj, we still dey wait

I keep getting ban by Nairaland bot whenever I post more than three links. There is no way I can write this tutorial without image link, download link and other links. You can check by blog for updated version of the tutorial here - http://inducesmile.com/android/android-tutorial-part-4-african-hairstyle-app/

For the main time I will try and see how I can reduce the links to one or two in each post. More will be coming soon
Re: For Beginners: Learn How To Create A Simple Android Native App by Nobody: 11:03am On Jan 03, 2015
nollyj:


I keep getting ban by Nairaland bot whenever I post more than three links. There is no way I can write this tutorial without image link, download link and other links. You can check by blog for updated version of the tutorial here - http://inducesmile.com/android/android-tutorial-part-4-african-hairstyle-app/

For the main time I will try and see how I can reduce the links to one or two in each post. More will be coming soon

I've even read the part 5.
Nice job.
Re: For Beginners: Learn How To Create A Simple Android Native App by Borwe: 6:27pm On Jan 08, 2015
Why not create android programming group in whatsapp for people who want to start android programming and are probably stuck in some small basic code. It would be alot helpful to get help from someone on the go.
Re: For Beginners: Learn How To Create A Simple Android Native App by nollyj: 9:24pm On Jan 08, 2015
Borwe:
Why not create android programming group in whatsapp for people who want to start android programming and are probably stuck in some small basic code. It would be alot helpful to get help from someone on the go.

I have a chat application on my website and I am always online. So people can easily reach me if they have programming related questions http://inducesmile.com/blog/
Re: For Beginners: Learn How To Create A Simple Android Native App by Borwe: 10:05pm On Jan 09, 2015
nollyj:


I have a chat application on my website and I am always online. So people can easily reach me if they have programming related questions http://inducesmile.com/blog/

Hmm, can't see the so called application, anyways, about the inter activity communication tutariol you did, I tried running mine with min and max sdk set to 10 and compiler of android 2.3.3 and whenever I click the button to go to the next activity it crashes, I think that method doesn't work gingerbread or lower. please assist.
Re: For Beginners: Learn How To Create A Simple Android Native App by nollyj: 11:24pm On Jan 09, 2015
Borwe:


Hmm, can't see the so called application, anyways, about the inter activity communication tutariol you did, I tried running mine with min and max sdk set to 10 and compiler of android 2.3.3 and whenever I click the button to go to the next activity it crashes, I think that method doesn't work gingerbread or lower. please assist.

can you show the error you are getting?
Re: For Beginners: Learn How To Create A Simple Android Native App by Nobody: 8:32am On Jun 26, 2015
nollyj:


can you show the error you are getting?


Just.StumbleUpon this thread.
I realized you faced lots of challenges in trying to teach people here.Challenges like those that does not want u to teach newbies.

My ad to you is sign on git and broadcast your I.d here.
People can follow your tut on githup on the command line interface.It's better for u
Re: For Beginners: Learn How To Create A Simple Android Native App by sureshdasari: 9:32am On Nov 17, 2018
Re: For Beginners: Learn How To Create A Simple Android Native App by nurain150(m): 10:15am On Nov 17, 2018
You said native app my own mission is to use cpp and c in Android I no like Java
Re: For Beginners: Learn How To Create A Simple Android Native App by Fastlinkpro: 3:54pm On Nov 17, 2018

(1) (2) (3) (4) (Reply)

I Need Help On How To Avoid Burnout / Why I Dumped Wordpress As A CMS (my Experience) / Coding Looks Cool.

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 84
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.