Thursday 18 May 2017

Creating EditText and TextView

EditText and TextView are the subclass of  View Class.

EditText takes the  input from the user and TextView shows the output to the user.

actvity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <TextView
        android:id="@+id/name_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:textColor="@android:color/holo_orange_dark"
        android:textSize="18dp" />

    <EditText
        android:id="@+id/take_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:hint="Please enter your name" />

    <Button
        android:id="@+id/click_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_marginTop="20dp"
        android:onClick="selfDestruct"
        android:text="@string/button" />

</LinearLayout>

MainActivity.java

package sravani.com.buttonexample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Button click_button;
    EditText take_name;
    TextView name_tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        click_button = (Button) findViewById(R.id.click_button);
        take_name = (EditText) findViewById(R.id.take_name);
        name_tv = (TextView) findViewById(R.id.name_tv);

        click_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                name_tv.setText("Welcom to " + take_name.getText().toString()+" Android Tutorial");

            }
        });
    }


}

output








Working with button

The android.widget.Button is subclass of TextView class and CompoundButton is the subclass of Button class.

                                            android button
There are different types of buttons in android such as RadioButton, ToggleButton, CompoundButton etc.

Button represents a push-button widget,Push-buttons can be pressed, or clicked, by the user to perform an action.

Now we will see how to implement button in our android project.

We can implement button in 3 ways.

                                           1.Normal way.
                                           2.Anonymous inner type.
                                           3.onclick method.

1.Normal way

The generated code for the ui components will be like this: 

activity_main.xml


<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/activity_main"

android:layout_width="match_parent"

android:layout_height="match_parent">

<Button
android:id="@+id/click_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button"/>



</RelativeLayout>


Now write the code to implement button in activity.

MainActivity.java


package sravani.com.buttonexample;

import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button click_button;
@Override 
protected void onCreate(Bundle savedInstanceState) 
{ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); click_button = (Button) findViewById(R.id.click_button); click_button.setOnClickListener(this); }
@Override 
public void onClick(View view) { Toast.makeText(this, "Button is working", Toast.LENGTH_SHORT).show(); }}




2.Anonymous inner type


we need not change anything in activity_main just a small change in Activity class.

package sravani.com.buttonexample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Button click_button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        click_button = (Button) findViewById(R.id.click_button);
        click_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "Button is working", Toast.LENGTH_SHORT).show();
            }
        });
    }

  
}

Here we are passing OnClickListener parameter directly to the setOnClickListener() instead of implementing Activity class.



3.onClick Method



Instead of applying an OnClickListener to the button in your activity, you can assign a method to your button in the XML layout, using the android:onClick attribute. For example:  
       
activity_main.xml


<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:id="@+id/click_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="selfDestruct"
android:text="@string/button"/>

</RelativeLayout>

MainActivity.java

package sravani.com.buttonexample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Button click_button;

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        click_button = (Button) findViewById(R.id.click_button);

    }

    public void selfDestruct(View v) {
        Toast.makeText(MainActivity.this, "on click method working", Toast.LENGTH_SHORT).show();
    }
}

To implement button listener we have 3 ways depending on requirement we can use it,for example if user want more buttons 
in the app then oblivously he can move to 1st method.If he needs only one button  then just he implement onclick method or anonymous OnClickListener.

Friday 12 May 2017

Android Application Components

An android component is simply a piece of code that has a well defined life cycle e.g. Activity, Receiver, Service etc.

The core building blocks or fundamental components of android are activities, views, intents, services, content providers, fragments and AndroidManifest.xml etc.

Activity

An activity is a class that represents a single screen. It is like a Frame in AWT.

View

A view is the UI element such as button, label, text field etc. Anything that you see is a view.

Intent

Intent is used to invoke components. It is mainly used to:
  • Start the service
  • Launch an activity
  • Display a web page
  • Display a list of contacts
  • Broadcast a message
  • Dial a phone call etc.   
Service

Service is a background process that can run for a long time.
There are two types of services local and remote. Local service is accessed from within the application whereas remote service is accessed remotely from other applications running on the same device.

Content Provider

Content Providers are used to share data between the applications.

Fragment

Fragments are like parts of activity. An activity can display one or more fragments on the screen at the same time.

AndroidManifest.xml

It contains informations about activities, content providers, permissions etc. It is like the web.xml file in Java EE.

Android Virtual Device (AVD)

It is used to test the android application without the need for mobile or tablet etc. It can be created in different configurations to emulate different types of real devices.

Wednesday 10 May 2017

Android Studio Installation and Setup

Install Android Studio and start your first Android project


System Requirements:

Windows


  • Microsoft® Windows® 8/7/Vista/2003 (32 or 64-bit)
  • 2 GB RAM minimum, 4 GB RAM recommended
  • 400 MB hard disk space + at least 1 G for Android SDK, emulator system images, and caches
  • 1280 x 800 minimum screen resolution
  • Java Development Kit (JDK) 7
  • Optional for accelerated emulator: Intel® processor with support for Intel® VT-x, Intel® EM64T (Intel® 64), and Execute Disable (XD) Bit functionality

Mac OS X

  • Mac® OS X® 10.8.5 or higher, up to 10.9 (Mavericks)
  • 2 GB RAM minimum, 4 GB RAM recommended
  • 400 MB hard disk space
  • At least 1 GB for Android SDK, emulator system images, and caches
  • 1280 x 800 minimum screen resolution
  • Java Runtime Environment (JRE) 6
  • Java Development Kit (JDK) 7
  • Optional for accelerated emulator: Intel® processor with support for Intel® VT-x, Intel® EM64T (Intel® 64), and Execute Disable (XD) Bit functionality


On Mac OS, run Android Studio with Java Runtime Environment (JRE) 6 for optimized font rendering. You can then configure your project to use Java Development Kit (JDK) 6 or JDK 7.

Linux

  • GNOME or KDE desktop
  • GNU C Library (glibc) 2.11 or later
  • 2 GB RAM minimum, 4 GB RAM recommended
  • 400 MB hard disk space
  • At least 1 GB for Android SDK, emulator system images, and caches
  • 1280 x 800 minimum screen resolution
  • Oracle® Java Development Kit (JDK) 7
First intstall JDK 7 or above version in any operating system as mentioned above . And if it is asking path setup the path .

Downloading Android Studio 

Friday 14 November 2014

AndroidManifest.xml file in android

The AndroidManifest.xml file contains information about your package, including components of the application such as activities, services, broadcast receivers, content providers etc.
It performs some other tasks also:
  • It is responsible to protect the application to access any protected parts by providing the permissions.
  • It also declares the android api that the application is going to use.
  • It lists the instrumentation classes. The instrumentation classes provides profiling and other informations. These informations are removed just before the application is published etc.

This is the required xml file for all the android application and located inside the root directory.

A simple AndroidManifest.xml file looks like this:



Elements of the AndroidManifest.xml file

The elements used in the above xml file are described below.

<manifest>



manifest is the root element of the AndroidManifest.xml file. It has package attribute that describes the package name of the activity class.


<application>



application is the subelement of the manifest. It includes the namespace declaration. This element contains several subelements that declares the application component such as activity etc.


The commonly used attributes are of this element are iconlabeltheme etc.
android:icon represents the icon for all the android application components.
android:label works as the default label for all the application components.
android:theme represents a common theme for all the android activities.

<activity>


activity is the subelement of application and represents an activity that must be defined in the AndroidManifest.xml file. It has many attributes such as label, name, theme, launchMode etc.
android:label represents a label i.e. displayed on the screen.
android:name represents a name for the activity class. It is required attribute.

<intent-filter>


intent-filter is the sub-element of activity that describes the type of intent to which activity, service or broadcast receiver can respond to.

<action>


It adds an action for the intent-filter. The intent-filter must have at least one action element.

<category>


It adds a category name to an intent-filter.

Dalvik Virtual Machine

As we know the modern JVM is high performance and provides excellent memory management. But it need to be optimized for low-powered handheld devices.

The Dalvik Virtual Machine (DVM) is optimized for mobile devices. It optimizes the JVM for memorybattery life andperformance.

Dalvik is a name of a town in Iceland. The Dalvik VM was written by Dan Bornstein.

The Dex compiler converts the class files into the .dex files that run on the Dalvik VM.

Let's see the compiling and packaging process from the source file:




The javac tool compiles the java source file into the class file.

The dx tool takes all the class files of your application and generates a single .dex file. It is a platform-specific tool.

The Android Assets Packaging Tool (aapt) handles the packaging process.

Thursday 13 November 2014

Android Project Structure

Any Android project contain things such as application source code and resource files. Some are generated for you by default, while others should be created if required. Lets have a look at android eclipse project directory structure below :


 Src :

           The src folder contains the Java source code files of your application organized into packages. You can have more than one package in your Android application. Its always a good practice to break the source code of your application into different packages based on its core functionality.  All the source files of your Activities, Services etc. Goes into this folder. In the above screen, you can see the source file of the Activity that we created for our project.


Gen :

         The files in the gen folder are automatically generated by the ADT. Here the R.java file contains reference/id's  to all the resources in the res we use in our program. Each time we add a new resource to the project, ADT will automatically regenerate the R.java file containing reference to the newly added resource. You should not edit the contents of R.java file manually or otherwise your application may not compile.


ANDROID <version number> :

              This folder is also called Android target library in Android project structure. The version number will be  same as the build target version that we choose while creating a new project. The android.jar file contains all the essential libraries required for our program.

ASSETS

             The assets folder is used to store raw asset files. You can keep any raw data in the assets folder and there’s an asset manager in Android to read the data stored in the folder. The raw data can be anything such as audio, video, images etc. On important point about assets folder is that the data stored in this folder can’t be referenced by an ID. To access a data in this folder, we have to work with bits and bytes.

BIN :

      Bin folder is where our compiled application files go. When we successfully compile an application, this folder will contain java class files, dex files which are executable under Dalvik virtual machine, apk archives etc.

RES :

         Res folder is where we store all our external resources for our applications such as images, layout XML files, strings, animations, audio files etc.

Sub folders:

res/drawable

This folder contains the bitmap file to be used in the program. There are three different folders to store drawables. They are drawable-ldpi, drawable-mdpi, drawable-hdpi. The folders are to provide alternative image resources to specific screen configurations. Ldpi, mdpi & hdpi stands for low density, medium density & high density screens respectively. The resources for each screen resolutions are stored in respective folders and the android system will choose it according to the pixel density of the device.

res/layout

XML files that defines the User Interface goes in this folder.

res/values

XML files that define simple values such as strings, arrays, integers, dimensions, colors, styles etc. are placed in this folder.

res/menu

XML files that define menus in your application goes in this folder.

ANDROID MANIFEST FILE :

          AndroidManifest.xml is one of the most important file in the Android project structure. It contains all the information about your application. When an application is launched, the first file the system seeks is the AndroidManifest file. It actually works as a road map of your application, for the system.

              The Android Manifest file contains information about:

                               Components of your application such as Activities, services etc.
                               User permissions required
                               Minimum level of Android API required

IC_LAUNCHER-WEB.PNG :

             This is an icon to be used in Google play. Applications on Google Play require a high fidelity version of the application icon. It is not used in your actual app or the launcher, so it is not packaged in the APK.. The specifications for the high-resolution icon are:
32-bit PNG with an alpha channel
512 x 512 pixels
Maximum size of 1024KB

PROGUARD-PROJECT.TXT :

            Everything in the proguard-project.txt file will be in commented out state, because in general most people don't have any project specific needs, just to run ProGuard tool with standard settings.
The ProGuard tool shrinks, optimizes, and obfuscates your code by removing unused code and renaming classes, fields, and methods with semantically obscure names. The result is a smaller sized .apk file that is more difficult to reverse engineer.

PROJECT.PROPERTIES :

             Project.properties is the main project’s properties file containing information such as the build platform target and the library dependencies has been renamed from default.properties in older SDK versions. This file is integral to the project.