Is there a way to remove a view from a ViewGroup - java

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 :)-

Related

What does findViewById(android.R.id.content) do in Snackbar?

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.

view and viewgroup android

I know java basics and I am learning Android development now. I have learnt that there are two central android classes for the ui, View and ViewGroup. I want you to correct me wherever I am wrong.
ViewGroup is a subclass of View and holds View objects together. TextView, EditText, etc are subclasses of View. LinearLayout, Gridview, etc are subclasses of ViewGroup.
I want to relate this to the OOP concept of java but I don't seem to get all this. Where are the objects? How come a subclass is a container?
I'll give you a basic overview here. I'm also pretty new to Android, so the correct-me-if-I'm-wrong thing goes for me too. :)
A View is basically a unit of UI; like, say, a box of 24x24 pixels (yes, THAT basic). Now this box can be used for anything, because it is the top-most entity in the hierarchy. We can define it more precisely by specifying what we want it to hold, this is where TextView, ImageView, WebView, etc. come in. So this box 24x24 may hold text, an image or a web-page content, respectively.
A ViewGroup can belong in the 'etc.' above. Just as for showing text, the 24x24 box can be used to display 4 boxes 12x12. If so, the View can be classified as a ViewGroup.
Further, when we know what type of arrangement we require the box to hold, in this case, we can further classify it as any of LinearLayout, RelativeLayout, GridLayout, FrameLayout, etc.
In such a hierarchy, an entity can have a child object of any kind, even an instance of its own class.
You may even say that View is the 'Object' of UI.
I hope I have helped you.
Comments/edits welcome. :)

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.

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

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.

Android - findViewById() and dynamically setting ID's

I have a poll of ID's (ids.xml), and I assign id's for views I create dynamically. Now my question is pretty simple - assume I create a new view and assign it an id with setId() in conjuction with R.id.uniqueId. Later on, can I access the view with findViewById(R.id.uniqueId)?
If so, what could be the reason it returns null?
Here is a toy example: UPDATED
LinearLayout l = new LinearLayout(this);
l.setId(R.id.mId);
setContentView(l); //i see on screen the views added to 'l'
l = (LinearLayout) findViewById(R.id.mId); //it returns null :(
How come it does not register\map the assgined ID to the view it was assigned?
Does your desired view have a parent view that you could use to call parent.findViewById on? That may help narrow your problem down.
One thing I noticed that's missing from your brief example: you need to make sure you're adding the new LinearLayout to the view hierarchy before you will be able to find it with findViewById:
findViewById(R.id.parent).addView(l)
You can also use the hierarchy viewer to take a look and see if everything's being set up properly.
encountered this many times, try cleaning your project by making a clean, Also check findViewById() returns null for custom component in layout XML, not for other components
From what you have given us it appears that the view is never added to the Activity's view. This is necessary as findViewById uses the activity's view as a parent to find a child view with that Id.
If you are creating the view you can always create it as a member variable and refer to it that way if you are going to be adding it to a view at a later time. Please note that the view must be added after the id has been set.

Categories

Resources