Get root view from current activity - android

I know how to get the root view with View.getRootView(). I am also able to get the view from a button's onClick event where the argument is a View. But how can I get the view in an activity?

If you need root view of your activity (so you can add your contents there) use
findViewById(android.R.id.content).getRootView()
Also it was reported that on some devices you have to use
getWindow().getDecorView().findViewById(android.R.id.content)
instead.
Please note that as Booger reported, this may be behind navigation bar (with back button etc.) on some devices (but it seems on most devices it is not).
If you need to get view that you added to your activity using setContentView() method then as pottedmeat wrote you can use
final ViewGroup viewGroup = (ViewGroup) ((ViewGroup) this
.findViewById(android.R.id.content)).getChildAt(0);
But better just set id to this view in your xml layout and use this id instead.

This is what I use to get the root view as found in the XML file assigned with setContentView:
final ViewGroup viewGroup = (ViewGroup) ((ViewGroup) this
.findViewById(android.R.id.content)).getChildAt(0);

I tested this in android 4.0.3, only:
getWindow().getDecorView().getRootView()
give the same view what we get from
anyview.getRootView();
com.android.internal.policy.impl.PhoneWindow$DecorView##########
and
getWindow().getDecorView().findViewById(android.R.id.content)
giving child of its
android.widget.FrameLayout########
Please confirm.

Get root view from current activity.
Inside our activity we can get the root view with:
ViewGroup rootView = (ViewGroup) ((ViewGroup) this
.findViewById(android.R.id.content)).getChildAt(0);
or
View rootView = getWindow().getDecorView().getRootView();

In Kotlin we can do it a little shorter:
val rootView = window.decorView.rootView

Just incase Someone needs an easier way:
The following code gives a view of the whole activity:
View v1 = getWindow().getDecorView().getRootView();
To get a certian view in the activity,for example an imageView inside the activity, simply add the id of that view you want to get:
View v1 = getWindow().getDecorView().getRootView().findViewById(R.id.imageView1);
Hope this helps somebody

Kotlin Extension Solution
Use this to simplify access in an Activity. Then you can directly refer to rootView from the Activity, or activity.rootView outside of it:
val Activity.rootView get() = window.decorView.rootView
If you'd like to add the same for Fragments for consistency, add:
val Fragment.rootView get() = view?.rootView

For those of you who are using the Data Binding Library, to get the root of the current activity, simply use:
View rootView = dataBinding.getRoot();
And for Kotlin users, it's even simpler:
val rootView = dataBinding.root

anyview.getRootView(); will be the easiest way.

to get View of the current Activity
in any onClick we will be getting "View view", by using 'view' get the rootView.
View view = view.getRootView();
and to get View in fragment
View view = FragmentClass.getView();

Another Kotlin Extension solution
If your activity's view is declared in xml (ex activity_root.xml), open the xml and assign an id to the root view:
android:id="#+id/root_activity"
Now in your class, import the view using:
import kotlinx.android.synthetic.main.activity_root.root_activity
You can now use root_activity as the view.

if you are in a activity, assume there is only one root view,you can get it like this.
ViewGroup viewGroup = (ViewGroup) ((ViewGroup) this
.findViewById(android.R.id.content)).getChildAt(0);
you can then cast it to your real class
or you could using
getWindow().getDecorView();
notice this will include the actionbar view, your view is below the actionbar view

Related

How onCreateView works

I started a TabLayout Activity, which includes the following code to create the fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_find, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
I've read the official documentation and still unsure how it works. If somebody could explain in detail how each part here is working that would be great.
Edit: Mainly referring to View rootView = inflater.inflate(R.layout.fragment_find, container, false); what each of these 3 parameters are doing and how inflater.inflate() is working here.
OK, here we go.
The process of inflating is simply creating your view explicitly instead of doing this implicitly, this is by using this:
public void onCreate(){
setContentView(R.layout.your_layout);
}
Compare with this question.
Now with the arguments. Compare this with this section.
R.layout.fragment_find returns ID of a fragment used somewhere in the code. R is a dynamic android class used for manipulating some of your app's resources such as views, strings etc. Compare.
container is a root from some ViewGroup. So you hgave a group of, say, buttons doing common things (for example choosing some colour in your application), and they all have same parent, in your case called a container.
attachToRoot is the last argument. According to docs:
If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML.
So it is not attached to the parent we talked about in last point. Compare here.
I hope this helped.
onCreateView():
After the onCreate() is called (in the Fragment), the Fragment's onCreateView() is called. You can assign your View variables and do any graphical initialisations. You are expected to return a View from this method, and this is the main UI view, but if your Fragment does not use any layouts or graphics, you can return null (happens by default if you don't override).
Here, it's a method of the lifecycle for Fragment.

Get ViewDataBinding with root view

I want to achieve this :
Create a ViewDataBinding by inflating a layout.
Set a tag to its root view.
Insert its root view into my layout.
Later on, call findViewWithTag() to retrieve its root view.
Getting the ViewDataBinding linked to its view.
But I can't figure out how to achieve the latest step.
This is how my code looks like :
MyViewDataBinding binding = DataBindingUtils.inflate(inflater, R.layout.my_layout, myContainer, false);
View bindingRootView = binding.getRoot();
bindingRootView.setTag("aTag");
myContainer.addView(bindingRootView);
//In another part of the code ...
MyViewDataBinding binding = myContainer.findViewByTag("aTag").getViewDataBinding();
But is their something like "getViewDataBinding"?
Thank's for help.
You can find appropriate method in DataBindingUtil class. It is called findBinding(View view). It may be needed to cast returned value to your wanted class type.
I'm not sure though whether it's the best architecture. I would be interested if you would give us more context.

How to get ids of widget without setcontent view?

I am new in android.I have a simple doubt,Is there any way to get id of a widget from some layout without set it as content view ?? I am looking to invisible a view from a class .I used the code
View b = findViewById(R.id.id2);
b.setVisibility(View.GONE);
But error in "id",is it possible to get ids if widget from a java class without set as contentview ?? please help me. Thanks in advance :)
You can inflate the views separately and then use findViewById on the root of the inflated layout.
// inside of Activity, you can use 'this' for the context
LayoutInflater inflater = LayoutInflater.from(context);
View root = inflater.inflate(R.layout.some_layout);
// from your example
View b = root.findViewById(R.id.id2);
b.setVisibility(View.GONE);
At this point however these views are not part of your Activity's view hierarchy and will not be seen by the user. You will have to add them either by using setContentView(root) or by finding a ViewGroup in the current view hierarchy and calling viewGroup.addView(root).
You can do it this way
Button filterButton = new Button(YourActivity.this);
filterButton.setVisibility(filterButton.GONE);
onCreate(Bundle) is where you initialize your activity. Most importantly, here you will usually call setContentView(view) with a layout resource defining your UI, and using findViewById(int) to retrieve the widgets in that UI that you need to interact with programmatically.
See below link :-
Why findViewById() is returning null if setcontentview() is not called?
'But error in "id",is it possible to get ids if widget from a java class without set as contentview ?? '
You have to create dynamic view then you do not have need to call setcontentview.
how to set setContentView?
Is there a way to set setContentView(int id) dynamically?

QuickReturn ListView in an activity?difficulty in 2 views in an activity

I am trying to use a QuickReturn ListView in an activity. The link given below uses a fragment do it.
http://www.androidviews.net/2012/12/quickreturn-listview/
I am stuck here:
View view = inflater.inflate(R.layout.fragment, null);
View mHeader = inflater.inflate(R.layout.header, null);
I can't find an alternative. How can I change the above code so that it can be used in an activity?
Quick return ListView uses to different views and stitches them together. Then works upon them in a fragment. I can't do it in case of an activity.
To get the the root view from inside activity use:
View rootView = getWindow().getDecorView().findViewById(android.R.id.content)
To get inflater from inside activity use:
LayoutInflater layoutInflater = this.getLayoutInflater();
Hope this help, please tell me if not.

How to add Layout as a child

In my application I have 2 layouts. One's is root layout that changes dynamically and the second is just a form that opens many times. The form must be child of the root layout but I'm failing to perform so.
I assume that I should simply use:
main.AddView(formLayout)
but I can't figure out how get this formLayout object.
Will thank you for possible answers.
Sounds like you need the LayoutInflater object Android reference.
This allows you to create an object from the xml layout in your project.
With the advice of cjk I wrote piece of code that actually answers my question:
setContentView(R.layout.main);
main = ((ViewGroup)findViewById(android.R.id.content));
LayoutInflater inflater = this.getLayoutInflater();
ViewGroup form= (ViewGroup) inflater.inflate(R.layout.formLayout, null);
main.addView(form);
Thank you all
Not sure if I understand the question properly, but something like that might work:
View myView;
myView = (View) this.findViewById(R.id.formLayout);
main.addView(myView);
By get I figure you mean you want to retrieve a field in the new opened layout, you can do it by making it a new Intent and using startActivityForResult instead of startActivity.

Categories

Resources