How do you setup a ViewSwitcher like the Android ICS Dialer? - java

How do you setup a ViewSwitcher like the ICS Dialer has? I can setup a ViewSwitcher and make it have a animation when you swipe left or right, but I dont know how to make it so you can actually drag the view left or right!

You can use the Horizontal View Swiping with ViewPager. This is good and recommended way so that your app can run on broader platforms.

You can try this: https://github.com/davidsun/horizontalpager/tree/patch-1.
The basic idea is like this.
Firstly, when view is touched, onTouchEvent will be called. But since the view onTouchEvent can be intercepted (by its child view), the parent view must override the onInterceptTouchEvent method. So, just override two methods of ViewGroup, which are onTouchEvent and onInterceptTouchEvent.
Secondly, to build your own displaying method, you need to implement methods onMeasure and onLayout, which calculate the sizes of its child views.
This is the basic idea of building a draggable view switcher.

Related

How to programatically find which view is being scrolled to the top in the screen?

I have four recyclerviews inside a NestedScrollview and i am setting tag for each recyclerview elements in order to design sticky header.
I have to get the view at the top most position so that i can get the tag of that view and implement sticky.
After i am searching through forums, still i didn't able to get the correct code on getting the view at the top position of a screen.
Any help on this will be really helpful for my project
Set and implement OnScrollListener on your Activity class. Then you should override onScroll, onScrollStateChanged which will provide you the view is scrolled and its state. Look at it - https://developer.android.com/reference/android/widget/AbsListView.OnScrollListener.html

Is there a way to remove a view from a ViewGroup

I'm working on a Tetris android game. I need to remove some ImageViews from a layout manager. But I can't find any methods in the ViewGroup class to remove views. I know that you can use
view.setVisibility (View.INVISIBLE);
to make the view invisible, but the view is still here, in the memory, right?
So that will affect the performance, right?
Yes:
((ViewGroup)view.getParent()).removeView(view);
What IDE are you using? simply putting ((ViewGroup) view.getParent()). in the ide should give you the list of all methods.
And ofcourse you have to remove it from the parent viewgroup. that is the need of the method getParent() to get the ViewGroup the view is in, first.
And there is the method removeView(View view) right there in the list. Self Explanatory.
Just one thing to remember, you have to call this method from the UIThread. You cannot call this from any other Thread running in the background.
as you focused to INVISIBLE of VIEW in your Question,instead of using this you can use Gone of View.so further you can use gone in your XML or Java both to a view.
like in XML
android:visibility="gone"
And In Java
view.setVisibility(View.GONE);
enjoy your code :)-

Is there any event to know if a view is moved below some other view?

I have a stack of views shown on top of each other. So at a time only 1 view is shown and others are below the active view. I want to know whether a particular view is active or moved below some other view inside the ViewPart code. Is there any listener which can tell me that the composites of any view are now moved below some other composites of another view?
Also it would be great if can I know through listeners that a particular view is now made invisible.
I have only access to my view code and that is why I have to implement it in that view only.
Stack of views is shown under one Window so the parent shell is same of all the views.
You can use an org.eclipse.ui.IPartListener to listen for events about parts.
In a ViewPart you can use:
getSite().getPage().addPartListener(partListener);
to add a listener. The listener has method for parts being opened, closed, activated and deactivated and brought to the top of stack.
You can also use IPartListener2 which has some additional methods.

Is there a generic way to display progress bar over Views in Android?

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.

Synchronize Two HorizontalScrollViews Android

I have two HorizontalScrollViews in my layout and I want to move them at the same. So if I touch scrollview a I want to move scrollview b at the same time and vice versa. I have done some searching around and haven't found a good solution. I have gotten it close by setting an onTouchListener() and trying to manually move each but I can't get it to move smoothly. Any ideas?
I was able to figure out what I needed to do. I noticed that there is a protected method called onScrollChanged() in the ScrollView and HorizontalScrollView classes. So I subclassed HorizontalScrollView and implemented that method manually. From there I just created a callback that gets called when onScrollChanged() is invoked. Works like a charm. Why Google didn't make that method public is beyond me.

Categories

Resources