Multi-Language Supported Android Application

Multi-Language App :

Android support multi-language in application for different country peoples languages like  Hindi, English , French etc.If you want to go for globally successfully android application then your application should be support Multi-Languages. In This tutorial we are explaining how you can create multi-language app.

How to Multi-Language Supported Android Application?

1.Create new project in Android Studio (for creating new android project follow this tutorial …Android Studio First Application)

2.Now Create new Values folder for different languages. app-> src-> main-> res (right click on res and create folders ..values-fr and values-hi.

Multi-Language Supported Android Application

3. Create strings.xml inside values-fr Folder and add this code.

4. Create strings.xml inside values-hi Folder and add this code.

5. Add following string in default values->strings.xml

6. Open layout file activity_main.xml (res->layout -> activity_main.xml) and add this code.

 

7. Now last step do code in MainActivity.java (src->packagename->Mainactivity.java)

Output : Multi-Language Supported Android Application

Multi-Language Supported Android Application

Download Source Code :

Official android document http://developer.android.com/training/basics/supporting-devices/languages.html

Read More

Android First Program in Android Studio

Android Studio :

Android Studio First App

Android Studio is IDE for Android application development (integrated development environment ). its support cross operating systems Windows, OS X and Linux. Android Studio build system for android app is Gradle build systems.

Feature of Android Studio :

  • Gradle build system
  • Maven-based build dependencies
  • Build variants and multiple-APK generation
  • Graphical layout(design) editor

Start Android Studio First app development :

1. Download and Install Android Studio IDE and SDK (download android studio)

2. Start Android Studio , If Welcome screen come choose “Start a new Android Studio project

otherwise Go File -> New -> New Project.

Android first app in android studio 06

3. New Project window will be show.

Enter Application name and Company Domain(Package name) then click next

Android first app in android studio 06

4. Target Android Devices Window will be show

Select Minmum SDK (API 9 : Android 2.3 Gingerbread) and other options as below screen shot.

click Next.

Android first app in android studio 06

5. Add an Activity to Mobile window will be show.

Select Blank Activity then click next.

Android first app in android studio 06

6. Customize the Activity window will be show :

here you can change Activity Name ,Layout Name ,Title and Menu as per you otherwise leave it same.

Click Finish.

Android first app in android studio 06

7. Android Studio IDE project window will be open.

Now your first android application ready.

 

Android first app in android studio 06

How to Create Emulator (Android virtual machine -AVD) in Android Studio :

1. In Android Studio IDE click on AVD Manager (as show in bellow screen shot).

Android Studio AVD Manager 07

2. Android Virtual Device Manger window will be show.

Click on “Create Virtual Device“.

Android Studio AVD Manager 07

3. Virtual Device Configuration window will be show.

Select Device type ( 3.2″ HVGA slider) or as your requirement  then click next.

Android Studio AVD Manager 07

4. System Image window will be show.

Select your installed System image (Lollipop select for this example). Click next.

 

Android Studio AVD Manager 07

5. Android Virtual Device window will be show.

here you can give AVD Name (My Device or as per you requirement) and click Finish.

Android Studio AVD Manager 07

6. Now you can see your new AVD is show in Android Virtual Device Manger.Your AVD is ready now close this windows.

Android Studio AVD Manager 07

Finally Run your First Android Studio Project :

1. Click on Run , Choose device window will be show.

Select you device (earlier created- My Device) then click ok

Android Studio AVD Manager 07

2. Your application launched (some time emulator will be take too long to launch)

Output of First program:

Android first app in android studio 06

Video Tutorial : How to Create First Android Application in Android Studio


Read More

Android Fragment Tutorial

Android Fragment :

A Fragment represents a behavior or a portion of user interface in an Activity. Fragments mus be embedded with activities, they can not run independent.Single Activity can use multiple fragments and fragment usable in several activities.Each fragment has its own lifecycle, closely associated with the lifecycle of its host activity. a fragment may be a static a part of an activity or instantiated automatically during the activity’s creation or you can create(), add(), and remove() fragments dynamically in an activity at run-time.

Android Fragment tutorial example

Solution Steps Android Fragment : Implemented in Honeycomb (3.0) or Later

The primary classes related to fragments are:
android.app.Fragment
The base class for all fragment definitions
android.app.FragmentManager
The class for interacting with fragment objects inside an activity
android.app.FragmentTransaction
The class for performing an atomic set of fragment operations

1. Create Android New project (Click here for setup new Android project).

2. Add Following xml code into activity_main.xml

File : res/layout/activity_main.xml

In this example we are using two Buttons One for FragmentOne or second for FragmentTwo and one Fragment Layout for show fragments.

3. Create “fragment_one.xml” file and add following code.

File : res/layout/fragment_one.xml

4. Create “fragment_two.xml” file and add following code.

File : res/layout/fragment_tow.xml

5. Open “FragmentOne.java” file and add following JAVA code.

File : src/package-name/FragmentOne.java

Initialize fragment_one layout:

5. Open “FragmentTwo.java” file and add following JAVA code.

File : src/package-name/FragmentTwo.java

Initialize fragment_two layout:

6. Open “MainActivity.java” file and add following JAVA code.

File : src/package-name/MainActivity.java

In tutorial by default FragmentOne displayed , after click Button One or Two fragment it will display respective fragment layout.

 

Output of  Android Fragment Example :

Android fragment tutorila example

Note : This example (Project) is developed in Eclipse Version Kepler,tested on Android 4.4.4 (KitKat Version) and 5.0 (lollipop) and SDK build-tools 24.1

MinSdkVersion=”14″

TargetSdkVersion=”21″

Download Android Fragment example  Source code :

Read More

Android Camera API Tutorial

Android Camera :

Most Android phones today include inbuilt cameras — one at the front and one at the rear. This example shows you the way to programmatically invoke the camera to take an image and then save the image to external storage.In this Android tutorial you will learn how to use in-build android camera to take picture.

Android camera tutorial example

Solution Steps Android Camera :

In this example we will use Android camera api  (Existing android camera application api), which is use existing camera application installed on your phone.You are using an Intent object to invoke the camera, using the MediaStore.ACTION_IMAGE_CAPTURE action.

1. Create Android New project (Click here for setup new Android project).

2. Add Following xml code into activity_main.xml

File : res/layout/activity_main.xml

3. Open “MainActivity.java” file and add following JAVA code.

File : src/package-name/MainActivity.java

Here, you’re using an Intent object to invoke the camera, using the MediaStore.ACTION_IMAGE_CAPTURE action. At the same time, you’re telling the camera that you want to save the captured image to external storage and name the saved image MyPhoto.jpg.

Finally, you call the startActivityForResult() method to fire the Intent object. This ensures that after the picture has been saved, its path are displayed using the Toast class. this is often done through the onActivityResult() callback function.

4. Open “AndroidManifest.xml” file and add <uses-feature>.

you need to add the <uses-feature> element to your AndroidManifest.xml file:

File : ProjectName/AndroidManifest.xml

To ensure that your application can only be installed on devices with a camera(it will not work with Android emulator).

Output of  Android Camera Api access Example :

Android Camera Tutorial example

Note : This example (Project) is developed in Eclipse Version Kepler,tested on Android 4.4.4 (KitKat Version) and SDK build-tools 24.1

MinSdkVersion=”14″

TargetSdkVersion=”21″

Download Android Camera Api example  Source code :


Read More

Android Internal Storage: Read and Write text file example

Android Internal Storage :

In Android you can also save files (text file etc) directly on the device’s internal storage. By default,Saving and Loading Files to the internal storage are private to your application and different applications will not access them (nor can the user). once the user uninstalls this application, these files will automatically remove.

android read and write text file example

In Android data storage, It’s good practice to use Shared Preferences or a database to store your application data, but there are still times once you’ll need to use files directly rather than rely on Android’s managed mechanisms.

Sometime want to save data which have not be best represented as key/value pairs, then you may need to use the primitive method of saving it on to the filesystem as files. Saving data in text file is helpful for saving long strings of text, or binary data etc in your application.

Solution Steps Write and Read Text File :

As well as the standard Java I/O classes and methods, android also offers openFileInput and openFileOuput to modify reading and writing streams from and to local files. In  android to create a file and saving, you can use the openFileOutput() method and openFileInput() method to open a file for reading:

1. Add Following xml code into activity_main.xml

File : res/layout/activity_main.xml

2. Open “MainActivity.java” file and add following JAVA code.

File : src/package-name/MainActivity.java

For writing text to a file, use the OutputStreamWriter class and use its write() method to write a string to the file. To save the changes to the file, use its close() method:

 

Output of  Android Read Write Text File in emulator :

android read and write text file example

Note : This example (Project) is developed in Eclipse Version Kepler,tested on Android 4.4.2 (KitKat Version) and SDK build-tools 23.0

Download Android Custom Read Write Text File Source code :


Read More

Android Custom Listview with Image and Text using ArrayAdapter

Custom Listview with Image and Text :

The standard ListView only displays a list of items. while it may suffice when you simply need to show a list of text, it’s not enough for more refined applications. in this example, you’ll see how you’ll be able to customise the ListView so that you’ll be able to show pictures, multiple lines of text, and more.

custom listview image text

A. First Learn How to Display Image in ListView :

1. The first thing you’ll learn in this example is how to show a picture next to each item in the ListView. To do that, Create an XML file in your res/layout folder and name it, for example, mylist.xml

custom listview image text

 

2. Now Open “res/layout/mylist.xml” file and put this xml code it into file:

Above code will show each row will contain an ImageView and a TextView.

3. To display a ListView in your activity, you need to add the <ListView> element to your UI, such as the activity_main.xml file:

File : res/layout/activity_main.xml

1. Open “MainActivity.java” file and add following JAVA code.

File : src/package-name/MainActivity.java

Note that instead of using one of the default layout types, in this example you are using your own self-defined layout:

Output of  Android Custom ListView with Image in emulator :

custom listview image text

You can see each row have same icon If you want different icons or images for each row, you must need to create your own custom adapter. In next part B shows you how to achieve this.

Download Android Custom List with Same Image Source code :


B. How to Create Custom Adapter of Display Image and TextView in ListView :

For better understanding Create new Project and Follow this steps :

1. Create an XML file in your res/layout folder and name it, for example, mylist.xml. (As previous Part)

2. Now Open “res/layout/mylist.xml” file and put this xml code it into file:

The preceding layout contains an ImageView and two TextViews.

3. To display a ListView in your activity, you need to add the <ListView> element to your UI, such as the activity_main.xml file:

File : res/layout/activity_main.xml

4. Add 8 images to the res/drawable-mdpi folder (see Figure ).

custom listview image text

5. To populate each row using this newly created layout, you need to create a new custom array adapter. To do that, add a new Java class to the project and name it CustomListAdapter.java (see Figure).

custom listview image text

6. Open “CustomListAdapter.java” file and add following JAVA code.

File : src/package-name/CustomListAdapter.java

The CustomArrayAdapter class extends the ArrayAdapter base class.

7. To display the ListView using the new layout, add the following code to the MainActivity.java file:

File : src/package-name/MainActivity.java

 

Output of  Android Custom ListView with Image & Text in emulator :

custom listview image text

Download Android Custom List with Image and Text Source code :

Note : This example (Project) is developed in Eclipse Version Kepler,tested on Android 4.4.2 (KitKat Version) and SDK build-tools 23.0

Read More

Android Gallery View – Displaying a List of Images

Android Gallery View :

In your application if you want display a series of images to the user, you can make use of the Gallery.The Gallery is a view that shows items (such as images) in a center-locked, horizontal scrolling list.

android Gallery View example 2

How to Create Android Gallery View:

  • Gallery is used to show Views in a horizontal list, and user will select a view , User selected view will be shown in center of the Horizontal list
  • The items of Gallery are get from an Adapter, just like ListView, in which ListView items are get from an Adapter.
  • We need to make an Adapter class that extends BaseAdapter class and override  getView() method.
  • getView() method known as automatically for all items of Gallery

Solution Stuff :

1. For Android Gallery View Example, assume you have some images stored in the res/drawable-mdpi folder of your project (see Figure).

android Gallery View example
2. Create an XML file named attrs.xml and store it in the res/values folder (see Figure).

android Gallery View example13. Add  this content to the attrs.xml file:

File: res/values/attrs.xml

4. Open “res/layout/activity_main.xml” file and put this xml code it into file:

To use the Gallery, add the <Gallery> element in your UI, such as the activity_main.xml file:

File : res/layout/activity_main.xml

5. Open “MainActivity.java” file and add following JAVA code.

File : src/package-name/MainActivity.java

In Android Gallery View , you need create the ImageAdapter class which extends the BaseAdapter class.Which will bind to the Gallery view with a series of ImageView views. The BaseAdapter class will work as a bridge between an AdapterView and also the data source that feeds data into it.

For the ImageAdapter class, you implemented the following methods:

  •  getCount()
  •  getItem()
  •  getItemId()
  •  getView()

When a picture in the Gallery view is selected, the selected image’s position (0 for the first image, 1 for the second image, and so on) is displayed and the image is displayed in the ImageView.

 Output of  Android  Gallery View Example in emulator :

android Gallery View example 3

Download Android Gallery View example Source code :

 Note : This example (Project) is developed in Eclipse Version Kepler,tested on Android 4.4.2 (KitKat Version) and SDK build-tools 23.0

Read More

Android Toast – Creating a Android Custom Toast

Toast in Android :

Toast is transient notifications that stay visible for less than a few seconds before fading out. Toast don’t take focus and its not a modal , so that they don’t interrupt the application.You can also use it for debugging purpose. A toast provides simple feedback about an operation in a small popup.

android-custom-toast-example

Toasts are excellent for informing your users of events without forcing them to open an Activity or browse a Notification. they provide a perfect mechanism for alerting users to events occurring in background Services without interrupting foreground applications. Generally, your application should show Toasts only if one of its Activities is active.

Creating Simple (Default) Android Toast :

The Toast class includes a static makeText method that makes a standard Toast display window.

1. Open “MainActivity.java” file and add following JAVA code.

File : src/package-name/MainActivity.java

2. Open “res/layout/activity_main.xml” file and put this xml code it into file:

File : res/layout/activity_main.xml

In Android create a new Toast, pass the current Context, the text message to show, and also the length of your time to show it (LENGTH_SHORT or LENGTH_LONG) into the makeText method. once making a Toast, you’ll be able to show it by calling show.

Output of  Android Default Toast in emulator :

android simple toast example


Creating a Custom Toast View :

Sometimes a simple text message is not enough, you’ll be able to make a customized layout for your toast notification. To make a custom layout, define a view layout, in XML or in your application code, and pass the root view object to the setView(View) method.

1. Create “res/layout/mycustom_toast.xml” file and put this xml code it into file:

File : res/layout/mycustom_toast.xml

You can also create  new  border color, radius etc,

2. Create “res/drawable/mycustom_toast_border.xml” file : (as show in figure)

android custom toast example 1

File : res/drawable/mycustom_toast_border.xml

Now add code for create Custom Toast in java, that Layout of the Toast is “mytoast.xml” call by View.

3. Open “MainActivity.java” file and add following JAVA code.

File : src/package-name/MainActivity.java

That Code contain 4 Toast Codes

Output of  Android Custom Toast in emulator :

android custom toast example

 

Download Android Simple and Custom Toast example Source code :

Note : This example (Project) is developed in Eclipse Version Kepler,tested on Android 4.4.2 (KitKat Version) and SDK build-tools 23.0

Read More