Create a directory anim on your res folder and add this two xml file on anim folder
1) fromnothing.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1"
android:duration="800"/>
</set>
2) fromsmall.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!--Todo: Pop-up Animation -->
<translate
android:fromYDelta="-100%p"
android:toYDelta="0"
android:duration="1600"/>
<scale
android:fromXScale="0.0"
android:fromYScale="0.0"
android:toYScale="1.0"
android:toXScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="1600"/>
<alpha
android:fromAlpha="0.0"
android:toAlpha="1"
android:duration="800"/>
</set>
3) MainActivity.XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!--Todo: Pop-Up Start-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/popup_bg"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:elevation="4dp"
android:id="@+id/popup"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:text="Pop-Up"
android:layout_marginTop="5dp"
android:textStyle="italic"
android:textSize="20sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Pop-Up Message Here"
android:textSize="17sp"
android:textColor="#000000"
android:textStyle="bold"
android:textAlignment="center"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Close"
android:id="@+id/btn_close"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:backgroundTint="@android:color/holo_green_light"/>
</LinearLayout>
<!--Todo: Background End-->
<!--Todo: Background Start-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#CF000000"
android:elevation="2dp"
android:id="@+id/bg_popup"/>
<!--Todo: Background End-->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="33dp"
android:text="Open Pop-Up"
android:id="@+id/popup_open"/>.
</RelativeLayout>
4) MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
LinearLayout bg_popup, popup;
Animation fromsmall, fromnothing;
Button popup_open, btn_close;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bg_popup = (LinearLayout) findViewById(R.id.bg_popup);
popup = (LinearLayout) findViewById(R.id.popup);
popup_open = (Button) findViewById(R.id.popup_open);
btn_close = (Button) findViewById(R.id.btn_close);
fromsmall = AnimationUtils.loadAnimation(this, R.anim.fromsmall);
fromnothing = AnimationUtils.loadAnimation(this, R.anim.fromnothing);
bg_popup.setAlpha(0);
popup.setAlpha(0);
popup_open.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bg_popup.setAlpha(1);
bg_popup.startAnimation(fromnothing);
popup.setAlpha(1);
popup.startAnimation(fromsmall);
}
});
btn_close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bg_popup.setAlpha(0);
popup.setAlpha(0);
}
});
}
}
No comments:
Post a Comment