Tuesday, September 24, 2013

Create an Android Activity as a popup

For the most of your popup needs, Android's Dialog should be good enough.  To popup some message, use AlertDialog.  Always use AlertDialog.Builder to build a new AlertDialog.  If you need a custom set of controls in the main message, use AndroidDialog.Builder's setView() method to set appropriate layout.

Now, if we need to popup a special kind of Activity that a Dialog cannnot solve, we can still do that.  But using Activity as a popup has a few problems.  In this post, I would like to discuss some of the approaches that I had taken to solve the problems.


First, how do we make an Activity popup?

1. Open AndroidManifest.xml file (it should be in the root of the project).
2. There should be an entry for your activity in the file.  Add theme to it:
<activity
            android:name="xyz"
            android:label="@string/title_activity" >
becomes


<activity
            android:name="xyz"
            android:label="@string/title_activity"

            android:theme="@android:style/Theme.Holo.Dialog" >


Next, what are the problems with using an Activity as popup

1. Touching on the screen anywhere outside the popup closed the popup.
2. If I touched a control in the background grayed-out region, it was working just fine.

Solutions:

1. First point is pretty easy to solve.


getWindow().setFlags(                                                         WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,                 WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);

This will make sure that touching outside the popup Activity will not close the Activity.
Based on this.

2. The second point is much more difficult.  Here are the steps:


2.1 Create an activity with LinearLayout.  (I used RelativeLayout.  The discussion given below mentions RelativeLayout.  But, LinearLayout turned out fine)  Here is the content



<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:background="@android:color/transparent"
   android:orientation="vertical"
   tools:context=".BlockBackgroundActivity" >


</LinearLayout>



2.2 Set the Activity's theme to Translucent.  This way, users will not know there is an Activity on the top of the previous Activity.



android:theme="@android:style/Theme.Translucent.NoTitleBar"


2.3 Instead of calling startActivity() on the old popup Activity, start the newly created Activity.

2.4 This newly created Activity is not going to do anything.  This should just start the popup activity.



protected void onStart() {
              Intent intent = new Intent(
                     this.getApplicationContext(),                                    OldPopupActivity.class);
              startActivity(intent);
        }



Also, we need to make sure that this newly created Activity gets closed when popup Activity closes.



                   @Override
        protected void onResume() {
             super.onResume();
             this.finish();
        }


The idea is that when onStart() gets invoked, we invoke startActivity(); so onResume() never gets invoked on our newly created Activity.  When popup Activity is closed, the newly created Activity's onResume() is invoked.  At that point, we finish() the newly created background Activity.







Based on this.

No comments:

Post a Comment