Wednesday, January 30, 2019

One of the most coolest things you can add on your activity, is the ability to create a custom popup activity. Follow the steps bellow.

1. We are going to create a layout for our popup activity res/layout/popup_activity.xml the file name does not matter.



<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    tools:context="com.designspaza.carcontroller.PopupActivity"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#555"    android:gravity="center"    android:orientation="vertical"    android:id="@+id/pop_layout">

    <TextView        android:id="@+id/pop_title"        android:layout_width="wrap_content"        android:layout_height="50dp"        android:background="@android:color/transparent"        android:gravity="center_horizontal"        android:text="POP1"        android:textColor="#fff"        android:textSize="20sp" />

    <Button        android:id="@+id/close_btn"        android:layout_width="100dp"        android:layout_height="60dp"        android:text="Close" />
</LinearLayout>


2. next create the PopupActivity class to handle any View present on your popup

public class PopupActivity extends Activity {
@Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.popup_activity);

Note: popup_activity: is the name of our popup layout.

3. we need a theme for our popup, see below.

<style name="AppTheme.PopMe" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowCloseOnTouchOutside">true</item>
</style>

android:windowIsTranslucent: will make the pop up transparent beyond its borders.
android:windowCloseOnTouchOutside: will close the pop up when you click out side its borders.

4. we need to register our class with the manifest file manifests/AndroidManifest.xml

<activity    android:name=".PopupActivity"    android:theme="@style/AppTheme.PopMe" />


5. here i will show you how to actually display the popup after a click event.

    //details direction for popup    button.setOnClickListener(new View.OnClickListener(){
        @Override        public void onClick(View v) {
            Intent i = new Intent(context.getApplicationContext(), PopupActivity.class);
            context.startActivity(i);
        }
    });

Note: PopupActivity: is the name of our class.


I hope this was very helpful. Also read how to pass values to your popup activity.












Leave a Reply

Subscribe to Posts | Subscribe to Comments

- Copyright © Mdunge Apps - Blogger Templates - Powered by Blogger - Designed by Johanes Djogan -