Wednesday, 20 March 2013

Android: Alert dialog with Animation

 /res/values/styles.xml


<resources>

    <style name="AppTheme" parent="android:Theme.Light" />

    <style name="DialogAnimation">
        <item name="android:windowEnterAnimation">@anim/slide_up</item>
        <item name="android:windowExitAnimation">@anim/slide_down</item>
    </style>
</resources>

/res/layout/dialoglayout.xml.


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:orientation="vertical">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"/>
    <Button
        android:id="@+id/dismiss"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="dismiss"/>

</LinearLayout>


/res/anim/slide_up.xml.


<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="300"
        android:fromYDelta="100%p"
        android:toYDelta="0" />

</set>

/res/anim/slide_down.xml.


<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="300"
        android:fromYDelta="0"
        android:toYDelta="100%p" />

</set>

MainLayout

<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" >

    <Button
        android:id="@+id/opendialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:padding="@dimen/padding_medium"
        android:text="Open Dialog"
        tools:context=".MainActivity" />

</RelativeLayout>

MainActivity.java

package com.example.animationdialog;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity;
import android.app.Dialog;

public class MainActivity extends Activity {

 Button btnOpenDialog;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnOpenDialog = (Button)findViewById(R.id.opendialog);
        btnOpenDialog.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    openDialog();
   }});
    }

    private void openDialog(){
     final Dialog dialog = new Dialog(MainActivity.this);
     dialog.setTitle("Animation Dialog");
     dialog.setContentView(R.layout.dialoglayout);
     dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
     Button btnDismiss = (Button)dialog.getWindow().findViewById(R.id.dismiss);
     
     btnDismiss.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    dialog.dismiss();
   }});
     
     dialog.show();
    }
    
}