Menus

Saturday, 2 July 2016

String Resources


String Resources


A string resource provides text strings for your application with optional text styling and formatting. There are three types of resources that can provide your application with strings
String
XML resource that provides a single string.
String Array
XML resource that provides an array of strings.
Quantity Strings (Plurals)
XML resource that carries different strings for pluralization.
All strings are capable of applying some styling markup and formatting arguments. For information about styling and formatting strings, see the section about Formatting and Styling.

String


A single string that can be referenced from the application or from other resource files (such as an XML layout).
FILE LOCATION:
res/values/filename.xml
The filename is arbitrary. The <string> element's name will be used as the resource ID.
COMPILED RESOURCE DATATYPE:
Resource pointer to a String.
RESOURCE REFERENCE:
In Java: R.string.string_name
In XML:@string/string_name
SYNTAX:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="string_name">text_string</string>
</resources>
ELEMENTS:
<resources>
Required. This must be the root node.
No attributes.
<string>
A string, which can include styling tags. Beware that you must escape apostrophes and quotation marks. For more information about how to properly style and format your strings see Formatting and Styling, below.
attributes:
name
String. A name for the string. This name will be used as the resource ID.
EXAMPLE:
XML file saved at res/values/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="logiclab">LogicLab Solutions</string>
</resources>
This layout XML applies a string to a View:
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/logiclab" />
This application code retrieves a string:
String string = getString(R.string.logiclab);

String Array


An array of strings that can be referenced from the application.

FILE LOCATION:
res/values/filename.xml
The filename is arbitrary. The <string-array> element's name will be used as the resource ID.
COMPILED RESOURCE DATATYPE:
Resource pointer to an array of Strings.
RESOURCE REFERENCE:
In Java: R.array.string_array_name
SYNTAX:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="string_array_name">
        <item>text_string</item>
    </string-array>
</resources>
ELEMENTS:
<resources>
Required. This must be the root node.
No attributes.
<string-array>
Defines an array of strings. Contains one or more <item> elements.
attributes:
name
String. A name for the array. This name will be used as the resource ID to reference the array.
<item>
A string, which can include styling tags. The value can be a reference to another string resource. Must be a child of a <string-array>element. Beware that you must escape apostrophes and quotation marks.
No attributes.
EXAMPLE:
XML file saved at res/values/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="vehicle_array">
        <item>Bike</item>
        <item>Car</item>
        <item>Jeep</item>
        <item>Bus</item>
    </string-array>
</resources>
This application code retrieves a string array:
Resources res = getResources();
String[] planets = res.getStringArray(R.array.vehicle_array);

Quantity Strings (Plurals)


Android supports Plurals. Plurals are XML based resources which allow to handle different quantities. This way you can select the right text based on the quantity. In your XML file you specify values for the quantities “zero”, “one”, “two”, “many”, “few”, “many”, “other” and in your code you use the method getQuantityString() to get the correct value. You can also format strings. If now Strings are formated then you pass in the plural resources and the number. If Objects should be used for formating you pass them as additional parameters.

FILE LOCATION:


res/values/filename.xml
The filename is arbitrary. The <plurals> element's name will be used as the resource ID.
RESOURCE REFERENCE:
In Java: R.plurals.plural_name
SYNTAX:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <plurals
        name="plural_name">
        <item
            quantity=["zero" | "one" | "two" | "few" | "many" | "other"]
            >text_string</item>
    </plurals>
</resources>
ELEMENTS:
<resources>
Required. This must be the root node.
No attributes.
<plurals>
A collection of strings, of which, one string is provided depending on the amount of something. Contains one or more <item> elements.
attributes:
name
String. A name for the pair of strings. This name will be used as the resource ID.
<item>
A plural or singular string. The value can be a reference to another string resource. Must be a child of a <plurals> element. Beware that you must escape apostrophes and quotation marks.
attributes:
quantity
Keyword. A value indicating when this string should be used. Valid values, with non-exhaustive examples in parentheses:
ValueDescription
zeroWhen the language requires special treatment of the number 0 (as in Arabic).
oneWhen the language requires special treatment of numbers like one (as with the number 1 in English and most other languages; in Russian, any number ending in 1 but not ending in 11 is in this class).
twoWhen the language requires special treatment of numbers like two (as with 2 in Welsh, or 102 in Slovenian).
fewWhen the language requires special treatment of "small" numbers (as with 2, 3, and 4 in Czech; or numbers ending 2, 3, or 4 but not 12, 13, or 14 in Polish).
manyWhen the language requires special treatment of "large" numbers (as with numbers ending 11-99 in Maltese).
otherWhen the language does not require special treatment of the given quantity (as with all numbers in Chinese, or 42 in English).




EXAMPLE:


XML file saved at res/values/strings.xml:

we are going to use “Plurals” tag rather than “String” tag


<resources>
<plurals name="numberOfBooks">
<item quantity="one">%d book!</item>
<item quantity= "other">%d books!</item>
</plurals>
</resources>


This application code

int booksCount= 30;
String result = getResources().getQuantityString(R.plurals.numberOfBooks, booksCount,booksCount);
textView.setText(result);


result should be “30 books!”




Formatting and Styling



Escaping apostrophes and quotes



<string name="good_example">This is a \"good string\".</string>
<string name="good_example1">This\'ll work</string>

Formatting strings

Example

<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>

The format string has two arguments: %1$s is a string and %2$d is a decimal number.

Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);


Styling with HTML markup


<resources>
    <string name="welcome">Welcome to <b>Android</b>!</string>
</resources>

Supported HTML elements include:
  • <b> for bold text.
  • <i> for italic text.
  • <u> for underline text.

Wednesday, 29 June 2016

Running State Changing Example


Different States of an Android Activity




Methords


 @Override
    protected void onStart() {
        super.onStart();
        Log.i(TAG, "onStart");
    }


    @Override
    protected void onResume() {
        super.onResume();
        Log.i(TAG, "onResume");
    }


    @Override
    protected void onPause() {
        super.onPause();
        Log.i(TAG, "onPause");
    }


    @Override
    protected void onStop() {
        super.onStop();
        Log.i(TAG, "onStop");
    }


    @Override
    protected void onRestart() {
        super.onRestart();
        Log.i(TAG, "onRestart");
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy");
    }


    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.i(TAG, "onSaveInstanceState");
    }


    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        Log.i(TAG, "onRestoreInstanceState");
    }



If you have abstract methods to implement, press Alt+Enter over class name to open a wizard. Otherwise, for overriding a function, just start typing the name of your method (without any access attribute or type), anywhere in your class scope. Then, Android Studio should suggest you overridable methods.


-------------------------------------------------------------------------------------------------------------------------

Method Overriding in Java

If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in java.
In other words, If subclass provides the specific implementation of the method that has been provided by one of its parent class, it is known as method overriding.

Usage of Java Method Overriding

  • Method overriding is used to provide specific implementation of a method that is already provided by its super class.
  • Method overriding is used for runtime polymorphism

Rules for Java Method Overriding

  1. method must have same name as in the parent class
  2. method must have same parameter as in the parent class.
  3. must be IS-A relationship (inheritance).
-------------------------------------------------------------------------------------------------------------------------



Example of adding different activity in my android application


package com.example.root.myfirstandroidapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.util.Log;


public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MyActivity";

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i(TAG,"onCreate");
    }

    @Override    protected void onStart() {
        super.onStart();
        Log.i(TAG, "onStart");
    }


    @Override    protected void onResume() {
        super.onResume();
        Log.i(TAG, "onResume");
    }


    @Override    protected void onPause() {
        super.onPause();
        Log.i(TAG, "onPause");
    }


    @Override    protected void onStop() {
        super.onStop();
        Log.i(TAG, "onStop");
    }


    @Override    protected void onRestart() {
        super.onRestart();
        Log.i(TAG, "onRestart");
    }


    @Override    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy");
    }


    @Override    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.i(TAG, "onSaveInstanceState");
    }


    @Override    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        Log.i(TAG, "onRestoreInstanceState");
    }


    public void onButtonClick(View V){

        EditText e1 = (EditText)findViewById(R.id.editText);
        EditText e2 = (EditText)findViewById(R.id.editText2);
        EditText e3 = (EditText)findViewById(R.id.editText3);

        int num1 = Integer.parseInt(e1.getText().toString());
        int num2 = Integer.parseInt(e2.getText().toString());

        int sum = num1+num2;

       
        e3.setText(Integer.toString(sum));

       
    }

}


Log details of above code run


06-29 11:12:48.872 2996-2996/com.example.root.myfirstandroidapplication I/MyActivity: onCreate
06-29 11:12:48.872 2996-2996/com.example.root.myfirstandroidapplication I/MyActivity: onStart
06-29 11:12:48.872 2996-2996/com.example.root.myfirstandroidapplication I/MyActivity: onResume
06-29 11:28:18.959 2996-2996/com.example.root.myfirstandroidapplication I/MyActivity: onPause
06-29 11:28:19.037 2996-2996/com.example.root.myfirstandroidapplication I/MyActivity: onStop
06-29 11:28:19.037 2996-2996/com.example.root.myfirstandroidapplication I/MyActivity: onDestroy






Friday, 24 June 2016

Running a Simple App


Setting up your projects in Android Studio



In this section first explain to setting up your projects on Android Studio.

The first step in the application development process is to create a new project within the Android Studio environment.

First open android studio and goto menu File -> New -> NewProject


New Project 

Once this windows appears, Android studio is ready for a new project to be crated. Fill the all field in new project window.




Target Adroid Studio

This window help to select the form factors your app will run on.

The minimum SDK version determines the lowest level of Android that your app will run on.






Add an Activity to Mobile


The next step is to define the type of initial activity that is to be created for the application.
A range of different activity types is available when developing Android applications. 
simply select the option to create a Blank Activity.






Customize the Activity






Finally, click on Finish to initiate the project creation process.


User Interface Designer tool


This window is used to create user interface and add or edit code. 




Application test to run for click to run button




Running a simple APP


Android Application Add two numbers Calculator adding two numbers in android 

public void onButtonClick(View v){

        EditText e1 = (EditText)findViewById(R.id.editText);
        EditText e2 = (EditText)findViewById(R.id.editText2);
        TextView t1 = (TextView)findViewById(R.id.textView4);


        int num1  = Integer.parseInt(e1.getText().toString());
        int num2  = Integer.parseInt(e2.getText().toString());

        int sum = num1 + num2;

        t1.setText(Integer.toString(sum));

        
    }

}

















Thursday, 23 June 2016

What is Android


What is Android

Android is an open source operating system, created by Google specifically for use on mobile devices (cell phones and tablets)

Linux based (2.6 kernel)

Can be programmed in C/C++ but most app development is done in Java (Java access to C Libraries via JNI (Java Native Interface))

Supports Bluetooth, Wi-Fi, and 3G and 4G networking

Android has a large number of applications available for download in Google play store. Around 1 million apps are published and over 50 billion downloads.

 According to survey conducted in April-May 2013 found that Android is the most popular platform for developers .It is used by almost 71% of the mobile developer’s population.

Google Proudly announced that 1 billion activated devices now use the Android OS worldwide.

Android was founded by  Andy Rubin(co-founder of Danger),

Rich Minner(co-founder of wild-fire communications)and

Chris White(headed design and interface development  at WebTV).

Android Application Life Cycle

The Android application life cycle is unique in that the system controls much of the life cycle of the application. All Android applications, or Activities, are run within their own process. All of the running  processes are watched by Android and, depending on how the activity is running (this is, a foreground activity, background activity, and so forth),Android may choose to end the activity to reclaim needed resources.
An activity represents a single screen with a user interface just like window or frame of Java.Android activity is the subclass of ContextThemeWrapper class.
If you have worked with C, C++ or Java programming language then you must have seen that your program starts from main() function. Very similar way, Android system initiates its program with in an Activity starting with a call ononCreate() callback method. There is a sequence of callback methods that start up an activity and a sequence of callback methods that tear down an activity as shown in the below Activity life cycle diagram:





Some of the specific methods and description

onCreate()      This is the first callback and called when the activity is first created.

onStart()         This callback is called when the activity becomes visible to the user.

onResume()     This is called when the user starts interacting with the application.

onPause()        The paused activity does not receive user input and cannot execute any code and                                   called when the current activity is being paused and the previous activity is being                                 resumed.

onStop()          This callback is called when the activity is no longer visible.

onDestroy()     This callback is called before the activity is destroyed by the system.

onRestart()       This callback is called when the activity restarts after stopping it.





























Wednesday, 22 June 2016

Develop Android Application

Android software development


Android software development is the process by which new applications are created for the Android operating system. Applications are usually developed in Java programming language using the Android software development kit (SDK), but other development environments are also available.


Integrated development environment (IDE) 

Eclipse using the Android Development Tools (ADT) Plugin.

NetBeans IDE also supports Android development via a plugin.

Android Studio is the official integrated development environment (IDE) for Android platform development.


Java download

http://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html


Installing Android Studio


Android Studio provides the fastest tools for building apps on every type of Android device.
World-class code editing, debugging, performance tooling, a flexible build system, and an instant build/deploy system all allow you to focus on building unique and high quality apps.

https://developer.android.com/studio/index.html