I have a ListView and an ImageView contained in a LinearLayout. Unfortunately, ListView doesn't scroll the ImageView together with it.
I've tried to use ScrollView to wrap both components, but there is a problem with ListView if I did so.
Anyone know how to make all of the components inside LinearLayout scroll?
Here is my layout xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/imgCanvas"
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="centerCrop" />
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/imgCanvas"
android:smoothScrollbar="true" />
</LinearLayout>
Here is the screenshot of how it looks right now:
You can make your ImageView in another layout file, inflate it and add as HeaderView.
For example:
View header = LayoutInflater.from(this).inflate(R.layout.your_imageview_layout, null);
yourListView.addHeader(header, null, false);
You should try to set your image as header of your list.
Use the following function to do this:
yourListView.addHeaderView(yourImageView, null, false);
Related
I want to create the layout to be like what in the diagram
how can I make the listView Scroll independently and make the textView and ImageView stead?
just replace your xml layout like below code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:padding="5dp"
android:weightSum="2"
android:orientation="vertical" >
<LinearLayout android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="vertical">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Small Text"
android:layout_gravity="right"
android:textColor="#android:color/black"
android:textAppearance="?android:attr/textAppearanceSmall" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:src="#drawable/yourimg" />
</LinearLayout>
above xml given output below screenshot:
For the Lower Steady Image View you can use : Create a footer view
layout consisting of text that you want to set as footer and then try
View footerView = ((LayoutInflater) ActivityContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_layout, null, false);
ListView.addFooterView(footerView);
For Header: The solution that works for me is to create a TableLayout
that has a row with the heading and a row with the list like this:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include layout="#layout/header" />
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</TableLayout>
Please make sure that the ListView has #android:id/list as the ID.
OR
Rather google adding header and footer to listview in android that
will help u much better
Sorry for the pseudo code, I can try a better attempt later on, but you need:
RelativeLayout
TextView - align top
ImageView - align bottom
ScrollView - align above ImageView, align below TextView
Create a linear layout with orientation as vertical. Add textview listview and imageview with the weight as you want. Remember to give layout_height as 0dp for all the three components.
Add Header And Footer To the Listview,Like
ListView mList=getListView();
mList.addHeaderView(View v);//Your textView
and
mList.addFooterView(v);//your imageview
i want to create a horizontal scroll view just like the one shown in image
In android widgets i found Horizontal Scroll View widget in composite section but dont know how to use it.Please help
Create xml file and add HorizontalScrollView first then add one layout to this as child. Then you can add any no. of views to the newly added child, that makes entire things scroll.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<!-- child views here -->
</RelativeLayout>
</HorizontalScrollView>
</LinearLayout>
I create a listview and that listview has a header. I use addHeaderView() method. But when i scroll down in listview the scrollbar vanishes. Is there any way to avoid this i.e i want to show the header all the times, if scrolled still it will show.
To do that you can't do that with the addHeaderView() method.
If you want to place a header on top you should place that in the layout, something like this.
<?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" >
<TextView
android:id="#+id/titleBarText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Title"/>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
The widget is behaving as intended. For what you need, you need to create a relativelayout with TextView at top followed by listview. The textview would act as the header and be static irrespective of your scroll.
everyone.
Can you help me with setting footer to ListView? I've described footer in layouts, and then:
View footer = getLayoutInflater().inflate(R.layout.bottom, getListView(), false);
getListView().addFooterView(footer);
But it didn't appear... Can you give an example? Also I need to set the height of footer dynamically. Here is the code of footer:
<?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:background="#87ceeb"
>
<TextView android:id="#+id/loading"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#000000"
android:textStyle="bold"
android:text="Loading..."/>
</LinearLayout>
There can be few reasons for this,
Have you added your code
getListView().addFooterView(footer); before the setAdapter of ListView ? If yes.
There might be problem in xml layout,
If your listview is wrap_content and your footerlayout has fill_parent , Footer might not appear unless some text is there in your textview of footer layout.
Change the listview layout_width="fill_parent" and footer layout's layout_width="fill_parent"
To quickly check , give a background color to your footer layout and see its placement.
Hope it helps.
The best way to do this is with RelativeLayout.
1) Create a RelativeLayout for the whole layout.
2) Create two views, perhaps ListView and LinearLayout for the footer.
3) Make the listView aligned to the parent top with a bottom margin equal to the height of the footer view or linear layout.
4) make the linear layout aligned the parent bottom
Done. Hope this helps.
EDIT:
Some quick code to try and get the point across:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent" >
<ListView
android:id="#+id/list"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginBottom="50dp"
android:layout_alignParentTop="true" />
<TextView
android:text="Some text"
android:gravity="center"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true" />
</RelativeLayout>
I would like to add an ImageView under my ListView in my ListActivity. So that when the user scrolls to the end of the list, the ImageView shows.
I can't seem to get my layout correct. Either the ImageView is not displayed at all, or if I use layout_weights, then both the ListView and ImageView get 50% of the screen. Any ideas how I can get what I want? Thanks.
<?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" >
<ListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/android:empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/list_no_items" />
<ImageView
android:id="#+id/instruct_image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/instructions"/>
</LinearLayout>
Unfortunately I don't think what you want to accomplish is easily done with standard Android layouts. You can't have a ListView within another ScrollView.
You could use the ListView footer to position the ImageView below the ListView.
Or, you could try to add the ImageView as the last row in the ListView. To do this, override the getView method of the adapter and return the ImageView if it is the last row.