Android ScrollView with RelativeLayout - android

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

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

android - add a view which fills the screen height

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

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?

Dynamically add view to the bottom of RelativeLayout

I have in main.xml TitlePageIndicator and ViewPager, and I want to add admob (right into LinearLayout) at the bottom of RelativeLayout. How can I do this?
When I launch it, the log says nothing about errors (since there is no place to put admob), but admob is invisible, I can`t see it. (looks like admob is outside the screen because I tried to set specific sizes to ViewPager and it works perfect)
I do not want to set specific sizes to ViewPager (becouse of the different screen sizes)
Thanks.
My main.xml is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.viewpagerindicator.TitlePageIndicator
android:id="#+id/indicator"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"/>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/indicator"/>
<LinearLayout
android:id="#+id/for_ads"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/viewpager"/>
</RelativeLayout>
UPD.
solved
I used this answer and it works perfect for me
First, your RelativeLayout needs an ID if you want to reference it:
RelativeLayout rLayout = (RelativeLayout)findViewById(R.id.yourRelativeId);
Then create some LayoutParams for the object (in this case, your admob adview) that tell it to align itself to the bottom (and not align to any other views, this way it isn't pushed off-screen or moved by the other views):
RelativeLayout.LayoutParams rLParams =
new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
rLParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 1);
Next, add the view to your RelativeLayout with your LayoutParams:
rLayout.addView(yourAdView, rLParams);

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

Categories

Resources