triad
triad

Reputation: 21577

How to overlay a view on top of an Activity?

Is there a way to add a View dynamically on top of the current Activity's view? One thing to note is this needs to be done from another class which only has access to the Activity's context.

ex:

public class ActivityClass extends Activity
{
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        …       

        PopupClass popup = new PopupClass(this);
        popup.showPopup();
    }

}


public class PopupClass
{
    Context context;

    void Popup(Context ctx)
    {
        context = ctx;
    }

    void showPopup()
    {
        // Add a view on top of the current Activity.
    }
}

Is there a way for PopupClass to be able to add a view to the current Activity with just knowing the context?

PopupClass does not know anything else about the Activity nor can it pass back the View to ActivityClass for ActivityClass to add.

Upvotes: 4

Views: 4086

Answers (3)

Squonk
Squonk

Reputation: 48871

Why not make PopClass extend Activity and in the AndroidManifest.xml attributes for the PopupClass activity, set android:theme="@android:style/Theme.Dialog"

Then in ActivityClass, instead of using the code in the sample you showed, you could use...

Intent i = new Intent("PopupClass.class");
startActivityForResult(i);

When the PopupClass activity is finished it can set return data in another Intent and finish.

Then in the ActivityClass you override onActivityResult(...) and process the returned Intent.

Upvotes: 0

Lior Ohana
Lior Ohana

Reputation: 3527

I'm using relative layouts for that purpose. I guess you can also use frame layouts. Take a look here: http://www.learn-android.com/2010/01/05/android-layout-tutorial/5/

Upvotes: 1

Related Questions