I am quite new to Android, and while trying to understand how dialogs work, I had this below question.
For an Activity, once the View Hierarchy is set through setContentView(View), the View hierarchy cannot be changed unless the activity is recreated. This is the reason Activities are recreated on Rotating the devices, so that new View hierarchy layouts can be utilized.
However assuming the above statements are true, then how does a Dialog fit in the Activities view Hierarchy, when they are created? They just appear floating above the Activity window, with having no apparent space in the Activities view hierarchy? Although certainly they are somehow linked with the activity, as getActivity() methods returns a valid Activity instance.
Any pointers or clarification will really be appreciated.
A dialog isn't actually a part of the activity's view hierarchy. Dialogs are added via the WindowManager.
Check the source code for Dialog. When the Dialog is instantiated, it obtains a reference to the WindowManager from the context, and initializes a new Window.
mWindowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
Window w = PolicyManager.makeNewWindow(mContext);
Then later, when you show() the Dialog:
WindowManager.LayoutParams l = mWindow.getAttributes();
try {
mWindowManager.addView(mDecor, l);
mShowing = true;
When you call setContentView(), the view you supply is attached to the activity's default window. When you call Dialog.show(), the dialog's view is attached to a different window for the same display. That's why they are both displayed without actually being part of the same view hierarchy.
Related
I have created a view in which two views are included. Once a view as a waiting room (A) and in the second view (B), a call can be answered.
After a call has arrived at view A, the second view will be invoke (B). It works. If the user has finished the interaction he should get back to the view (A). Unfortunately that does not work.
Here is the code for invoke the view B. This works fine.
// Add view to content
OVSLobbyActivity.this.rootWaitingRoom.view.setVisibility(View.GONE);
OVSLobbyActivity.this.root.removeChild(OVSLobbyActivity.this.rootWaitingRoom);
OVSLobbyActivity.this.root.appendChild(OVSLobbyActivity.this.rootCallRoom);
OVSLobbyActivity.this.rootCallRoom.view.setVisibility(View.VISIBLE);
// Set fullscreen
OVSLobbyActivity.this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
OVSLobbyActivity.this.onWindowFocusChanged(true);
// force to change the layout
root.view.invalidate();
root.view.requestLayout();
Here is the code to hide the view B and show view A again. This does not work well. Here is the problem that only a white page will be shown. The elments inside the activity is missing.
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN, WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
this.onWindowFocusChanged(false);
this.rootCallRoom.view.setVisibility(View.GONE);
this.root.removeChild(OVSLobbyActivity.this.rootCallRoom);
this.root.appendChild(OVSLobbyActivity.this.rootWaitingRoom);
this.rootWaitingRoom.view.setVisibility(View.VISIBLE);
// force to change the layout
root.view.invalidate();
root.view.forceLayout();
root.view.requestLayout();
Do have any idea what is wrong?
Thank you for your help.
The solution is that the size of the view has to be recalculated. After the measures recalculated you have to execute following line:
this.view.requestLayout ();
After that the correct view will be shown.
I have seen in Snackbars being passed this findViewById(android.R.id.content) argument. We can't pass getContext() method as it demands a View parameter. I have seen on internet that programmers pass this argument inside Snackbar, what does it really mean?
Also, since it asks a View argument can i pass like any view that i have in my xml file, for example, any imageview or any videoView. If i pass these as an arguments, would my code still work? If yes, isn't it a little unexplanatory in code about what's really going on?
what does it really mean?
It asks the hosting activity to find a widget whose ID is android.R.id.content. All activities have one of these, set up by the framework Activity implementation and its associated Window. It represents the main content area of the activity.
isn't it a little unexplanatory in code about what's really going on?
You are certainly welcome to add comments to your code to explain your choice.
The Snackbar documentation explains the role of the View:
Snackbar will try and find a parent view to hold Snackbar's view from the value given to view. Snackbar will walk up the view tree trying to find a suitable parent, which is defined as a CoordinatorLayout or the window decor's content view, whichever comes first.
Having a CoordinatorLayout in your view hierarchy allows Snackbar to enable certain features, such as swipe-to-dismiss and automatically moving of widgets.
With that in mind...
can i pass like any view that i have in my xml file, for example, any imageview or any videoView. If i pass these as an arguments, would my code still work?
Perhaps. It depends a bit on the UI of your app. If there is a particular CoordinatorLayout that you want to use with the Snackbar, pass it (or a child) to make(). Otherwise, any widget should work.
I have seen numerous posts on how to display progress bar while the data loads in the background. All suggestions requires we manually place a ProgressBar in the layout xml and then use an AsyncTask to show and hide the ProgressBar and the View in question.
However, I would like to make a generic class which creates the ProgressBar programmatically at runtime and place it exactly over the view in question and maybe also slightly shade or blur the view while the ProgressBar is displayed. Had this been a Swing application I would have painted my progress bar on the "glass pane" of the view after slightly shading it with gray. In that case since the progress bar is the child of the same pseudo parent hence I could easily position that as centred.
In Android UI toolkit I am not aware of any such "glass panes". How do I achieve this effect?
Make a BaseActivity that you derive all your Activities from (same goes for Fragments).
Give it something like
protected void showLoading(){
ViewGroup content = findViewById(...);
content.setVisibility(Visibility.GONE);
ViewGroup root = findViewById(...);
root.addView(new ProgressBar());
}
Gotta make sure all your layouts have a ViewGroup for root and one for content, which otherwise might not be necessary and bloat layouts, but thats how I do it and it works fine.
Above is pseudocode of course, but you get the idea.
There's also this libary: http://www.androidviews.net/2013/04/progressfragment/, but I don't think it's necessary to import a library for that task.
Unfortunately you have to create this functionality. I always do this by creating a class from a framelayout and then place my imageview inside with my progressbar ontop. I then create an interface I use as a callback so that when said process is complete and the data is finished being processed I get my callback and I hide the progressbar. I use a framelayout because its the easiest view to use to "stack" views ontop of one another by simply placing them inside the FrameLayout. You may also need to place views inside the frame inside of a relativelayout with the width and height set to match parent so you can set the layout_centerInParent to true on your progressbar so it sits nicely inside your compound view.
Well, I'm not sure I get the question right, because it seems easier to me than it might be. But anyway:
To instantiate programmatically a progress bar, you need to do the following in your activity:
ProgressBar pb = new ProgressBar(this);
((ViewGroup) this.findViewById(R.id.view_that_will_contain_progressbar)).addView(pb);
This will add the view to the ViewGroup view_that_will_contain_progressbar. This ViewGroup should be a FrameLayout if you want to overlay over other information.
Tip: if you want to customize your ProgressBar, you can declare it in a layout file, and do the following to instantiate and attach the PB (still in your activity/fragment) :
this.getLayoutInflater().inflate(R.layout.progressbar,parent);
with parent refering to the parent you want to attach it to.
So I have 2 fragments visible if the user uses a device with a big enough screen (like a Samsung Galaxy Tab).
Right now I am displaying a fragment list twice (same fragment twice). Inside the code of the fragment I use this to hide a progressbar.
ProgressBar pb = (ProgressBar)getActivity().findViewById(R.id.progress);
pb.setVisibility(View.GONE);
Problem is that it only works on one of the fragments. Both have the same id since its the same fragment?
Should I create 2 identical fragments or is it possible to find the "correct" progress-bar in the correct fragment?
In stead of finding the view (the ProgressBar) in the activity's view hierarchy, find it in the fragment's view hierarchy. So inside the fragment, do the following:
ProgressBar pb = (ProgressBar) getView().findViewById(R.id.progress);
Generally, you don't want to do lookups in the parent's view hierarchy, so above basically applies to all views in the fragment's layout.
I have a tabhost. One of the tab's activity is a ViewGroup. This viewgroup manages two different activities. I do this so I can navigate between activities within a tab. I add the activities like so:
if (videoViewLive == null)
videoViewLive = getLocalActivityManager().startActivity("VideoPlayerLive", new Intent(this,VideoPlayerLive.class).
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
videoViewLive.setVisibility(View.VISIBLE);
this.addContentView(videoViewLive, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT));
Each of my content view activities receives asynchronous notifications. What I would like to do is somehow remove the activity/content view that is not being used. So in essence, I load content view A, B dies, becomes null, or whatever, and vice versa. I want to do this because the way I am managing these views seems problematic. (errors when loading a view, loading the other view, then loading the first again, etc.)
Have you tried ViewGroup.removeAllViews()?
This is kind of tangential to the issue here, but why are you doing this with separate Activities?
This is exactly the kind of thing Fragments were designed for. There is actually a class called ViewPager included in the android support library (see also FragmentStatePagerAdapter) which allows the same kind of behavior via tabs (potentially in the ActionBar) or swiping. The adapter automagically handles the lifecycles of the Fragments as you navigate between them, all within the context of a single Activity, such that you can use the top-level Activity for routing events and maintaining overarching state if necessary.
I would try following approach:
//Add OnGlobalLayout Listener using ViewTreeObserver
View rootView = (android.R.id.content);
//Assuming you are managing these two activities inside the ViewGroup
Activity activityA = <someRef Value>;
Activity activityB = <someRef Value>;
rootView.addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
//try couple of things here
// 1. determine which activity has focus
// 2. you could also check position of View on screen to determine which one is active or on the top
if (activityA.hasWindowFocus())
{
//do some action --remove other content from ViewGroup
}
});