Can't correct add child to LinearLayout - android

I've a simple task - add child views to a LinearLayout dynamically, but I don't know, how to do it in a proper way.
There are two ways:
1. Inflate view and passed parent container to the inflate method.
View view = LayoutInflater.from(getActivity()).inflate(R.layout.item, container, true);
At this case, when I've more than one child, LayoutInfater returns the same child view object every time, when inflate it. So I can proper initialized other child views.
2. The next way is to use LinearLayout method addView(View view).
The problem is, that the child view lose it's LayoutParams state.
And I must set new LayoutParams for child programmatically. It's a not good practice.
Also we can put child in the complimentary wrapper layout in his own layout resource file. But it's also a not good practice put layout to the wrapper.
My child resource:
<RelativeLayout android:gravity="center_vertical"
android:paddingLeft="25dp"
android:layout_width="match_parent"
android:layout_height="#dimen/list_item_height">
<TextView android:id="#+id/tv_name"
android:text="Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
So what is the proper way to add child to LinearLayout, without adding LayoutParams programmatically and without wrapper layout?
Thank's a lot for help!

Did you try this way?
View view= LayoutInflater.from(getActivity()).inflate(R.layout.item, null, false);
parentView.addView(view);

if you want to add child to a RelativeLayout,you need to set LayoutParams for child view ,and if you want to add child to a LinerLayout,you don't need to set LayoutParams,so if you want to custom layoutParams,i suggest you to use RelativeLayout,or you can make a wrapper view for child View ,just like:
//inflate a wrapper view
View child = LayoutInflater.from(getActivity()).inflate(R.layout.item, container, false);
//add to a linerLayout
LinerLayout.addChild(child)

Try this way,
View view = LayoutInflater.from(getActivity()).inflate(R.layout.item, **container**, false);
layout.addView(view);

Related

How to add view in custom layout in runtime

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

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" />

ListView / GridView: Setting child layout properly

I want to create a ListView (same question is relevant for a GridView) where the items have very specific LayoutParams. Take this example where I want each row to be a LinearLayout with a height of 100dp:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="100dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/pd_textview"
android:gravity="center_vertical"/>
</LinearLayout>
When I use this XML layout with a standard BaseAdapter implementation of getView() that inflates the views none of the LayoutParams for my LinearLayout get applied (each row simply wraps around the size of the text in the TextView):
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Inflate view if necessary
if (convertView == null) {
convertView = ((Activity) mContext).getLayoutInflater().inflate(ID_LAYOUT, null, false);
}
// Return the view
return convertView;
}
Fishing around in the appropriate section of the Android documentation, it says the following about getView() which explains the current behaviour:
Get a View that displays the data at the specified position in the
data set. You can either create a View manually or inflate it from an
XML layout file. When the View is inflated, the parent View (GridView,
ListView...) will apply default layout parameters unless you use
inflate(int, android.view.ViewGroup, boolean) to specify a root view
and to prevent attachment to the root.
My question is simply, how should I actually be doing this? The suggestion of using a root view with the desired LayoutParams seems convoluted given that the LayoutParams I want to inflate the view with are in the view that I'm trying to inflate (yes I could inflate a static view and leave it as a member of the class to reference against... but that seems silly). The other option would be to only define the contents of the row item in XML and manually create the container and its LayoutParams every time?
Is there a 'correct' way (i.e. not a wasteful hack) that I'm missing to simply create items with the LayoutParams defined in the XML?
I'd have preferred to put this in a comment, but I don't have the 50 reputation needed to do that...
There is detailed explanation of the issue here:
http://www.doubleencore.com/2013/05/layout-inflation-as-intended/
The main point is this:
"The problem with this is android:layout_xxx attributes are always be evaluated in the context of the parent view. As a result, without any known parent, all LayoutParams you declared on the root element of your XML tree will just get thrown away [,...]"
So, indeed, the solution is:
convertView = ((Activity) mContext).getLayoutInflater().inflate(ID_LAYOUT, parent, false);

How to merge the three views into a blank view and display?

I want to set a ScrollView for some views. I need to merge some view into one view and add to ScrollView.
The three views are listView, custorm view and Gallery.
Who knows how to merge the three views into a blank view and display?
In XML you can structure your app like the following snippet:
<ScrollView>
<LinearLayout>
<CustomView/>
<ListView/>
<Gallery/>
</LinearLayout>
</ScrollView>
Alternatively in code, you can do it like so:
ScrollView SV = (ScrollView) findViewById(R.id.my_scrollview);
LinearLayout LL = (LinearLayout) findViewById(R.id.my_linear_layout);
LL.addView(my_customView)
LL.addView(my_istView)
LL.addView(my_gallery)
SV.addView(LL);
hope that gives you some pointers in how to achieve what you'd like to
You can create those objects and assign them inflated elements. I mean, if the listView is in listView.xml you can inflate it with getLayoutInflater().inflate(R.layout.listView,null) and put in a ListView object. Then you can add it to a ViewGroup.
Inflate the ScrollView, you can create programmatically too. ScrollView scroll=new ScrollView(this). Don't forget adding LayoutParams to the view if you do it programmatically.
ViewGroup scrollView = (ViewGroup) getLayoutInflater().inflate(R.layout.scrollView, null);
Then you inflate or create the other views the same way. Or you can get it from an activity with findViewById(R.id.idOfView1).
Finally just add the views you've created, inflated... with scrollView.addView(View child).
You need a ViewGroup to add views as childs.

How to Programmatically Add Views to Views

Let's say I have a LinearLayout, and I want to add a View to it, in my program from the Java code. What method is used for this? I'm not asking how it's done in XML, which I do know, but rather, how can I do something along the lines of this sample code?
(One View).add(Another View)
Like one can do in Swing.
Calling addView is the correct answer, but you need to do a little more than that to get it to work.
If you create a View via a constructor (e.g., Button myButton = new Button();), you'll need to call setLayoutParams on the newly constructed view, passing in an instance of the parent view's LayoutParams inner class, before you add your newly constructed child to the parent view.
For example, you might have the following code in your onCreate() function assuming your LinearLayout has id R.id.main:
LinearLayout myLayout = findViewById(R.id.main);
Button myButton = new Button(this);
myButton.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
myLayout.addView(myButton);
Making sure to set the LayoutParams is important. Every view needs at least a layout_width and a layout_height parameter. Also getting the right inner class is important. I struggled with getting Views added to a TableRow to display properly until I figured out that I wasn't passing an instance of TableRow.LayoutParams to the child view's setLayoutParams.
The best way I found is to use the inflate static method of View.
View inflatedView = View.inflate(context, yourViewXML, yourLinearLayout);
where yourViewXML is something like R.layout.myView
please notice that you need a ViewGroup in order to add a view (which is any layout you can think of)
so as an example lets say you have a fragment which it view already been inflated and you know that the root view is a layout, and you want to add a view to it:
View view = getView(); // returns base view of the fragment
if (view == null)
return;
if (!(view instanceof ViewGroup))
return;
ViewGroup viewGroup = (ViewGroup) view;
View popup = View.inflate(viewGroup.getContext(), R.layout.someView, viewGroup);
EDIT:
Kotlin code for the example above (view is the getView() of a fragment)
(view as? ViewGroup)?.let {
View.inflate(context, R.layout.add_credit_card, it)
}
To add the view programmatically, you can do:
LinearLayout rlmain = new LinearLayout(this);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);
LinearLayout ll1 = new LinearLayout (this);
ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.logo);
LinearLayout .LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
iv.setLayoutParams(lp);
ll1.addView(iv);
rlmain.addView(ll1);
setContentView(rlmain, llp);
You can also add any number of views.
LinearLayout is a subclass of ViewGroup, which has a method called addView. The addView method should be what you are after.
The idea of programmatically setting constraints can be tiresome. This solution below will work for any layout whether constraint, linear, etc. Best way would be to set a placeholder i.e. a FrameLayout with proper constraints (or proper placing in other layout such as linear) at position where you would expect the programmatically created view to have.
All you need to do is inflate the view programmatically and it as a child to the FrameLayout by using addChild() method. Then during runtime your view would be inflated and placed in right position. Per Android recommendation, you should add only one childView to FrameLayout [link].
Here is what your code would look like, supposing you wish to create TextView programmatically at a particular position:
Step 1:
In your layout which would contain the view to be inflated, place a FrameLayout at the correct position and give it an id, say, "container".
Step 2
Create a layout with root element as the view you want to inflate during runtime, call the layout file as "textview.xml" :
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
</TextView>
BTW, set the layout-params of your frameLayout to wrap_content always else the frame layout will become as big as the parent i.e. the activity i.e the phone screen.
android:layout_width="wrap_content"
android:layout_height="wrap_content"
If not set, because a child view of the frame, by default, goes to left-top of the frame layout, hence your view will simply fly to left top of the screen.
Step 3
In your onCreate method, do this :
FrameLayout frameLayout = findViewById(R.id.container);
TextView textView = (TextView) View.inflate(this, R.layout.textview, null);
frameLayout.addView(textView);
(Note that setting last parameter of findViewById to null and adding view by calling addView() on container view (frameLayout) is same as simply attaching the inflated view by passing true in 3rd parameter of findViewById(). For more, see this.)
One more way to add view from Activity
ViewGroup rootLayout = findViewById(android.R.id.content);
rootLayout.addView(view);
You guys should also make sure that when you override onLayout you HAVE to call super.onLayout with all of the properties, or the view will not be inflated!

Categories

Resources