1

In my application, I want to create a floating view that will be a show at a place (chosen by me, but can be everywhere on the screen) that has nothing to do with the current ongoing activity. The activity below will still be touchable (and has focus), and this view is only touched every time it is explicitly touched.

Moreover, the floating view shall be kept when the user moves from the activity to another activity as long as he is in the application.

See the image below.

My aim for this is, when a certain thing happens, I want to this floating view to show for couple of seconds, where the user can press on it, or press the X to dismiss it (meaning it will have a custom layout)..

What is the best way to accomplish this ?

I will be targeting 4.0.x* and above.

  • x > 1

enter image description here

tony9099
  • 4,567
  • 9
  • 44
  • 73

2 Answers2

0

I think the best way to do this is to use fragment instead of activities. Your floating view would be set in the ContentView of your FragmentActivity. This way, you can set it above the fragment container (declare your floating view after the container in your xml). Then use fragmentTransaction animation to switch fragments like if it was activities.

Let me know if it's not clear or if you can only use activities and not fragments.

Damien R.
  • 3,383
  • 1
  • 21
  • 32
  • Thanks a lot. I am not familiar a lot with fragments, and the application is mainly done with lots of activities. So I guess it will be via activities, at least for the time being. Btw, a seperate question : If my app will not target tablets, and just mobiles, is it worth to use fragments instead of standards activities with buttons ? – tony9099 Nov 07 '13 at 15:21
  • It depends of your application I guess. I mostly use fragments when I deal with tabs or viewPager. – Damien R. Nov 07 '13 at 15:26
  • So how can I make this task happen without fragments ? Is it possible ? – tony9099 Nov 07 '13 at 15:27
  • Except with fragment, I don't know how to do this, sorry! Which doesn't means that it's not possible ;) – Damien R. Nov 07 '13 at 15:35
  • 1
    hehehe, Damien, thanks for being honest. I will do a bit research about it. If not possible, time to learn doing things the fragment way ! – tony9099 Nov 07 '13 at 15:36
0

I believe you could add your floating view to the root content view and then remove it once you were done. You can get the root content view inside an activity by calling

ViewGroup rootGroup = (ViewGroup) findViewById(android.R.id.content);

So it might look something like this:

protected void showPopup(View view) {
    view.setId(POPUP_VIEW_ID);
    ViewGroup rootGroup = (ViewGroup) findViewById(android.R.id.content);
    rootGroup.addView(view);
}

protected void removePopup() {
    ViewGroup rootGroup = (ViewGroup) findViewById(android.R.id.content);
    int i = 0;
    while(i < rootGroup.getChildCount()) {
        if(rootGroup.getChildAt(i).getId() == POPUP_VIEW_ID) {
            rootGroup.removeViewAt(i);
        } else {
            i++;
        }
    }
}

These methods might work well in a BaseActivity class.

Mark
  • 1,746
  • 2
  • 18
  • 19