Showing posts with label Android Development. Show all posts
Showing posts with label Android Development. Show all posts

[Android development] 6. Data transfer through activites using intents- Explicit intents part 2

Android dev 6

Learn how to transfer data to and from an activity in android using Intents.

My Demo application consists of 2 activities
  1. MainActivity
  2. newactivity

Content: 
In MainActivity, there is layout which contains, a Text View to display 'Activity 1. and an Edit Text to enter the text which you will be transferring to new_activity. and a 'click me' Button on click of which, the second activity should start with the data in the edit text packed and sent to the second activity through an intent.

Code for the Java class 'MainActivity.java' is as follows,

Code for the Layout of MainActivity - 'activity_main.xml' is as follows,


In new_activity, there is a Text View which will display the data received from the MainActivity. and a 'Send back the data' button, on click of which "Data successfully returned" string is sent back to the MainActivity and is displayed in the Edit Text. (this is receiving data)

Code for the Java class 'newactivity.java' is as follows,


Code for the Layout of new_activity - 'activity_newactivity.xml' is as follows,



Some Functions used in the code and its brief explanation:
  • PutExtra(String id, value):
Use the 'putExtra(string id,value)' function. id should match during extraction process. Learn more here.


  • StartActivityforResult(Intent, Request_code)
This calls an intent and starts an activity and when returning, will handle the result sent back from the called activity. Received data should be handled in 'OnActivityResult()' method. see below. Learn more about 'StartActivityforResult()'.


  • OnActivityResult(int request_code, int result_code, Intent data)
This will handle the result returned back from an activity which was called for a result using 'StartActivityforResult()' method. Parameters are 'request code' , 'result code', and 'data returned'. If the result was successful, result code will be 'RESULT_OK'. Learn more about each parameter and this function here.

  • setResult(RESULT_CODE, RESULT_INTENT)
This is to set the result to an intent and send it back to the called activity which is handled there in 'OnActivityResult()'. two parameters here. One is the result code. That is 'RESULT_OK' if the result was successful. and the second parameter is 'Intent' which contains the data to be returned. Learn more about this here.

  • finish()
This function finishes the current activity.


Watch the video demonstration at my YouTube channel.




Queries will be cleared if comments are dropped. ;)

[Android development] 5. Explicit Intents - part 1

Explicit  intents-1

Hey reader,
In this post, I will tell you about Explicit Intents in android. Implicit intent will be covered in the coming post.

In order to know about intents, you need to first know about ACTIVITY. Activity is a app component which can represent a single layout on the android screen at a time. It simply represents a screen in the android device.
Learn more about Activity from Google developer site.

      There can be many activities in an application. When we use many activities (screens), we need to switch between these screens according to some conditions. This is where Intents come into the picture.
   
      Intent is a communicator or messenger within your app. It can perform many operations like
1. Start a new activity from current activity
2. Start a new service.
3. Transfer data between activities in the app and many more.

Learn more about intents on Google developer site.

There are two types of intents,
 1. Implicit intents: Used to perform general tasks like sending a message/broadcast, from other apps, from within your app.
2. Explicit Intents: Used to shift between activities within your own app.

We will be using Explicit intent to start a new activity in this tutorial.

Follow the steps:

  • Open the 'Hello world' Application that you created during the previous tutorial '3. Creating a 'HELLO WORLD' app [Android development]'.
  • 'Create a New activity' by right clicking on the 'java' folder in the 'project pane' on the left side of your android studio screen. Give the name as 'newactivity'.
  • Alter the MainActivity.java as given below,
  • Alter the activity_main.xml file to:

  •  MainActivity's layout includes a TextView which prints 'Activity 1' and a Button which shows 'Click me', on click of which we should navigate to second activity.
  • Type the following into newactivity.java:

  •  Type the following in activity_newactivity.xml:



This Code includes a component called 'Toast'. Learn more about toast messages in the coming tutorials.

Video Tutorial for this post at 'Codeshuffle YouTube'



Cheers!!

[Android development] 4. Building a signed .APK file

 Building a signed .APK file


Hey folks!
Welcome back to the new tutorial. This is going to be about  'How to generate a signed .APK file" for the android application that you have created. In this tutorial im going to create to generate signed apk of the 'Hello World' app that we studied in the 'Previous tutorial'.
 
All you have to do is follow the simple steps given below:

1. Open the android project and click on Build>Generate signed apk 
     (as shown in the picture below)

generate a signed apk


2. Create a 'Key store' and sign your application
'Key store' is just like a signature for your application.
Learn more about Key Store from 'here'.

Note:
  • Save the key in a safe location and dont forget the details you entered about the key store, like passwords and other details (This will be needed if you publish your app in the Google play store).
  • Do not ever forget the password of the key store, since the key is of no use without the password.
  • Do not share this file with anyone who doesn't own the application, because this is your unique signature for the application. 
If you have not created key already, click on 'Create new' button and create one as shown in the picture below.
key


key store


3. Finish this last step
After you have created the key and filled all the other information, click on 'NEXT' .

In the next screen, select the location where the .APK should be produced. Select the release type as 'Release' and click on 'FINISH'.

Wait for the project to get built and after the build is done, you will see the apk file in the selected destination folder.


Video tutorial for this post on 'CodeShuffle YouTube' 





That's all, you have generated your first apk now. Got queries? Drop comments.. let me solve it :)


[Android development] 3. Creating a 'HELLO WORLD' app

'HELLO WORLD' app

Hey readers, in this post lets learn how to create our first andriod app which displays 'HELLO WORLD'.

Follow the steps:
  1. open 'ANDROID STUDIO'
  2. Start a new android project and give the appropriate names in the fields which will be asked.
  3. Locate the generated activity file, which in my case is 'ActivityMain.java', and enter the following code,
 Notes:
  •    onCreate function : This function gets called when your activity is started. Learn more about onCreate in stackoverflow. 
  •    setContentView: This sets the layout for the activity, parameter to be passed is the layout's resource id, something in the form of 'R.layout.name'. Learn more about this here.
 

    4. locate the xml file associated with the activity's java class, which in my case is 'activity_main.xml'. Enter the following code

 Notes:
  • RelativeLayout: A layout type, learn more here. There are also other type of layouts, check them out here
  • TextView: A view item which displays a text on the layout screen. Learn more here
After your coding is done, Just click on 'RUN APP' at the top toolbar in the android studio.
Select an emulator and Run on it. [To know how to setup emulator, read this post
Have a look at our video tutorial for this post on 'CodeShuffle YouTube'

 In the next post, lets learn 'How to build a signed apk' of your android project.
 

[Android development] 2. Creating and running Emulators for testing apps

 android dev 2

Welcome back guys,
In this tutorial lets learn about Emulators/Virtual Devices

Emulator is a software that can run a virtual device or software that is actually designed to work in other Operating system. Learn more about Emulator in Wikipedia - Click Here.

In our case, we need to test and run android applications in the emulator, so we need an Android Emulator in the windows operating system. Lets get started,

Follow the Steps for creating Android Virtual device (AVD) :-
open avd manager
  1. Open Android studio Home screen.
  2. Click on 'Start a new Android Project' option. ( Since you cant access AVD Manager settings directly through the Android studio home screen ).
  3. Keep on clicking 'Next', don't change any names in any windows(Time waste). at last click on 'Finish'.
  4. Your temporary project will get loaded in some time, wait until then. 
  5. After your project files are loaded ( in this case , a 'Hello World' code which comes by default in the Android studio when you create a new project ), Go to 'AVD Manager' which can be found at the top toolbar of the Android studio. (See the screenshot below).
  6. It will launch the AVD Manager.
  7. Then Click on 'Create Virtual Device' option. (See the screenshot below)
    create avd
     
  8. Fill in the details of your new Virtual device (such as name, screen size, screen density, RAM Value etc)
  9. Change the RAM Value to 1GB(Minimum), for good performance of the emulator.
  10. Click on 'Save'.
  11. It will save the Device in some time.
  12. Launch the Emulator by clicking the play button in the actions column of the device list.
  13. It will take some time to launch the AVD, since it is the first run (like any new android smartphone would take)
  14. After the emulator has started, the next thing to do is test your Default Hello world application in it.
  15. Go to Android Studio and click on 'Run App' button which can be found at the top toolbar of the Android Studio.
  16. Android studio will check for errors in the code(if any), and if there are no errors, it will launch the app in the emulator successfully. 
  17. you can deal with the virtual device like any other android smartphone.  
 Look at the Video Tutorial of this post in YouTube




 That's it. You are ready with your Emulator. In the next tutorial We will learn to create a 'Hello World' Application in Android studio.
Keep visiting...

[Android development] 1. Setting Up Android Studio

Setting Up Android Studio


This first post in the android application development tutorial is about how to install android studio in windows and set up your PC for android development.
      

All you have to do is just the 3 steps below.

1. Installing the Appropriate JDK ( Java development kit ).
     This is a very basic thing needed for the development. This JDK includes a private JVM and a few other resources to finish the development of a Java Application. Since Android programming is done in JAVA language, this is the primary necessity for the Android Studio.

[NOTE: Install the appropriate JDK, i.e., If you are using a 32 bit operating system download JDK for 32bit only. and for 64bit Operating systems download JDK 64 bit version. Otherwise the JDK wont work.]

Learn more about JDK on Wikipedia.

Download link given below.

2. Creating an Environment variable.
   The next step,
    What's an Environment variable? The question may come to your mind. Answer is,
    These are the dynamic declared values that affect the way running processes on a computer. They are part of the environment in which a process runs the operating system.
 
    You should create an environment variable to refer to the JDK you had just installed. Its done as follows:
  • Go to 'My Computer' in your Windows PC.
  • Right click inside My Computer and go to properties.
  • In the left side tab, you will find 'Advanced system settings'. click on that option.
  • You should find 'Environment Variables' option here. click on that option.
  • There will be two sections, namely 'User variables' and 'System variables'. Navigate to 'User variables' and click on 'NEW'.
  • It will ask for two values. Enter the following values
  • Variable Name: JAVA_HOME.
  • Variable value: Path of JDK you just installed. ( find it in C:/Program files/java/jdk . here)
  • Select 'OK' and close the windows.
You are Done with creating the environment variable. 


 
3. Installing Android Studio setup.
    This is the third and probably last step. Download the Setup from the link given below and install it. Normal Installation but make sure to increase the RAM value of the Emulator which will make your Application testing pretty much faster.

[Note: Increase the emulator RAM Value during the setup, If you use a system with 4GB or less RAM, set it to 1GB/1024MB. Else you can set it up to 2024MB. This will speed up your Emulator processing. Learn more about 'Emulators' on Wikipedia.]



Watch the video tutorial for this first on 'CodeShuffle YouTube'.





Download links:
  1. Android Studio
  2. Java Development kit 
[Note: Make sure to install the proper JDK according to your System (32 bit / 64 bit) .]