Adding LinearLayout xml in a ScrollView - android

I have a xml with a ScrollView and a LinearLayout as a child of ScrollView.
<?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" >
<ViewStub
android:id="#+id/social_empty_stub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:inflatedId="#+id/social_empty_stub_inflted"
android:layout="#layout/empty_screen"
android:visibility="gone" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/activity_stream"
android:background="#DCDCDCDC"
android:fadingEdge="none" >
<LinearLayout
android:id="#+id/activity_stream_wrap"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</RelativeLayout>
and I have another xml with a LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<!-- List Item on activity stream -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout_Activity_Streams"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="5dp" >
<!-- Avatar -->
<com.example.bb.ShaderImageView
android:id="#+id/imageView_Avatar"
android:layout_width="#dimen/social_avatar_size"
android:layout_height="#dimen/social_avatar_size"
android:layout_marginLeft="5dp"
android:scaleType="fitXY" />
<!-- Content -->
<LinearLayout
android:id="#+id/relativeLayout_Content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dip"
android:layout_marginRight="5dp"
android:background="#drawable/news_browser_background_shape"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:id="#+id/textView_Name"
style="#style/textview_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:maxLines="5"
android:textStyle="bold" />
<TextView
android:id="#+id/textView_Message"
style="#style/textview_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginTop="3dip"
android:maxLines="5" />
<TextView
android:id="#+id/textview_temp_message"
style="#style/textview_content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginTop="3dip"
android:maxLines="5"
android:visibility="gone" />
<ViewStub
android:id="#+id/attached_image_stub_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="2dp"
android:inflatedId="#+id/attached_image_stub_inflate"
android:layout="#layout/activity_image_display_layout"
android:padding="5dp"
android:visibility="gone" />
<TextView
android:id="#+id/activity_comment_view"
style="#style/textview_content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginTop="3dip"
android:maxLines="5"
android:visibility="gone" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp"
android:layout_marginRight="10dp"
android:layout_marginTop="7dp"
android:gravity="center_vertical">
<ImageView
android:id="#+id/activity_image_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/ImageDesc"
android:layout_marginLeft="5dp"
android:padding="2dp"
android:scaleType="fitXY" />
<!-- Time text view -->
<TextView
android:id="#+id/textView_Time"
style="#style/textview_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/activity_image_type"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:gravity="center_horizontal"
android:layout_marginRight="3dp" />
<!-- Comment button -->
<Button
android:id="#+id/button_Comment"
style="#style/textview_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#drawable/social_activity_browser_comment_button_shape"
android:gravity="center_vertical"
android:padding="2dp" />
<!-- Like button -->
<Button
android:id="#+id/button_Like"
style="#style/textview_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/button_Comment"
android:background="#drawable/social_activity_browser_like_button_shape"
android:gravity="center_vertical"
android:padding="2dp" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
Now I want to add the LinearLayout in second xml programmatically in ScrollView of first xml. I am using following code
setContentView(R.layout.activitybrowserview);
View view = LayoutInflater.from(context).inflate(R.layout.activitybrowserviewcell, null);
LinearLayout ll = (LinearLayout) view.findViewById(R.id.RelativeLayout_Activity_Streams);
ScrollView sv = (ScrollView) findViewById(R.id.activity_stream);
sv.addView(ll);
I get following error, how can I avoid that. I don't understand the error.
IllegalStateException "Scrollview can host only one direct child"

Scrollview can host only one direct child,the scrollvie has direct child yet(android:id="#+id/activity_stream_wrap"),u should add view with this one.
setContentView(R.layout.activitybrowserview);
View view = LayoutInflater.from(context).inflate(R.layout.activitybrowserviewcell, null);
LinearLayout ll = (LinearLayout) view.findViewById(R.id.RelativeLayout_Activity_Streams);
LinearLayout parentLl = (LinearLayout ) findViewById(R.id.activity_stream_wrap);
parentLl .addView(ll);

In your first XML file you have
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/activity_stream"
android:background="#DCDCDCDC"
android:fadingEdge="none" >
<LinearLayout
android:id="#+id/activity_stream_wrap"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
If you remove the <LinearLayout> here, then you can add another LinearLayout instead. You can either remove it directly from the XML file or remove it programatically.

Just add a Linear Layout as a container to your current LinearLayout and the LinearLayout that you want to add pragmatically. I think this should solve your problem. ScrolView Can contain only one child as the exception says the same.
<ScrolView>
<LinearLayout> this will be the container
<linearlayout> the one that is currently in the first xml file
</LinearLayout>
</LinearLayout>
</ScrolView>
Hope this will help.

setContentView(R.layout.activitybrowserview);
View view = LayoutInflater.from(context).inflate(R.layout.activitybrowserviewcell, null);
LinearLayout ll = (LinearLayout) view.findViewById(R.id.RelativeLayout_Activity_Streams);
ScrollView sv = (ScrollView) findViewById(R.id.activity_stream);
sv.removeAllViews();
sv.addView(ll);

Related

Inflating RelativeLayout View into horizontal scroll view deletes layout settings

Sorry for the long question..
I have Relativelayout of elements I am adding dynamically into Linearlayout of Horizontalscrollview.
When I see the layout the preview seems great, but when I am adding it programmatically it ruins the view.. the Text moved to another locations on the layout, and the padding is ignored..
This is the Relativelayout element deal_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="310dp"
android:layout_height="wrap_content"
android:layout_marginEnd="15dp"
android:layout_marginRight="15dp"
android:id="#+id/deal_layout"
>
<ImageView
android:layout_width="310dp"
android:layout_height="130dp"
android:src="#drawable/deal_square"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:contentDescription="deals background" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:paddingTop="25dp"
android:id="#+id/from_to_deals_txt"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="from "
android:textAppearance="#style/from_to_deals"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Source"
android:textAppearance="#style/source_deals"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" to "
android:textAppearance="#style/from_to_deals"
/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="destination"
android:textAppearance="#style/dst_deals"
android:layout_below="#id/from_to_deals_txt"
android:layout_alignRight="#id/from_to_deals_txt"
android:paddingTop="10dp"
android:id="#+id/dst_deals_txt"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1099 X"
android:textAppearance="#style/price_deals"
android:layout_below="#id/dst_deals_txt"
android:layout_alignRight="#id/from_to_deals_txt"
android:layout_alignEnd="#id/from_to_deals_txt"
android:paddingTop="10dp"
android:id="#+id/price_deal_txt"
/>
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:src="#drawable/ic_launcher"
android:background="#000000"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
/>
</RelativeLayout>
This is the Horizontal scroll view:
<?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="fill_parent"
android:background="#drawable/bg"
android:padding="10dp">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/dealsView"
android:paddingTop="10dp"
>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
I am adding the Relativelayout elements with this java function:
LinearLayout dealsView;
dealsView = (LinearLayout)findViewById(R.id.dealsView);
for(int i=0;i<5;i++)
{
LayoutInflater mInflater ;
mInflater = LayoutInflater.from(this);
View cur_deal = mInflater.inflate(R.layout.deal_layout, null);
dealsView.addView(cur_deal);
}
In the preview it looks like this:
On the application it looks like this:
Does anyone has idea what am I doing wrong?
When I am adding the elements with copy paste to the linearlayout it seems great, and the margin is works fine, only from the code it not works..
Thanks!
Instead of passing null to the inflater, pass the Views intended parent (dealsView) so that the LayoutParams from the root can be used to inflate.
View cur_deal = mInflater.inflate(R.layout.deal_layout, dealsView, false);
I believe the issue is caused by all of your Views using width="wrap_content" but without the inflater knowing the root it cannot properly inflate that value.

ListView inside a ScrollView issue in Android

Here is my XML layout. I have a list view inside my scrollview. I am setting the height manually if I change the listview height to wrap_content it is not working. And also if the data grows beyond the limit it is hiding. I am seeing many answers to solve this. What is the best option to solve it for my case?
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:focusableInTouchMode="true" >
<LinearLayout
android:id="#+id/store_scroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="#c6c6c6"
android:padding="10dp" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
layout_alignParentLeft="true"
android:text="Stores"
android:textSize="20dp" />
<TextView
android:id="#+id/merchant_count_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="0 Results" />
</RelativeLayout>
<Gallery
android:id="#+id/gallery1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:gravity="left"
android:listSelector="#color/gridviewlistselector"
android:paddingLeft="10dp"
android:paddingRight="20dp"
android:paddingTop="10dp"
android:spacing="10dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="55dp"
android:background="#c6c6c6"
android:padding="10dp" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
layout_alignParentLeft="true"
android:text="Products"
android:textSize="20dp" />
<Spinner
android:id="#+id/filter_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_toRightOf="#+id/textView1" />
<TextView
android:id="#+id/product_count_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="0 Results" />
</RelativeLayout>
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="4800dp"
android:layout_weight="1"
android:drawSelectorOnTop="true"
android:listSelector="#color/gridviewlistselector"
android:orientation="vertical" />
</LinearLayout>
</LinearLayout>
</ScrollView>
Short answer: You can't (shouldn't) have a ListView wrapped in a ScrollView.
Long answer: As Romain Guy (UI-guru at Google themselves) once said:
Using a ListView to make it not scroll is extremely expensive and goes against the whole purpose of ListView. You should NOT do this. Just use a LinearLayout instead.
Quote via "How can I put a ListView into a ScrollView without it collapsing?".
Instead using List View in ScrollView you can make separate layout where you can define your desired components. then you can make that layout haeder of the ListView. You can use following code:
LayoutInflater inflater = LayoutInflater.from(this);
View LTop = inflater.inflate(R.layout.header_tolistview, null);
listview.addHeaderView(LTop);

adding elements to scrollview using java when there is more then 1 child

I am trying to add elements to my scrollview,
this scroll view has a vertical linear layout inside of it.
(i added another linear layout - horizontl, inside that will manage 2 textviews and an image according to the attributes i assigned them),
i am trying to add elements of this inner layout to my scroll view (id=insideLineLayout)
(each element added to scrollview will contain these 2 texts and an image)
because of the fact scrollview can only have 1 child i am unable to do this.
would appreciate some help, nothing seems to be working here.
Thanks.
my current xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/searchScreenOuterLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="#+id/catagoryselectedText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:text="selected catagory"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="12dp"
android:orientation="horizontal" >
<EditText
android:id="#+id/searchText"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp" />
<ImageButton
android:id="#+id/searchButton"
android:layout_width="50dp"
android:layout_height="40dp"
android:background="#drawable/search_icon" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="10dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/radiusText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:paddingTop="5dp"
android:text="Search Radius"
android:textAppearance="?android:attr/textAppearanceMedium" />
<SeekBar
android:id="#+id/radiusBar"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp" />
</LinearLayout>
<ScrollView
android:id="#+id/searchScrollView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/searchScrollViewLinear"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingTop="10dp" >
<LinearLayout
android:id="#+id/insideLineLayout"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:orientation="horizontal"
android:weightSum="100"
>
<TextView
android:id="#+id/linesPrice"
android:layout_weight="20"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="tmp1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/linesInfo"
android:layout_weight="60"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:text="tmp2"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="#+id/imageInfo"
android:layout_width="40dp"
android:layout_height="match_parent"
android:background="#drawable/tv_icon" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
Consider how long your list can get as ListView has been optimized in many ways which a regular scroll view is not. If you have too many elements you might start get out of memory exceptions.
In order to dynamically add views to a ScrollView you first need to get the container view inside the ScrollView (most likely a LinearLayout), then you simply do following for each view you add:
View view = inflater.inflate(R.layout.view_to_add, container, false); // this is used if you create from xml, if you make it in code you'd use the regular constructor.
// update the view content (view.findViewById(); etc.)
container.addView(view);

Automatically adjustable layout (having ListView) at runtime

I have an activity with dialog theme. Its layout has 3 parts
HEADER
BODY (HAS ListView)
OK BUTTON
This has to be the structure of my layout always. If I use following layout xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dip" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/iv_av_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="4dip"
android:layout_marginTop="2dip"
android:paddingLeft="6dip"
android:src="#drawable/tablet_ic_logo" />
<RelativeLayout
android:id="#+id/rl_av_found"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/iv_av_logo"
android:layout_marginBottom="5dip"
android:paddingLeft="16dip" >
<TextView
android:id="#+id/tv_av_found"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/orange"
android:textSize="22dp"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_av_recommendation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tv_av_found"
android:layout_marginBottom="8dip"
android:textColor="#color/white"
android:textSize="13dp"
android:visibility="gone" />
</RelativeLayout>
<LinearLayout
android:id="#+id/ll_av_body"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/rl_av_found"
android:background="#drawable/tablet_dialog_glow" >
<ListView
android:id="#+id/lv_av_threats_found"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"
android:divider="#drawable/tablet_dialog_separator" >
</ListView>
</LinearLayout>
<RelativeLayout
android:id="#+id/rl_button_bar"
android:layout_width="wrap_content"
android:layout_height="70dip"
android:layout_below="#id/ll_av_body" >
<Button
android:id="#+id/btn_av_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:onClick="onClick"
android:text="#string/btn_ok" />
<ImageView
android:id="#+id/iv_separator"
android:layout_width="match_parent"
android:layout_height="1dip"
android:layout_alignParentTop="true"
android:background="#drawable/tablet_dialog_footer" />
</RelativeLayout>
</RelativeLayout>
It gives me what I want. But if the list grows and I have say 100 items, OK button goes out of the picture.
And if I use this layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dip" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/iv_av_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="4dip"
android:layout_marginTop="2dip"
android:paddingLeft="6dip"
android:src="#drawable/tablet_ic_logo" />
<RelativeLayout
android:id="#+id/rl_av_found"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/iv_av_logo"
android:layout_marginBottom="5dip"
android:paddingLeft="16dip" >
<TextView
android:id="#+id/tv_av_found"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/orange"
android:textSize="22dp"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_av_recommendation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tv_av_found"
android:layout_marginBottom="8dip"
android:textColor="#color/white"
android:textSize="13dp"
android:visibility="gone" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/rl_button_bar"
android:layout_width="wrap_content"
android:layout_height="70dip"
android:layout_alignParentBottom="true" >
<Button
android:id="#+id/btn_av_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:onClick="onClick"
android:text="#string/btn_ok" />
<ImageView
android:id="#+id/iv_separator"
android:layout_width="match_parent"
android:layout_height="1dip"
android:layout_alignParentTop="true"
android:background="#drawable/tablet_dialog_footer" />
</RelativeLayout>
<LinearLayout
android:id="#+id/ll_av_threats"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#id/rl_button_bar"
android:layout_below="#id/rl_av_found"
android:background="#drawable/tablet_dialog_glow" >
<ListView
android:id="#+id/lv_av_found"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"
android:divider="#drawable/tablet_dialog_separator" >
</ListView>
</LinearLayout>
</RelativeLayout>
The OK button is always displayed at the bottom of the screen and the layout's height takes full screen even if I have just one item in the ListView.
How should I structure my xml so as to have its height wrapped according to the content and OK button always visible independent of the number of items in the ListView?
By Default you should have the params as,
Header - Align Top
Body(ListView) - Below header,
Button - below body(ListView)
Now at runtime,
You should get the height of the list view and if it goes beyond the screen, and if it does, then Set the layout params of button to alignParentBottom and layout params of list view to be below header and above button.
Use may use this in vice versa.
To get the height of the list view. Use this
public int getTotalListViewHeight(ListView lv, BaseAdapter ba) {
int listviewElementsheight = 0;
for (int i = 0; i < ba.getCount(); i++) {
View view = ba.getView(i, null, lv);
view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
listviewElementsheight += view.getMeasuredHeight();
// for Width use view.getMeasuredWidth()
}
return listviewElementsheight;
}
Here the adapter is listviews adapter.
also will checking this, add the buttons height to the total height.

problem to make scroll able,set scroll for whole layout android

had make layout for showing the content in listview and a button below on list view for displaying more information on list view ,so first i am showing only 4 rows in list view and when i next time click on button 4 rows again added in list so now my button goes down in screen so how to make whole layout scroll able so that i can go to button via scrolling my layout is..
listplacehold.xml is
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#1A77BD"
>
<ListView
android:id="#+id/android:list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawSelectorOnTop="false" />
<View
android:id="#+id/horizontolline"
android:layout_width="fill_parent"
android:layout_height="1dip"
android:layout_below="#id/android:list"
android:background="#000000"
/>
<Button
android:text="Load More..."
android:textColor="#color/white"
android:id="#+id/keywordsearchbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/horizontolline"
android:drawableLeft="#drawable/load_more_off"
android:gravity="left|center_vertical"
android:background="#1A77BD"
/>
</RelativeLayout>
and for list item layout is: listitem.xml is
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView android:id="#+id/img"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
<!-- Location where barrio items will be displayed -->
<TextView
android:id="#+id/reportid"
android:layout_marginLeft="70dip"
android:text="reporet id"
android:textColor="#color/white"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
<TextView android:id="#+id/status"
android:textColor="#color/white"
android:text="status"
android:layout_marginTop="40dip"
android:layout_marginLeft="200dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:id="#+id/title"
android:text="title"
android:layout_marginLeft="150dip"
android:textColor="#color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/date"
android:text="dateeeeeee"
android:layout_marginLeft="70dip"
android:layout_marginTop="40dip"
android:textColor="#color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
items are dynamically loadein list view when button click
i think that scroll will be set on above xml means on "listplacehold.xml" but how?,when i am doing it is set to for half screen,means it is not displaying in whole view... Thanks for help in advance.
don't use the ScrollView with the combination of the ListView. Instead make your layout like this.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#1A77BD" >
<Button
android:id="#+id/lodmore"
android:text="Load More..."
android:textColor="#color/white"
android:id="#+id/keywordsearchbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true""
android:drawableLeft="#drawable/load_more_off"
android:gravity="left|center_vertical"
android:background="#1A77BD"/>
<View
android:id="#+id/horizontolline"
android:layout_width="fill_parent"
android:layout_height="1dip"
android:layout_above="#id/loadmore"
android:background="#000000"/>
<ListView
android:id="#+id/android:list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_alignParentTop="true"
android:layout_above="#id/horizontolline/>
</RelativeLayout>
put scrollview over your root layout and remember only one layout node is allowed under scrollview.....
refer this doc:
http://developer.android.com/reference/android/widget/ScrollView.html

Categories

Resources