android - add a view which fills the screen height - android

I want to programmatically add a View to mainHolder that is declared as below:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000" >
<LinearLayout
android:id="#+id/mainHolder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
android:orientation="horizontal" />
I want the new view to fill the screen height. How is that possible?

Try this :
LinearLayout mainHolder = (LinearLayout)findViewById(R.id.mainHolder);
//untested adding of view I got it from http://stackoverflow.com/questions/5731487/how-to-add-views-to-linear-layout
mainHolder.addView(View, mainHolder);
//this is tested however
View.setMinimumHeight(mainHolder.getHeight());

set Linear Layout property
android:layout_width="match_content"
android:layout_height="match_content"
Then add following code:
LinearLayout mainHolder=(LinearLayout)findViewById(R.id.mainHolder);
RelativeLayout.LayoutParams layer=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
layer.addRule(RelativeLayout.ALIGN_PARENT_TOP);
mainHolder.addView(layer);
Same as above code, you can add your views programatically

Related

How can i set LinearLayout into another LinearLayout programatically in android?

I want to set the layout inside another layout programatically in android.
Here, is my xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:baselineAligned="false"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/Layout_second_overs"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="6"
android:orientation="vertical">
<!--Second textview overs-->
</LinearLayout>
<LinearLayout
android:id="#+id/Layout_second_balls"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<!--Second textview balls--> //Here i want to put one linear layout.
</LinearLayout>
</LinearLayout>
</LinearLayout>
Please, help me to solve out this problem.
LinearLayout parent = (LinearLayout) findViewById(R.id.parentlayout);
LinearLayout child = new LinearLayout(getApplicationcontext());
child .LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
parent.addView(child);
You can add Layout inside root Layout using below way.
// This is your parent `LinearLayout`
LinearLayout parent = (LinearLayout) findViewById(R.id.parentlayout);
// Create New LinearLayout inside parent layout using below way.
LinearLayout child = new LinearLayout(context);
parent.addView(child);
Additional you can set width and height dynamically.
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
child.setLayoutParams(layoutParams);

setting linearlayout height does not work

I have a listview inside a linear layout whose height is set as "WRAP_CONTENT", so the list is appearing inside the linear layout, when i populate the list, i attach some hidden list items within each list row, i want to show these items onListitemClick listner,
Everything is working fine,but if i have only one list child, then i have to scroll the view to see that layout that is visible now,
My question is:
I want to increase the height of that linear layout when the list item is clicked and and the view is visible..
here is xml..
<LinearLayout
android:id="#+id/listContainer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
//used this relative layout to set different backgroung for each time..
<RelativeLayout
android:id="#+id/listHeader"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/categoryTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:textColor="#FFFFFF"
android:textSize="15sp" />
</RelativeLayout>
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
When the hidden item is visible, then i use this code to increase height,
LayoutParams paramsLinearLayout;
LinearLayout layoutContainer;
layoutContainer = (LinearLayout) findViewById(R.id.listContainer);
paramsLinearLayout = layoutContainer.getLayoutParams();
layoutContainer.setMinimumHeight(paramsLinearLayout.WRAP_CONTENT);
Use this piece of code :
LinearLayout linear = new LinearLayout(getContext());
LayoutParams linearParams=new LayoutParams(LayoutParams.MATCH_PARENT,Height what you want);
instead of :
LayoutParams paramsLinearLayout;
LinearLayout layoutContainer;
layoutContainer = (LinearLayout) findViewById(R.id.listContainer);
paramsLinearLayout = layoutContainer.getLayoutParams();
layoutContainer.setMinimumHeight(paramsLinearLayout.WRAP_CONTENT);
I think you should not have a view with height match_parent inside a layout with height wrap_content, as in your case:
<LinearLayout
android:id="#+id/listContainer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
//used this relative layout to set different backgroung for each time..
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Can't you set linearlayout's height to match_parent as well?

Android ScrollView with RelativeLayout

I have a fragment with two RelativeLayout, one inside the other. This fragment is inside a tab. I want that the internal layout(layoutInternal) is scrollable, but with the following code does not work. I create the view object into the layoutInternal dinamically. Where is my mistake?
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutExsternal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="#dimen/fragmentHall_10dp_margin"
android:layout_marginTop="#dimen/fragmentHall_10dp_margin">
<RelativeLayout
android:id="#+id/layoutInternal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</RelativeLayout>
</ScrollView>
</RelativeLayout>
The following code is where i insert the view object (table) inside the intenalLayout.
while (iteratorTables.hasNext()) {
[...] //Here i calculate the dimension and coordinates
b_table = new ButtonTable(SelzApplication.getAppContext(),tableMap.getValue(), widthTable, heightTable);
b_table.setX(Math.round((tableMap.getValue().getX())* Xratio));
b_table.setY(Math.round((tableMap.getValue().getY())* Yratio));
//add Touch Listeners to the button
b_table.setOnLongClickListener(new OnLongTuchDragDrop());
b_table.setOnClickListener(new TableOnClick());
layoutInternal.addView(b_table, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
This is the screenshot of my app.
The RelativeLayout layoutInternal has the height initially set as android:layout_height="wrap_content" but its content is initially empty because the content is created dynamically; for this reason is not scrollable.
To solve this, when you finish to draw the tables into RelativeLayout layoutInternal, you have to set its height dynamically as below:
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) layoutInternal.getLayoutParams();
params.height = max_y_coordinate_used;
layoutInternal.setLayoutParams(params);

dynamically adding two views one below other

I want to add two views one after the other, I used this way but I am getting an error.
This is my XML.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/parent"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/rel1"
android:layout_alignParentTop="true"
></RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/rel2"
android:layout_below="#+id/rel1"
></RelativeLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
In the two Relative layouts rel1,and rel2 I will add my custom views which are going to be drawn dynamically.
my code:
setContentView(R.layout.main);
RelativeLayout rlstat1=(RelativeLayout)findViewById(R.id.rel1);
RelativeLayout rlstat2=(RelativeLayout)findViewById(R.id.rel2);
RelativeLayout.LayoutParams para1 = new RelativeLayout.LayoutParams(
viewWidth, viewHeight);
RelativeLayout.LayoutParams para2 = new RelativeLayout.LayoutParams(
viewWidth, viewHeight);
rlstat1.setLayoutParams(para1);
rlstat1.addView(mView);
para2.addRule(RelativeLayout.BELOW, R.id.rel1);
rlstat2.addView(mView2);
Here mView and mView2 are two view type which I want to set in the two relative layouts.
ViewWidth and ViewHeight are the width and height of the screen it is running.
The problem:
If only one view is added i.e. mView or mView2 it's showing but if both the views are added(like in above) then only one relative layout is shown.
I want that both of my views get set one below other.
Hope I am clear in my question.Can you please tell me the appropriate way to do this.
you are setting layout params to views dynamically in activity and these are new layout params objects, so your rule to add rel2 to below of rel1 gets cleared, instead try:
setContentView(R.layout.main);
RelativeLayout rlstat1=(RelativeLayout)findViewById(R.id.rel1);
RelativeLayout rlstat2=(RelativeLayout)findViewById(R.id.rel2);
RelativeLayout.LayoutParams para1 = rlStat1.getLayoutParams();
para1.width=LayoutParams.FILL_PARENT;
para1.height=LayoutParams.FILL_PARENT;
RelativeLayout.LayoutParams para2 = rlStat2.getLayoutParams();
para2.width=LayoutParams.FILL_PARENT;
para2.height=LayoutParams.FILL_PARENT;
rlstat1.setLayoutParams(para1);
rlstat1.addView(mView);
para2.addRule(RelativeLayout.BELOW, R.id.rel1);
rlstat2.addView(mView2);

Adding a view to a LinearLayout at a specified position

I have the following main.xml file with a LinearLayout
<?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"
android:weightSum="1" android:id="#+id/llid">
<TextView android:text="Client profile"
android:id="#+id/ProfileName"
android:layout_width="fill_parent"
android:textStyle="bold"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
</TextView>
<TextView android:text="Specs"
android:id="#+id/Specs"
android:layout_width="fill_parent"
android:textStyle="bold"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
</TextView>
</LinearLayout>
I add an image to the LinearLayout via code at runtime like so
ImageView image = new ImageView(this);
image.setImageBitmap(bmp);
LinearLayout ll = (LinearLayout) findViewById(R.id.llid);
ll.addView(image);
However, I want to add the ImageView between the 2 TextViews in my LinearLayout. I can't seem to find a way in the android docs to add a view before another view, or after. How can I do this?
NB I call
setContentView(R.layout.main);
Before I add the ImageView to the LinearLayout.
When adding a View to a ViewGroup, you can specify an index which sets the position of the view in the parent.
You have two views and so (counting from zero) you would want to add at the 1st position; just call ll.addView(image, 1); to have it placed in between the two TextViews.
The docs state you can use the index to insert it where you want. I see you are using the signature of the view only, did you try the signature with the index parameter?
public void addView(View child, int index)
I faced a similar problem. In my case I wanted to add a LinearLayout at last position of another LinearLayout. To accomplish it, I did:
LinearLayout parentLayout = (LinearLayout) findViewById(R.id.parentLayout);
LinearLayout layout = new LinearLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
// add others views to your linear layout
parentLayout.addView(layout, parentLayout.getChildCount());
setContentView(R.layout.main);
ImageView img = (ImageView) findViewById(R.id.imagefield);
img.setImageResource(your_image_here);
and in the xml
<?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"
android:weightSum="1"
android:id="#+id/llid">
<TextView android:text="Client profile"
android:id="#+id/ProfileName"
android:layout_width="fill_parent"
android:textStyle="bold"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
</TextView>
<ImageView android:id="#+id/imagefield"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView>
<TextView android:text="Specs"
android:id="#+id/Specs"
android:layout_width="fill_parent"
android:textStyle="bold"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
</TextView>
</LinearLayout>
Add an ImageView into the xml, and if its not being used, make it invisible (image.setVisibility(View.INVISIBLE)). It may not show anything anyway when no image is set.
To get position of view in a view group
val targetPosition = oldLL.indexOfChild(viewToAdd)
To add a view at a position in a view group
newLL.addView(viewToAdd,targetPosition)

Categories

Resources