Adding multiple child views to parent view - android

I have a LinearLayout view element inside a ScrollView (main.xml):
<ScrollView ...>
<LinearLayout
android:id="#+id/root"
android:orientation="vertical"
>
<TextView .../>
<EditText .../>
...
</LinearLayout>
</ScrollView>
As you see above, there are also some other elements inside the root LinearLayout .
Now, I would like to programmatically(dynamically) add more views to the LinearLayout (id="root").
I tried the following way to add more child views to this root:
Firstly, I created my child view which is in a separate layout file:
child.xml
<LinearLayout
android:id="#+id/child"
>
<TextView id="mytxt"... />
<ListView id="mylist".../>
</LinearLayout>
Secondly, I inflate & get two instances of above child view, initialize elements inside:
/***inflate 1st child, initialize its elements***/
LinearLayout child_1 = (LinearLayout) inflater.inflate(R.layout.child, null);
TextView txt1 = (TextView)child_1.findViewById(R.id.mytxt);
txt1.setText("CAR");
ListView list1 = (ListView)child_1.findViewById(R.id.mylist);
// Code to initialize 'list1' (I did not paste code here)
/*** inflate 2nd child, initialize its elements ****/
LinearLayout child_2 = (LinearLayout) inflater.inflate(R.layout.child, null);
TextView txt2 = (TextView)child_2.findViewById(R.id.mytxt);
txt2.setText("PLANE");
ListView list2 = (ListView)child_2.findViewById(R.id.mylist);
// Code to initialize 'list2' (I did not paste code here)
Finally, I add them to root LinearLayout:
//get root
View contentView = inflater.inflate(R.layout.main, null);
LinearLayout root = (LinearLayout) contentView.findViewById(R.id.root);
//add child views
root.add(child_1);
root.add(child_2);
When I run my app on device, I can only see child_2 layout without seeing child_1 under 'root', why??

in LinearLayout default orientation is horizontal set it vertical.......
from
http://developer.android.com/reference/android/widget/LinearLayout.html
see
The default orientation is horizontal.
and you set text in txt1.setText("PLANE"); set in txt2.setText("PLANE");
both text set in same textview.....
txt1.setText("CAR");
txt1.setText("PLANE");

How do you create your layout? Do you do it through setContentView(int)? Then you should retrieve that instance by doing this in your activity:
findViewById(R.id.root);

Related

android margins not working when dynamically inserting views

I have a simple view:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/contact_selected"
android:layout_marginTop="10dp"
android:layout_marginEnd="5dp"
android:orientation="horizontal"
android:padding="3dp">
<TextView
android:id="#+id/txt_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Billy Bob"
/>
</LinearLayout>
When I statically copy the LinearLayout markup into my main activity layout, the margins are as expected. However, when I add the view into the main activity layout dynamically, the margins are ignored. Here's how I insert the view
LayoutInflater inflater = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.test, null);
TextView txt_title = (TextView)view.findViewById(R.id.txt_title);
txt_title.setText("Dynamic #1");
llayout.addView(view, llayout.getChildCount()-1);
View view2 = inflater.inflate(R.layout.test, null);
txt_title = (TextView)view2.findViewById(R.id.txt_title);
txt_title.setText("Dynamic #2");
llayout.addView(view2, llayout.getChildCount()-1);
Here's what it looks like:
The container in the main layout is a LinearLayout, which is a child of a HorizontalScrollView. Any insight is appreciated.
When dynamically adding views, you shouldn't inflate the View with a null ViewGroup parent. So, in other words you should be using inflater.inflate(R.layout.test, linearLayout, false);. The parent is used when determining what type of layout parameters to generate. Pass your parent container (in this case, your linear layout), so it correctly instantiates the ViewGroup.MarginLayoutParams from your XML.
This happens because you need to give "Margin" to layouts dynamically. You can do this by creating an object of "LayoutPrams", like this:-
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(30, 20, 30, 0);
Here , you can set the LayoutParams to the linearlayout:
ll.addView(okButton, layoutParams);
Hope it helps.
First, you have to get display density.
related docs are https://developer.android.com/reference/android/util/DisplayMetrics.html
and get ID which you want set margin view.
for my case,
layout_login_box = (ConstraintLayout)findViewById(R.id.login_layout_box);
float density = getResources().getDisplayMetrics().density;
ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams)layout_login_box.getLayoutParams();
params.setMargins((int) (24 * density),0,(int) (24 * density),(int) (16 * density));
layout_login_box.setLayoutParams(params);
Also, you can change ConstraintLayout to your own view.

Add view to the top of FrameLayout

main.xml:
<FrameLayout id="parent">
<ImageView layout_width="match_parent" layout_height="match_parent"/>
</FrameLayout>
ActivityMain.java
onCreate() {
super.onCreate();
setContentView(R.layout.nain);
ViewGroup parent = (ViewGroup) findViewById(R.id.parent);
TextView textView = new TextView(this);
textView.setText("Hello, world");
parent.addView(textView);
// parent.addView(textView, 0);
// parent.addView(textView, 1);
// parent.bringChildToFont(textView);
}
I inflated the code above as the content, and then, how can I add another view to the FrameLayout and it's on the top of the ImageView?
I had tried some several hours, but still don't know how to programmatically add another view to the top of the FrameLayout.
I found that it's easy to achieve the goal by add two children to the XML file. And at the end, I use a ViewStub and complete my work.
But I'm so curious about how to do this without a ViewStub.
my solution:
main.xml:
<FrameLayout id="parent">
<ImageView layout_width="match_parent" layout_height="match_parent"/>
<ViewStub id="stub">
</FrameLayout>
widget_text_view.xml:
<TextView />
ActivityMain.java
onCreate() {
super.onCreate();
setContentView(R.layout.nain);
ViewStub stub = (ViewStub) findViewById(R.id.stub);
stub.setLayoutResource(R.layout.widget_text_view);
TextView textView = (TextView) stub.inflate();
}
yes you can add item to the top of another view. android:layout_gravity attribute. visite This. Or This

LinearLayout not redrawing after invalidate

I have the following LinearLayout in which I'm dynamically inserting views:
<LinearLayout
android:id="#+id/loyaltycardlist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:background="#00f"
android:orientation="vertical"/>
and the Java code:
LinearLayout loyaltyCardLL = (LinearLayout)findViewById(R.id.loyaltycardinfo);
for(MyCard thisCardNode : mLoyaltyCards)
{
LinearLayout item = (LinearLayout)getLayoutInflater().inflate(
R.layout.activity_view_node_details_loyaltycard, null);
item.setTag(thisCardNode);
TextView tv = (TextView)item.findViewById(R.id.loyaltycard_item_title);
tv.setText(thisCardNode.getTitle());
... etc
loyaltyCardLL.addView(item);
}
loyaltyCardLL.invalidate();
The containing linear layout never expands to hold the layouts I've added, even though I'm calling invalidate().
And tips on why this is happening?

Inflate child with 0dp layout width & weight (programmatically)

Simple: I want to inflate parent with 1 child which has 0dp width.
Parent xml:
<com.example.KeyPad // extend LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="4" // for the children
android:layout_weight="1" // this is for its parent
android:clickable="true"
android:background="#color/MidnightBlue" >
The child class:
public class KeyButton extends RelativeLayout implements View.OnClickListener{
public KeyButton(Context c ) {
super(c);
RelativeLayout v = (RelativeLayout) LayoutInflater.from(c).inflate(R.layout.key_button, this, true);
}
}
}
which uses the R.layout.key_button xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_weight="1"
android:layout_width="0dp"
android:background="#android:color/holo_red_dark"
android:layout_height="wrap_content">
<TextView ... />
</RelativeLayout>
And the child is getting added by:
Parent.addView(new KeyButton(context) );
The problem is that android:layout_weight doesn't look to take action and the layout_width of the child stays at "0dp". If I change the width to 50dp I can see the child that has been correct inflated.
Also tried to add the parameters programmatically when getting added:
KeyButton bt = new KeyButton(context);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT,1.0f);
bt.setLayoutParams(lp);
Parent.addView(bt);
How can I inflate the child with 0dp/weight? Of course as you can see I have defined the weight_sum of the parent.
You have used a LinearLayout.LayoutParams but the parent of your button are a RelativeLayout.
Try this :
KeyButton bt = new KeyButton(context);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(0, RelativeLayout.LayoutParams.WRAP_CONTENT,1.0f);
Parent.addView(bt, lp);
You can simply set the visibility property to gone or hidden by following code :
bt.setVisibility(View.GONE);
You can find more information about setting views here

How do I add elements dynamically to a view created with XML

I'm trying to add dynamic content to a view that has been created with XML.
I have a view "profile_list" that has a ScrollView that I like to add some elements to.
Here is some code of what I'm trying to do.
// Find the ScrollView
ScrollView sv = (ScrollView) this.findViewById(R.id.scrollView1);
// Create a LinearLayout element
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
// Add text
tv = new TextView(this);
tv.setText("my text");
ll.addView(tv);
// Add the LinearLayout element to the ScrollView
sv.addView(ll);
// Display the view
setContentView(R.layout.profile_list);
The plan is to add a TableLayout and fill it dynamically and not just a dummy text but first I must get this to work.
Any help is welcome.
Kind Regards
Olle
I found the solution!
Stupid me I had left a element in my ScrollView in my XML-file!
Anyway her is a working example:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.profile_list, null);
// Find the ScrollView
ScrollView sv = (ScrollView) v.findViewById(R.id.scrollView1);
// Create a LinearLayout element
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
// Add text
TextView tv = new TextView(this);
tv.setText("my text");
ll.addView(tv);
// Add the LinearLayout element to the ScrollView
sv.addView(ll);
// Display the view
setContentView(v);
And the XML-file to match
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ScrollView
android:id="#+id/scrollView1"
android:clickable="true"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_marginBottom="50px"
android:layout_height="fill_parent">
</ScrollView>
</LinearLayout>
Hope this will help someone. Thanks for all help!
The way that you are adding things to the view is basically right - you just get the container object by its ID and then add them.
It looks like you are setting the content view to be the wrong view though. You should inflate the view, add the children and set it as the content view, or set the content view from a resource ID then do the manipulation. What you are doing is manipulating a view and then adding a different one. Try moving your setContentView(...) to the start of the block.

Categories

Resources