I have a main activity and its layout is a scrollView and has a "linear layout" called [taskLayout].
setContentView(R.layout.my_jobs_view_single_holder);
final LinearLayout taskLayout = (LinearLayout)findViewById(R.id.TaskLayout);
Then i'm inflating another view and adding the view to the main scrollviews.TaskLayout section.
LayoutInflater pickup_inflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = pickup_inflater.inflate(R.layout.my_jobs_view_single_pickup,null);
taskLayout.addView(view);
the newly added view has a listView which used an
ADAPTER_PICKUP_PASSENGER extends BaseAdapter
where it loads data Asynchronously from an external database.
The main scrollview layout looks like this:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fullscreen_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp"
android:background="#0099cc">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/TaskLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:orientation="vertical">
</LinearLayout>
the Layout which contains listView looks like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fullscreen_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="top"
android:background="#color/bg"
android:orientation="vertical">
<ListView
android:padding="2dp"
android:background="#drawable/border"
android:id="#+id/lst_passenger_pickup"
android:layout_width="fill_parent"
android:minHeight="200dp"
android:layout_height="fill_parent">
</ListView>
Everything works fine except the listView's height is not WRAP_CONTENT it only shows one item. When i change the ListView's
android:layout_height="fill_parent"
to
android:layout_height="200dp"
then the ListView gets 200dp height. For some reasons android:minHeight does not have any effect.
Ideally i would want to set the hight as WRAP_CONTENT so all items could be viewed but it doesn't work but either WRAP_CONTENT or FILL_PARENT just shows one item and setting manual height as 200dp is sometime too big if the list has only one item.
I have tried following steps so far:
In the Adapter just before returning convertView, i tried to do parent.requestLayout();
After setting the adapter to the listview, listview.requestLayout()
Nothing happens. could anyone shed some light please?
It will expand your listview as per your row height and width dynamically.
public static void getTotalHeightofListView(ListView listView) {
ListAdapter mAdapter = listView.getAdapter();
int totalHeight = 0;
for (int i = 0; i < mAdapter.getCount(); i++) {
View mView = mAdapter.getView(i, null, listView);
mView.measure(
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
totalHeight += mView.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (mAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
Related
I have to generate a dynamic listview in android. The listview is put inside a scrollview because I have other content too to display above the listview. Something like this :
Down part is the listview which is dynamically added. This all comes fine on a portrait mode on Samsung note5 but when I change the screen orientation to landscape the listview does not scroll. This could be because I have given wrap-content for the listview.
Please check my layout code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/rootLayout">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:background="#color/gray25"
android:id="#+id/scrollView"
android:layout_above="#id/footer">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/mainLayout"
android:background="#color/light_blue"
android:paddingBottom="#dimen/_10sdp">
<!--Top header-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_vertical"
android:id="#+id/topheader"
android:background="#drawable/dashboard_button"
android:layout_marginLeft="#dimen/_5sdp"
android:layout_marginRight="#dimen/_5sdp"
android:layout_marginTop="#dimen/_5sdp"
android:padding="#dimen/_5sdp">
</LinearLayout>
<!--middle portion-->
<ListView
android:id="#+id/RFIDList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:transcriptMode="alwaysScroll"
android:layout_marginTop="#dimen/_10sdp"
android:background="#drawable/dashboard_button"
android:layout_marginLeft="#dimen/_5sdp"
android:layout_marginRight="#dimen/_5sdp"
android:minHeight="#dimen/_50sdp" />
</LinearLayout>
</ScrollView>
<!-- Footer aligned to bottom -->
<RelativeLayout
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center"
android:background="#color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="left"
android:layout_centerInParent="true"
android:layout_marginLeft="#dimen/_15sdp">
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
I cannot give fixed height to the listview because its items are generated dynamically. I tried to find the solution and also tried changing my listview to recyclerview but did not work. but Any help would be appreciated.
If you use RecyclerView use NestedScrollView instead of ScrollView and set
recyclerView.setNestedScrollingEnabled(false); after setting layout manager
for ListView see my answer over here
Use below method after listView.setAdapter(yourAdapter);
as
setlistViewHeight(listView,context);
Your problem will be solved.
public static void setlistViewHeight(ListView listView, Context context) {
ListAdapter myListAdapter = listView.getAdapter();
if (myListAdapter == null) {
return;
}
int totalHeight = 0;
for (int size = 0; size < myListAdapter.getCount(); size++) {
View listItem = myListAdapter.getView(size, null, listView);
if (listItem instanceof ViewGroup)
listItem.setLayoutParams(new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT));
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
#SuppressWarnings("deprecation")
int screenWidth = display.getWidth();
int listViewWidth = screenWidth - 65;
int widthSpec = View.MeasureSpec.makeMeasureSpec(listViewWidth,
View.MeasureSpec.AT_MOST);
listItem.measure(widthSpec, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (myListAdapter.getCount()));
listView.setLayoutParams(params);
listView.requestLayout();
}
Hi i have try insert listview in scrollview but i have this problem:
the space that scrollview reserve to listview is little, i want that scrollview is than the screen , i want scrollview is visible only when listview becomes larger than the screen How i do?
this is code:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:layout_height="wrap_content"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:id="#+id/activity_lista__eventi"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="14dp"
android:paddingRight="14dp"
android:paddingTop="#dimen/activity_vertical_margin"
xmlns:android="http://schemas.android.com/apk/res/android"
tools:context="com.example.fra87.eudroid.activity_class.Lista_Eventi">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="vertical">
<SearchView
android:layout_width="match_parent"
android:layout_height="50dp"
android:queryHint="Evento"
android:id="#+id/cercaEvento"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:id="#+id/linearLayoutEventi">
<ListView
android:id="#+id/listViewEventi"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/cercaEvento"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp" />
</LinearLayout>
</LinearLayout>
</ScrollView>
How i do this?
More solutions can be found here: Android list view inside a scroll view
This is a common issue that a lot of developer face. The issue is you are stacking a scrollable view inside another scrollable view. One solution to remove the listview from the scrollview.
Another is the following code:
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
I want a Fragment that has 2 ListView. I dont want the listView to scroll instead i just want the whole Fragment to scroll. Heres the code:
<?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"
android:background="#fff" >
<TextView
android:id="#+id/day"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="center_horizontal"
android:paddingTop="15dp"
android:text="Monday"
android:textColor="#000"
android:textSize="55sp" />
<TextView
android:id="#+id/date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/day"
android:gravity="center_horizontal"
android:text="27 April, 2014"
android:textColor="#000"
android:textSize="45sp" />
<ListView
android:id="#+id/home_list_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/date"
android:layout_margin="10dp"
android:background="#color/list_home_time_bg"
android:divider="#android:color/white"
android:dividerHeight="20.0sp" />
<ListView
android:id="#+id/home_list_stime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/home_list_time"
android:layout_margin="10dp"
android:background="#color/list_home_stime_bg"
android:divider="#android:color/white"
android:dividerHeight="20.0sp" />
</RelativeLayout>
When i run this code the 2nd listView gets its own scroll bar, is there a way to stop this and make the entire Fragment to scroll?
To stop the listview scrolling use -
listView.setScrollContainer(false);
To scroll fragment put it inside ScrollView -
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- everything you already have -->
</ScrollView>
For more check - how-to-get-a-non-scrollable-listview.
NOTE:
If you put ListView inside a ScrollView then all the ListView does not stretch to its full height. Below is a method to fix this issue.
/**** Method for Setting the Height of the ListView dynamically.
**** Hack to fix the issue of not showing all the items of the ListView
**** when placed inside a ScrollView ****/
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
return;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED);
int totalHeight = 0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i++) {
view = listAdapter.getView(i, view, listView);
if (i == 0)
view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, LayoutParams.WRAP_CONTENT));
view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
To use this method just pass the ListView inside this method :
ListView list1 = (ListView) view.findViewById(R.id.home_list_time);
setListViewHeightBasedOnChildren(list1);
ListView list2 = (ListView) view.findViewById(R.id.home_list_stime);
setListViewHeightBasedOnChildren(list2);
More here - android-list-view-inside-a-scroll-view.
I'm trying to make an activity with a static header and a scrollable dynamic list. Here is the Layout:
<GridLayout
android:id="#+id/GridLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="1" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingTop="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<ListView
android:id="#+id/listex"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
I tried to use the height=0dp & weight=1 solution but the result is that when I add something to the list, it does not show any element.
If I use height= match_parent & weight=1 the elements are shown but is not scrollable
Please help, I tried a lot of combinations gettign no results.
Thanks.
I have been using this list view and it works fine.
Maybe you don't have enough items in you list view, for it to scroll?
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="290dp"
android:layout_weight="0.50" >
</ListView>
You can try using below codes :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="static header" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
This is my solution.
<ListView
android:id="#+id/listView"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="fill_horizontal"
android:fadeScrollbars="true"
android:layout_weight="1" />
set ListView weight to 1. or You can do like this.
just after u set ur adapter call this method and set adapternotifydatasetchange() after dat.
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
return;
int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
int totalHeight = 0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i++) {
view = listAdapter.getView(i, view, listView);
if (i == 0)
view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, ViewGroup.LayoutParams.WRAP_CONTENT));
view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
your list in the GridLayout remove the parent of list view that is GridLayout it will scroll perfectly.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:paddingTop="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<ListView
android:id="#+id/listex"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
</LinearLayout>
The vertically scrolling GridLayout should not contain another vertically scrolling widget (ListView).You shouldn't put a ListView inside a ScrollView because the ListView class implements its own scrolling and it just doesn't receive gestures because they all are handled by the parent that is its ScrollView
Set to the linearLayout weightSum = 1
i have some dynamic list which grow on some inputs i also have tab and some layout above it .and i want to scroll them as well as list grow.
in simple words just not load full list but scroll other things as i scroll list.
i m doing something like this .
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TabHost
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:dividerPadding="5dp"
android:orientation="horizontal" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
<FrameLayout
android:id="#+android:id/realtabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</TabHost>
</ScrollView>
note
i also want to scroll tabs .
i m currently using this code
public static class Utility {
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}
}
for make a listview but it when i load the screen it focuse the screen from the list not from the tabs. any one knew how to start focus it from the top
please help
Your ListView will always scroll independently of the other list. If you want to have everything scroll together then you need to look at imitating a ListView using a LinearLayout and adding each row to the layout. This will then force the layouts to adhere to the outer scroll view container.