create custom dialog

Showing posts with label create custom dialog. Show all posts
Showing posts with label create custom dialog. Show all posts

Wednesday, August 6, 2014

Creating animated dialog in android…..


This article shows you to create animated dialog in android.

1- Create anim folder inside resoures(res).

2- Create xml for slide in dialog(slide_in.xml) in anim.

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500"
    android:fromYDelta="100%p"
    android:toYDelta="0%p" />

3- Create xml for slide out dialog(slide_out.xml) in anim.


<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500"
    android:fromYDelta="0%p"
    android:toYDelta="100%p" />

4- Create style for animation in styles.xml.

<style name="DialogAnimation">
        <item name="android:windowEnterAnimation">@anim/slide_in</item>
        <item name="android:windowExitAnimation">@anim/slide_out</item>
</style>

<style name="AnimatedDialogTheme" parent="@android:style/Theme.Panel">
        <item name="android:windowAnimationStyle">@style/DialogAnimation</item>
</style>

5- In Java file initialize dialog as-

//Use try catch to avoid Exception in lower api devices
try {
dialog = new Dialog(this, R.style.AnimatedDialogTheme);
} catch(Exception ex) {
dialog = new Dialog(this);
}

Tuesday, August 5, 2014

Creating custom dialog using xml.....


This tutorial show you how to create a custom dialog in Android.
          1-  Create activity & custom dialog layout xml file.

activity_main.xml

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

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Press me!" />

</LinearLayout>


custom_dialog.xml


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


    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hey! I'm Dialog."
        android:textAppearance="?android:attr/textAppearanceLarge" />


    <Button
        android:id="@+id/but"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Press Me!" />


</LinearLayout>


2-  Create Activity Class.

MainActivity.java

package com.example.customdialog;

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


public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button pressMe = (Button) findViewById(R.id.button1);
pressMe.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showCustomDialog();
}
});
}

protected void showCustomDialog() {
// TODO Auto-generated method stub
final Dialog dialog = new Dialog(this);
dialog.setTitle("Custom Dialog");
dialog.setContentView(R.layout.custom_dialog);
Button button = (Button) dialog.findViewById(R.id.but);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
dialog.show();
}

}