Friday, 23 September 2011

Sliding notifications

How 2 add a slidey notification into your Android project

In Drawable:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
    <solid android:color="#C999" />
    <corners android:topLeftRadius="10sp"
             android:topRightRadius="10sp"/>

    <stroke android:color="#000000"
            android:width="1sp"/>
</shape>



In your layout XML




        <TextView     android:id="@+id/Message"

                    android:layout_height="wrap_content"

                    android:layout_width="fill_parent"

                    android:gravity="center"

                    android:layout_gravity="bottom"

                    android:textSize="10pt"

                    android:padding="10dp"

                    android:textColor="@android:color/black"

                    android:background="@drawable/popup_background"

                    android:text="POPUP MESSAGE" />


in Java:


AnimationSet slide = new AnimationSet(true);
Animation animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 1.5f,
Animation.RELATIVE_TO_SELF, 0f);
animation.setDuration(1000);
animation.setFillAfter(true);
slide.addAnimation(animation);

animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 1.5f);
animation.setDuration(1000);
animation.setStartOffset(3000);
animation.setFillAfter(true);
slide.addAnimation(animation);
slide.setFillAfter(true);
messageObject.startAnimation(slide);

No comments:

Post a comment