How to add view in custom layout in runtime - android

I am creating custom layout (extends FrameLayout). I have a bunch of view defined in xml(it doesn't matter now).
What I need to do. My custom layout has custom defined attributes, let's assume that it named footer_banner_type.
I have different Banners classes some of them I quite different from one another, so I cannot place some base banner in xml. So I have to add some banner based on attribute value.
I am extending FrameLayout . I am newbie and this is my first custom layout.
I don't know how to improve performance.
As I understand Layout iterating and inflating all child views. But if I need to add view in runtime.I don't want to make layout reiterate view hierarchy, because it will be performance issue.
My question is how to implement my task in better way.

//First create your view:
View wonderfulView = new View(this.getApplicationContext());
//Then, create its LayoutParams
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 1);
//Set those layout params in your view
wonderfulView.setLayoutParams(params);
//finaly, add the view to your ViewGroup
yourLayout.addView(wonderfulView);
That's it.
If you want to change the view container, you'll have to remove it form the previous parent like this:
View movingThing = ((YourLayoutClass)findViewById(R.id.currentContainer)).getChildAt(WhereYourViewis);
((YourLayoutClass)findViewById(R.id.currentContainer)).removeView(movingThing);
((YourLayoutClass)findViewById(R.id.newContainer)).addView(movingThing);

Related

Creating a layout Multiple times in Android

How to create a whole layout (Relative/Linear) multiple times in Android? I want the same layout to be created multiple times inside a horizontal scroll view.
You can use RecyclerView for Horizontal scrolling-
or-
Take horizontal scrollview reference in java code by findViewById.
Create one other xml for view which you want to display multiple
time.
inflate that view by getlayoutinflator. Create a loop in
view.
create a linearlayout at runtime and add those view to it by add
view
and add linearlayout to horizontal scroll view. by addview()
take a idea and modify the below code
scrollview = findViewByID(scrollview);
LinearLayout ll = new LinearLayout(this);
for(your loop){
View v= getLayoutInflator().inflate(R.layout.xml);
ll.addView(v);
}
scrollview.addView(ll);
Either you need to add inflated child views to the root view like below
RelativeLayout rootView = (RelativeLayout)findViewById(R.id.rootView);
View child = getLayoutInflater().inflate(R.layout.child, null);
rootView.addView(child);
OR you can define and include that layout multiple times inside other.
Check this link http://developer.android.com/training/improving-layouts/reusing-layouts.html
Include your reusable layout like this
<include
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="#layout/reusabelLayout" />

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

How to inflate another view from another view in 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)

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.

dynamically adding a view to activity layout

I have a custom view (an extension of a TextView) that I want to dynamically add to my Layout (don't want to include it in the main.xml file).
The book says to fetch the RelativeLayout using findViewById() in my java code then create a new instance of my custom view, then use addView on the RelativeLayout to add the new view.
I'm not getting any errors, but when I click my button to add the new view, nothing is happening (view isn't being added). Do I need to set additional properties on my custom view (layout width, layout height for example) in order for it to be shown?
EDIT: adding code
// changed to an imageview as I thought it might be easier to see an image
RelativeLayout rel = (RelativeLayout) findViewById(R.id.rellay);
MyCustomImageView mciv = new MyCustomImageView(null);
mciv.setId(5);
LayoutParams p = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
mciv.setLayoutParams(p);
mciv.setImageResource(R.drawable.someImage);
rel.Addview(mciv);
Please post your code where you add the view.
But yes, you might be missing the params for width and height. Try something like
LayoutParams p = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.FILL_PARENT);
txtView.setLayoutParams(p);
or what you would like the width and height to be. Also in xml layout, layout_width and layout_height are required attributes.

Categories

Resources