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