Saturday, 26 September 2015

Text Input Layout Using Android Design Support Library


TextInputLayout which wraps EditText to show a floating label when hint is hidden due to user input. it used for showing error and handling error and also enable hint in animated way.

The EditText has to be wrapped by TextInputLayout in order to display the floating Also supports showing an error via setErrorEnabled(boolean) and setError(CharSequence).

Implementation



build.gradle
apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "inputlayout.test.com.textinputlayout"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.0.1'
    compile 'com.android.support:design:23.0.0'
}

MainActivity.java
package inputlayout.test.com.textinputlayout;

import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import java.lang.ref.WeakReference;

public class MainActivity extends AppCompatActivity {

    private static final int MIN_TEXT_LENGTH = 5;
    private static final String EMPTY_STRING = "";

    private TextInputLayout mTextInputLayout;
    private EditText mEditText;
    private Toolbar mToolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);
        mTextInputLayout = (TextInputLayout) findViewById(R.id.text_input_layout);
        mEditText = (EditText) findViewById(R.id.editText);

        mTextInputLayout.setHint(getString(R.string.hint));
        mEditText.setOnEditorActionListener(ActionListener.newInstance(this));
    }

    private boolean shouldShowError() {
        int textLength = mEditText.getText().length();
        return textLength > 0 && textLength < MIN_TEXT_LENGTH;
    }

    private void showError() {
        mTextInputLayout.setError(getString(R.string.error));
    }

    private void hideError() {
        mTextInputLayout.setError(EMPTY_STRING);
    }

    private static final class ActionListener implements TextView.OnEditorActionListener {
        private final WeakReference<MainActivity> mainActivityWeakReference;

        public static ActionListener newInstance(MainActivity mainActivity) {
            WeakReference<MainActivity> mainActivityWeakReference = new WeakReference<>(mainActivity);
            return new ActionListener(mainActivityWeakReference);
        }

        private ActionListener(WeakReference<MainActivity> mainActivityWeakReference) {
            this.mainActivityWeakReference = mainActivityWeakReference;
        }

        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            MainActivity mainActivity = mainActivityWeakReference.get();
            if (mainActivity != null) {
                if (actionId == EditorInfo.IME_ACTION_GO && mainActivity.shouldShowError()) {
                    mainActivity.showError();
                } else {
                    mainActivity.hideError();
                }
            }
            return true;
        }
    }
}

activity_main.xml
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    </android.support.design.widget.AppBarLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:focusable="true"
        android:focusableInTouchMode="true" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize"
        android:orientation="vertical"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:paddingTop="60dp">

        <android.support.design.widget.TextInputLayout
            android:id="@+id/text_input_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            app:errorEnabled="true"
            app:errorTextAppearance="@style/ErrorText">>


            <EditText
                android:id="@+id/editText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:imeOptions="actionGo"
                android:inputType="text"
                android:singleLine="true" />
        </android.support.design.widget.TextInputLayout>


    </LinearLayout>

</android.support.design.widget.CoordinatorLayout>

strings.xml
<resources>
    <string name="app_name">TextInputLayout</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="error">Please enter at least 5 characters</string>
    <string name="hint">Please Enter Email Id</string>
</resources>

colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="green">#1E9618</color>
    <color name="accent">#4EE147</color>
    <color name="green_dark">#156912</color>
    <color name="error_text_color">#FF0000</color>
    <color name="colorPrimary">#F50057</color>
    <color name="colorPrimaryDark">#C51162</color>
    <color name="textColorPrimary">#FFFFFF</color>
    <color name="windowBackground">#FFFFFF</color>
    <color name="colorAccent">#FF80AB</color>
    <color name="navigationBarColor">#000000</color>
</resources>

Sunday, 20 September 2015

Android Questions and Answers

1. Android History.

Android is operating system for mobile device and also open source software for developing mobile application. Android was released by "Android Inc Company" in 2003. later in 2005 "Google" company acquired this company. In 2007 Google company formed one group that group name is called as "OHA" (Open Handset Alliance). This OHA contains more than 30 companies Among of them 80% companies are mobile manufacture companies. Some of the companies are Samsung, LG, HTC, Karbon etc..

2. What is android?

Android is a stack of software for mobile devices which has Operating System, middleware and some key applications. The application executes within its own process and its own instance of Dalvik Virtual Machine. Many Virtual Machines run efficiently by a DVM device. DVM executes Java language byte code which later transforms into .dex format files.

3. What are the advantages of Android?

The following are the advantages of Android:
  • The customer will be benefited from wide range of mobile applications to choose, since the monopoly of wireless carriers like AT&T and Orange will be broken by Google Android.
  • Features like weather details, live RSS feeds, opening screen, icon on the opening screen can be customized
  • Innovative products like the location-aware services, location of a nearby convenience store etc., are some of the additive facilities in Android.
  • Components can be reused and replaced by the application framework.
  • Optimized DVM for mobile devices
  • SQLite enables to store the data in a structured manner.
  • Supports GSM telephone and Bluetooth, WiFi, 3G and EDGE technologies
  • The development is a combination of a device emulator, debugging tools, memory profiling and plug-in for Eclipse IDE

4. What are android SDK Features?

Android as a development environment some API are given below
  • Wi-Fi hardware access
  • GSM, EDGE, and 3G networks for telephony or data transfer, enabling you to make or receive calls or SMS messages, or to send and retrieve data across mobile networks.
  • Comprehensive APIs for location-based services such as GPS.
  • Full multimedia hardware control, including playback and recording with the camera and microphone.
  • APIs for using sensor hardware, including accelerometers and the compass
  • Libraries for using Bluetooth for peer-to-peer data transfer
  • IPC message passing
  • Shared data stores
  • Background application and processes
  • Home screen Widgets, Live Folders, And Live Wallpaper
  • The ability to integrate application search results into the systems search
  • map controls as part of their user interface.
  • Media Libraries for playing and recording of audio/video.
  • Localization through a dynamic resource framework
  • An Application framework that encourage reuse of application components and the replacement of native applications

5. Describe the APK format.

The APK file is compressed the AndroidManifest.xml file, application code (.dex files), resource files, and other files. A project is compiled into a single .apk file.

6. What is .apk extension?

The extension for an Android package file, which typically contains all of the files related to a single Android application. The file itself is a compressed collection of an AndroidManifest.xml file, application code (.dex files), resource files, and other files. A project is compiled into a single .apk file.

7. What is .dex extension?

Android programs are compiled into .dex (Dalvik Executable) files, which are in turn zipped into a single .apk file on the device. .dex files can be created by automatically translating compiled applications written in the Java programming language.

8. Android Application structure

Coming Soon...


10. What is splash screen??

Splash screen will be displayed for few moment beofre landing to main screen of the project.
In splash activity you can add company logo or some information about your company.


Tuesday, 15 September 2015

Passing object between activity by using Serializable


Here we passing Serializable object and List of Object From one activity to another activity.
if you want to pass custom object from one activity to another activity you need to implement Serializable interface.
This is student class having some property in that case I want to transfer student object and list of student object between Activity. if you want to know about Serialization you can refer this link.

Student.java
package com.test.google.customobject.intent;

import java.io.Serializable;

/**
 * Created by admin on 11-Sep-15.
 */
public class Student implements Serializable {

    private String name;
    private int age;
    private String  address;
    private String city;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }
}

MainActivity.java
package com.test.google.customobject.intent;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import java.util.ArrayList;


public class MainActivity extends AppCompatActivity implements  View.OnClickListener {
private Student mStudent;
    private ArrayList<Student> mStudentList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        updateStudentData();
    }

    private void updateStudentData() {
        mStudentList= new ArrayList<Student>();
        for (int count = 0; count < 4; count++) {
            mStudent= new Student();
            mStudent.setAddress("Delhi");
            mStudent.setAge(30);
            mStudent.setName("Test User");
            mStudent.setCity("Faridabad");
            mStudentList.add(mStudent);
        }

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btnClick:
                Bundle bundle= new Bundle();
                bundle.putSerializable("studentData", mStudent);
                startActivity( new Intent(this,StudentActivity.class).putExtras(bundle));
                break;
            case R.id.btnListObject:
                Intent intent=  new Intent(this,StudentActivity.class);
                intent.putExtra("studentList",mStudentList );
                intent.putExtra("isListObject", true);
                startActivity(intent);
                break;
        }
    }
}

StudentActivity.java
package com.test.google.customobject.intent;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;


public class StudentActivity extends AppCompatActivity {
private LinearLayout mRelativeLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_student);
        mRelativeLayout= (LinearLayout) findViewById(R.id.container);
        showData();
    }

    private void showData() {
        boolean isListObject= getIntent().getBooleanExtra("isListObject", false);
        if(isListObject){
            List<Student> student= (List<Student>) getIntent().getSerializableExtra("studentList");
            int studentSize=student.size();
            for (int studentCount = 0; studentCount <studentSize ; studentCount++) {
            getStudentView(student.get(studentCount));
            }
        }else{
            Student student= (Student) getIntent().getSerializableExtra("studentData");
            getStudentView(student);
        }

    }

    private void getStudentView(Student student){
        View studentItemView= getLayoutInflater().inflate(R.layout.student_row, null);
        TextView nameTextView= (TextView) studentItemView.findViewById(R.id.tvName);
        TextView addressTextView= (TextView) studentItemView.findViewById(R.id.tvAddress);
        TextView ageTextView= (TextView) studentItemView.findViewById(R.id.tvAge);
        TextView cityTextView= (TextView) studentItemView.findViewById(R.id.tvCity);
        nameTextView.setText(student.getName());
        addressTextView.setText(student.getAddress());
        ageTextView.setText(String.valueOf(student.getAge()));
        cityTextView.setText(student.getCity());
        mRelativeLayout.addView(studentItemView);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_student, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="fill_parent"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin"
                tools:context=".MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:onClick="onClick"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:id="@+id/btnClick"
        android:text="@string/action_button"
        android:layout_height="wrap_content"/>
    <Button
        android:layout_width="wrap_content"
        android:onClick="onClick"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_below="@id/btnClick"
        android:id="@+id/btnListObject"
        android:text="@string/list_action_string"
        android:layout_height="wrap_content"/>

</RelativeLayout>


activity_student.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:orientation="vertical"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:id="@+id/container"
        android:paddingBottom="@dimen/activity_vertical_margin"
        tools:context="map.test.google.ajitdubey.intent.StudentActivity">
    </LinearLayout>
</ScrollView>

student_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:orientation="vertical"
              android:layout_height="match_parent">
    <TextView
        android:id="@+id/tvName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/tvAge"
        android:layout_width="wrap_content"
        android:layout_below="@+id/tvName"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/tvAddress"
        android:layout_width="wrap_content"
        android:layout_below="@+id/tvAge"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/tvCity"
        android:layout_below="@+id/tvAddress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:layout_width="match_parent"
        android:background="@android:color/darker_gray"
        android:layout_marginTop="5dp"
        android:layout_height="1dp"/>

</LinearLayout>

Friday, 11 September 2015

Difference between Parcelable and Serializable.


Serializable is a standard Java interface. You simply mark a class Serializable by implementing the interface, and Java will automatically serialize it in certain situations. The Problem is that it is used reflection so that it is a slow process. It is very easier to use. This approach used for while sending data over the network or sending object from one activity to another activity in android.
Serialization is the process of converting an object's state (including its references) to a sequence of bytes, as well as the process of rebuilding those bytes into a live object at some future time.Every serializable class is assigned a version identifier called a serialVersionUID

Uses of Serialization


  • Send data to a remote computer using such client/server Java technologies as RMI or socket programming
  • Send objects between the servers
  • Persist data for future use
  • Store serialized object in Disk or database as Blob
  • Send Object from one activity to another activity in android

Implementation


import java.io.Serializable;
import java.util.ArrayList;
import java.util.TreeMap;

import android.os.Parcel;
import android.os.Parcelable;

public class MyObjects implements Serializable {

private String name;
private int age;

public ArrayList<String> address;

public MyObjects(String name, int age, ArrayList<String> address) {
    super();
    this.name = name;
    this.age = age;
    this.address = address;
}

public ArrayList<String> getAddress() {
    if (!(address == null))
        return address;
    else
        return new ArrayList<String>();
}

public String getName() {
    return name;

}

public String getAge() {
    return age;
}

}

Parcelable :
Android Parcelable implementation allows objects to read and write from Parcels which can contain flattened data inside message containers.
If a developer wants to convert a Java object into Parcelable, then the best way to do so is by implementing the Parcelable interface and overriding the writeToParcel() methods in its own class. The first step is to override the writeToParcel() method and write all object members into parcel objects. The second is to create a static Parcelable.Creator object to de-serialize the Java object.

Implementation
 
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;
 
public class Student implements Parcelable{
 
    String mSName;
    int mSAge;
    String mSAddress;
    String mSCourse;
 
    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }
 
    /**
    * Storing the Student data to Parcel object
    **/
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(mSName);
        dest.writeInt(mSAge);
        dest.writeString(mSAddress);
        dest.writeString(mSCourse);
    }
 
    /**
    * A constructor that initializes the Student object
    **/
    public Student(String sName, int sAge, String sAddress, String sCourse){
        this.mSName = sName;
        this.mSAge = sAge;
        this.mSAddress = sAddress;
        this.mSCourse = sCourse;
    }
 
    /**
    * Retrieving Student data from Parcel object
    * This constructor is invoked by the method createFromParcel(Parcel source) of
    * the object CREATOR
    **/
    private Student(Parcel in){
        this.mSName = in.readString();
        this.mSAge = in.readInt();
        this.mSAddress = in.readString();
        this.mSCourse = in.readString();
    }
 
    public static final Parcelable.Creator<Student> CREATOR = new Parcelable.Creator<Student>() {
 
        @Override
        public Student createFromParcel(Parcel source) {
            return new Student(source);
        }
 
        @Override
        public Student[] newArray(int size) {
            return new Student[size];
        }
    };
}


Differences between Serialization and Parcelable.
  • Parcelable and Serialization are used for marshaling and unmarshaling Java objects.
  • Parcelable is faster than Serialization makes it a preferred choice of approach while passing an object.
  • Parcelable interface takes more time for implemetation compared to serializable interface
  • Serializable interface is easier to implement
  • Serializable interface create a lot of temporary objects and cause quite a bit of garbage collection
  • Parcelable array can be pass via Intent in android
  • Serialization is a marker interface, which implies the user cannot marshal the data according to their requirements. In Serialization, a marshaling operation is performed on a Java Virtual Machine (JVM) using the Java reflection API

Thursday, 10 September 2015

Expandable custom notification Handle event by using BroadcastReceiver

I am writing code for handling action event from notification by using BroadcastReceiver and also you can handle by using other way with in same activity.

MainActivity.java

package com.example.addactionnotification;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends ActionBarActivity implements OnClickListener {
 static String CLOSE_ACTION = "close_action";
 static String ACCEPT_ACTION = "accept_action";
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
 }

 @Override
 public void onClick(View view) {

  switch (view.getId()) {
  case R.id.btnAdddActionNotification:

   showAddActionNotification();
   break;
  default:
   break;
  }
 }

 private void showAddActionNotification() {

  int icon = R.drawable.ic_launcher;

  NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

  builder.setSmallIcon(icon);

  builder.setContentText("Add action notification").setContentTitle("Here is accept and close action only");

  Intent closeIntent = new Intent(this, MyReceiver.class);


  closeIntent.setAction(CLOSE_ACTION);


  PendingIntent closePendingIntent = PendingIntent.getBroadcast(this, 12345, closeIntent, PendingIntent.FLAG_UPDATE_CURRENT);

  builder.addAction(R.drawable.history, "Close", closePendingIntent);

  Intent acceptIntent = new Intent();
  acceptIntent.setAction(ACCEPT_ACTION);

  PendingIntent acceptPendingIINtent = PendingIntent.getBroadcast(this, 12345, acceptIntent, PendingIntent.FLAG_UPDATE_CURRENT);


  builder.addAction(R.drawable.contact, "Accept", acceptPendingIINtent);

  Notification notification = builder.build();


  Intent notificationIntent = new Intent(this, MainActivity.class);

  PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);


  notification.contentIntent = contentIntent;

  notification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_SHOW_LIGHTS;
  NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

  mNotificationManager.notify(1, notification);
 }
}

MyReceiver.java
package com.example.addactionnotification;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class MyReceiver extends BroadcastReceiver {

 @Override
 public void onReceive(Context context, Intent intent) {
  String action = intent.getAction();
  if (MainActivity.CLOSE_ACTION.equals(action)) {

   // write code for close action
   Toast.makeText(context, "Hello close action", Toast.LENGTH_LONG).show();

  } else if (MainActivity.ACCEPT_ACTION.equals(action)) {
   Toast.makeText(context, "Hello close action", Toast.LENGTH_LONG).show();
   // write code for accept action
  }
 }
}

activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin"  android:paddingLeft="@dimen/activity_horizontal_margin"   android:paddingRight="@dimen/activity_horizontal_margin"   android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.addactionnotification.MainActivity" >

    <Button       android:id="@+id/btnAdddActionNotification"
      android:layout_width="wrap_content"     android:layout_centerHorizontal="true"      android:layout_centerVertical="true"
        android:onClick="onClick"      android:text="@string/addActionNotification"
    android:layout_height="wrap_content" />
</RelativeLayout>

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.addactionnotification"
    android:versionCode="1"
    android:versionName="1.0">
    <uses-sdk
        android:minSdkVersion="12"
        android:targetSdkVersion="21" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:excludeFromRecents="true"
            android:label="@string/app_name"
            android:launchMode="singleTask">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name="com.example.addactionnotification.MyReceiver">
        </receiver>
    </application>
</manifest>