When to use getparent() - android

In my custom view I need to use the getParent method and set the visibility on some of it's child views depending on my custom view's state. The problem is that I want to instantiate the child views just once. Where is the best place to do this?

I'm not exactly sure what you want to do. But if all of this is occurring in the same Activity screen why don't you just assign ids (e.g. android:id="#+id/someId" to your elements in the layout.xml file.
This way you can reference any element programatically in your code by calling:
View someView = findViewById(R.id.someId);
I am unclear why you would need to call getParent. If you are trying to manipulate views in a different activity then I think you will need to use a Handler.

Related

Reason for NullPointerException if setContentView() is not used

I know that we need to place setContentView() in the onCreate() method before initializing any view otherwise it will throw a null pointer exception.
But what is the reason for it?Is the setContentView() similar to the inflate() method?
before initializing any view
I do not know for certain what you mean by "initializing any view". Given the rest of your question, I am going to interpret this as meaning "call findViewById() on the activity".
You need to call setContentView() before calling findViewById(), because otherwise there are no widgets to find.
Is the setContentView() similar to the inflate() method?
setContentView() will use a LayoutInflater and inflate() under the covers, if you pass a layout resource ID into the setContentView() method.
If no content View is set then from where you will reference the views like EditText,TextView,ListVIew and all other components which you have used in your layout.
It is like you have items in your bucket and its cover is locked for safety, you came in house without bucket and forgot it in the car and your mom asked you to put items 1 by 1 on Kitchen counter , but you don't have bucket?? so first you will get bucket then you will take out items from it.
Simply first you have to have a Container in your activity so that you can reference its items by using their ID which are assigned in layout xml.
Hope it is clear to you.!
Ok. Agree with #CommonsWare. In some details, let say if you have some views defined in your xml layout file and you want to use those views in you activity, so in this case you have to call setContentView(<R.layout.xml_layout_name>) and after that to inititlalize view using findViewById(R.id.<view_name>) with resource name as your xml layout defined name.
Ok but why we have to call setContentView() ?
So when you call setContentView() application activity means android nutshell will render views and prepare view hierarchy for your activity from layout file. Just remember you have defined views in layout using xml file and you are using those views in Java code, so for that all works to preparing views for you will do setContentView() that's why when you call findViewById() without setContentView() then your application can not find views from view hierarchy and it will throw NullPointerException.
setContentView() similar to the inflate() method ?
Some how, because both are doing the same thing, rendering views from given xml layout files but scope of works is different. setContentView() provides views throughout your activity scope while inflate() will only gives you a view from the layout file, that's why whenever you have used inflate() you have to always use reference of return view to call findViewById() like
pseudo code only for your understanding,
View view = infalter.inflate(<R.layout.<file_name>>);
TextView mextView = view.findViewById(R.id.textView);
And yes, setContentView() uses the same inflater.inflate() method too.
And when setContentView() and inflate() not required?
If you are creating dynamically views, in java code then you don't have to required call either setContentView() or inflate().
Note: In old android version when you create a dynamically views using java code and you pass it to some ListView's header or footer it won't work. For this views must be inflated before set to the ListView.

Widgets of an Activity

Is there any way in Android by which we get something like Map and List or any other collection object that give us object of all the fields (like Text-view, Edit-text, Button and any other widgets) that has been used or initialized in an Activity?
What you'd have to do is get a hold of your root view in your xml using getViewById(), and next recursively get its children using getChildCount() and getChildAt() until you have them all.
Note that the getChildCount method only applies to ViewGroups (these are things like LinearLayout, RelativeLayout,...).
Look here for a possible duplate.

getting more data from a listview row

I have a ListView that contain several TextViews. Once a TextView is pressed I get the onClick called with the view. What is the best practice to get the other text views on that row? IDs of the text views are similar along the rows so I need to keep on the context of the given view. I would guess I need to get the Parent of the given view and then grab the rest of the text views from it.
Thanks.
Yeah, you would need to get the parent of the current textview, then get all the children contained within that parent.
Depending on what you need to do with the textviews you could either call FindViewById on the parent view to get each TextView by ID. Or you can iterate through the children as suggested in this question/answer:
Android - get children inside a View?
I know people mention that calling FindViewByID is a taxing process, so consider storing the references to the textview's in some sort of object so you can quickly get the references to the other textview's within the row without having to look them up all the time.
ViewHolder's work pretty well, as they store the references to the textview's within an object (which you only need to fill once during creation) but it requires setting up your own customized adapter.

Android change setContentView

I want to dynamically set the contentView in my activity.
Because one time i use a xml as contentView, but at anothertime i use a custom view as contentView.
But how do i change the contentView?
I read about that the ViewFlipper can do this, but a viewFlipper is implemented in a xml file. And within this ViewFlipper you can add your different views.
But i dont know them at the beginning, so i cant write them all in my xml file.
Do you have any idea?
Thank you
You can call setContentView at any time*, not just in onCreate. Just define all the views you want in separate XML files and pass the relevant id when it's time to switch. If you want to define the new layout dynamically in code, then do that and call setContentView and pass the root view of your new layout.
* Technically, you can call setContentView any time you are executing on the event thread. Otherwise you need to use a Handler to call it.

Adding a view on runtime to a ViewFlippers

I have defined two views ExampleView1, ExampleView2, ExampleView3 and ExampleView4 in resources.
In my Activity I have an empty ViewFlipper. Based on doing some logic I want to add either ExampleView1 and ExampleView2 to the ViewFlipper and show the view.
Later I want to add based on internal logic either ExampleView3 and ExampleView4.
How do I do this? Is there some tutorial or can someone help me with example code?
Just use the addView method, which ViewFlipper inherits from ViewGroup. If your views are custom ones, you will have something like this:
flipper.addView(new ExampleView1());
On the other hand, if the views are defined inside an XML layout, you will have to inflate them first:
View view = LayoutInflater.from(context).inflate(R.layout.your_view, null);
flipper.addView(view);

Categories

Resources