How to inflate another view from another view in Android - android

I know in my onCreate() I can inflate a view from XML by something like:
loadingScreen = (RelativeLayout) findViewById(R.id.loadingScreen);
But how could I do this from another view? Im trying to call up a loading screen by setting its visibility from GONE to VISIBLE but cant seem to figure out how to do this from my glSurfaceView

If you want to inflate a layout the code looks like this:
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout myRoot = new LinearLayout(context);
View itemView = inflater.inflate(R.layout.layout_details, myRoot);
Here you first create a new LinearLayout an then inflate the layout with id R.layout.layout_details into it. The inflate method then returns the myRoot view.
Here is a tutorial about the LayoutInflater:
Layout resources in Android

Thats actually not inflating. Inflating is the process that parses a XML layout file and creates a structure of View and ViewGroup class instances out of it (setContentView() does this for you in the background for example).
What you do is getting a reference to a view in code that you have defined in your XML layout file. To change the visibility of your GLSurfaceView you have to reference it like you did above. But remember that the View (GLSurfaceView in this case) has to be defined in your layout file.
After referencing you have to call GLSurfaceView.setVisibility() to change it's visibility.
Here's an example:
GLSurfaceView glsurface = (GLSurfaceView) findViewById(R.id.myglsurfaceid);
glsurface.setVisibility(View.VISIBLE);
Of course you can use View.INVISIBLE or View.GONE either, depending on what you want to do.
If you reference a layout (such as a RelativeLayout), you may find children of this layout with the findViewById() of your RelativeLayout instance:
RelativeLayour rl = (RelativeLayout) findViewById(R.id.mylayout);
(Button) mybutton = (Button) rl.findViewById(R.id.mybutton);
But thats usually not neccessary (at least when you just started with Android) because the activities findViewById() finds all Views that are displayed, even in sublayouts. You only have to use it if you have duplicate ids in your ui structure (tbh I never had that case yet) and want to specifiy where to look for your particular View.

You can't get a reference to a View that's doesn't exists in your current Layout, or your current View, (your current Activity content) , but you can create a new View from another XML layout, using LayoutInflater from current Activity.
you can add to you current Activity content, a new View, that's what you mentioned as " loading screen ", even by showing it as a Dialog or by creating View and then add it to root layout in your Activity
I hope I helped you

If I correctly understood what you wanna do:
Supposing you have a glSurfaceView object and you wanna grab a view that's inside that one.
You'll do just the same thing you did for you normal view. Let's say a button:
Button button = (Button) glSurfaceView.findViewById(R.id.buttonid);
If you meant something different let me know in the comments.
EDIT: And then you can just set the button's visibility:
button.setVisibility(Button.GONE)

Related

In Android, how to I edit views in a layout, when my setContentView is not in that layout?

For example, I might be in R.Layout.activity_main ,
but when I onCreate my activity, I want to change a TextView in another layout, so I would create a TextView object and findViewById from that layout, and then change it.
But this will not work unless I set my contentview to that layout, but is it possible to be able to set the TextView in that layout, without having to setContentView to that layout?
This does not work in all situations, but you could try a LayoutInflator.
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.your_layout, null);
Then to find views in that layout, just do...
layout.findViewById(R.id.your_view);
It is better to store a reference of desired view in a class as a variable when you create it's parent layout.Then change it's property when you need.
You can see more details in these two questions:
Android: How to declare global variables?
Android, how to access Activity UI from my class?

Duplicate layout IDs returning as -1 after view replacement

Short Story:
I have a layout "layout.xml", which gets replaced by another layout "success.xml" after a successful web request. Both layouts have an ImageView that provides the backgrounds to the layouts. These 2 backgrounds both need to be the same, and both are dependent on a user preference.
Longer Story: This all happens in a Fragmnet with an AsyncTask replacing the contentView with "success.xml" in onPostExecute after the web request. This happens as follows:
View view = getView();
view = null;
view = View.inflate(context, R.layout.success, null);
What I tried to do is give both ImageViews the following android:id="#+id/background_image" and then call
ImageView background = (ImageView)view.findViewById(R.id.background_image);
background.setImageResource(R.drawable.bg1);
This background-setting works for the initial view (layout.xml), but on trying to change to "success.xml", I get a NullPointException because background is null.
I've checked and the View's id is set to -1 while the original view's background_image id is set to something sensible and valid.
I've also tried setting the second view's background id like this: android:id="#id/background_image", i.e. without the '+', but still no luck.
The added complication is that it's not just 2 layouts, but about 5 that I need to do this for, so it would be really handy to recycle view id's.
Any help would be greatly appreciated.
Your code for replacing the fragment's view will not do what you want, the original view will remain the same as you change only a reference to that view and not the actual object.
To replace the view of the fragment with the new layout you could have another ViewGroup(for example a FrameLayout) in the basic layout (layout.xml) wrapping your current content(don't forget to give it an id) of layouts.xml(as I understand this is the basic layout). Then, when it's time to replace the layout you could simply do:
// remove the current content
((ViewGroup) getView().findViewById(R.id.yourWrapperLayout)).removeAllViews();
// add the new content
View.inflate(context, R.layout.success, ((ViewGroup) getView().findViewById(R.id.yourWrapperLayout)));
You could avoid adding an extra layout if, by any chance, all your five layouts have the same type for the root view(like a LinearLayout etc). In this case you would use the same code as above but you'll modify the other layouts file to use a merge tag. Also, you'll be looking for the id of the root in the layout.xml layout into which you'll add the content of the other files.
Then you could have the same ids, but you'll have to reinitialize any reference to the views(meaning that you'll have to search for the view again if you store a reference to the view(like a Button field in the fragment class)).

What does it mean to inflate a view from an xml file?

I am new to android development and keep coming across references to Inflating views from a layout xml file. I googled and searched the development guide but still wasn't able to pick up a sense for what it means. If someone could provide a very simple example, it'd be much appreciated.
When you write an XML layout, it will be inflated by the Android OS which basically means that it will be rendered by creating view object in memory. Let's call that implicit inflation (the OS will inflate the view for you). For instance:
class Name extends Activity{
public void onCreate(){
// the OS will inflate the your_layout.xml
// file and use it for this activity
setContentView(R.layout.your_layout);
}
}
You can also inflate views explicitly by using the LayoutInflater. In that case you have to:
Get an instance of the LayoutInflater
Specify the XML to inflate
Use the returned View
Set the content view with returned view (above)
For instance:
LayoutInflater inflater = LayoutInflater.from(YourActivity.this); // 1
View theInflatedView = inflater.inflate(R.layout.your_layout, null); // 2 and 3
setContentView(theInflatedView) // 4
"Inflating" a view means taking the layout XML and parsing it to create the view and viewgroup objects from the elements and their attributes specified within, and then adding the hierarchy of those views and viewgroups to the parent ViewGroup. When you call setContentView(), it attaches the views it creates from reading the XML to the activity. You can also use LayoutInflater to add views to another ViewGroup, which can be a useful tool in a lot of circumstances.
Inflating is the process of adding a view (.xml) to activity on runtime. When we create a listView we inflate each of its items dynamically. If we want to create a ViewGroup with multiple views like buttons and textview, we can create it like so:
Button but = new Button();
but.setText ="button text";
but.background ...
but.leftDrawable.. and so on...
TextView txt = new TextView();
txt.setText ="button text";
txt.background ... and so on...
Then we have to create a layout where we can add above views:
RelativeLayout rel = new RelativeLayout();
rel.addView(but);
And now if we want to add a button in the right-corner and a textview on the bottom, we have to do a lot of work. First by instantiating the view properties and then applying multiple constraints. This is time consuming.
Android makes it easy for us to create a simple .xml and design its style and attributes in xml and then simply inflate it wherever we need it without the pain of setting constraints programatically.
LayoutInflater inflater =
(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View menuLayout = inflater.inflate(R.layout.your_menu_layout, mainLayout, true);
//now add menuLayout to wherever you want to add like
(RelativeLayout)findViewById(R.id.relative).addView(menuLayout);
A layman definition for inflation might be to convert the XML code to Java code. Just a way to understand, e.g., if we have a tag in XML, OS has to create a corresponding Java object in memory, so inflatter reads the XMLtags, and creates the corresponding objects in Java.
I think here "inflating a view" means fetching the layout.xml file drawing a view specified in that xml file and POPULATING ( = inflating ) the parent viewGroup with the created View.
Because we make UI into XML but view objects is what we display so we somehow need to convert xml into view objects so inflating means we are converting xml into view objects so that it can be displayed, for this we need a service called layout inflator service and give it an xml and it will be convert for you.
In the iOS UIKit universe, this means getting the reference to the .Xib (which is XML, just like android) file and adding it to the current ViewController's view hierarchy.

How do I set the behavior for an inflated view?

I have an audio player toolbar activity that has a corresponding layout file.
I need this player to show up at the bottom of another activity. I use a ViewStub and inflate the audio toolbar's layout file in the stub.
How do I access the buttons, etc on this inflated view and how do I set their behavior?
The docs on ViewStub did not mention anything about this (or maybe I totally overlooked something).
I may be doing something fundamentally wrong here so I would appreciate any help I can get :)
Once you've inflated a view, you call findViewById on the view with the ID. e.g.
View v = ...;
TextView textView = (TextView) v.findViewById(R.id.textview);
if in your xml you had a declaration of a TextView with id "#+id/textview"

Inflator in Android Application Development

Can anybody please tell What Inflator is and how it is being used in an Android application?
I don't know the exact use of it and Why it is being used.
My preferred way to handle inflation:
//First get our inflater ready, you'll need the application/activity context for this
LayoutInflater mInflater;
mInflater = LayoutInflater.from(mContext);
//Inflate the view from xml
View newView = mInflater.inflate(R.layout.my_new_layout, null);
//Then you'll want to add it to an existing layout object
mMainLayout.add(newView);
//Or perhaps just set it as the main view (though this method can also
// inflate the XML for you if you give it the resource id directly)
setContentView(newView);
Basically, you use it to inflate existing xml layouts at runtime. Usually you go ahead and insert those new views into previously defined ViewGroups or List objects.
Not quite sure what you mean, but if its related with inflating views, its used to load layout xml files into your application. e.g by
View myWelcome = View.inflate(this, R.layout.welcome, null);
Its easier and consider best practice to have you view definition inside layout xml files, instead of creating your views fully by code.
layout inflator is used to return a java object of your complete layout
suppose you have a layout xml file in which the root element is relative layout and it contains a imageview and textview then using layout inflator you can return a view object that refers to entire layout.
this basically is used in list view and grid view to plug into them a layout object of single row or element which is to be repeated.
you were asking for use of Inflator..
basically when you want to use two xml files in one java class ,inflator is used and its code is simple which is given below..
TextView text;
View layout;
LayoutInflater inflator=getLayoutInflater();
layout =inflator.inflate(R.layout.new_xml_that you want to use in that java class,null);
text=(TextView)layout.findViewById(R.id.text);
text.setText("progressing");
here i use textview,this is present in next xml with id=text
thats it..
if you find this worthy then please like this..
thanks

Categories

Resources