Remove scroll in Listview - android

There are only two items in my listview and it covers half of screen but listview still shows scroll around it. I have set height to wrap_content
layout code is below:
<?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" >
<ListView
android:id="#+id/listLikeWhatsapp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:divider="#android:color/transparent"
android:dividerHeight="10.0sp"
/>
</LinearLayout>
I don't want scroll to appear on listview because it will have only two items and there is no need to show. I am not sure what is causing scroll to appear.
Any help would be highly appreciated.
Thanking you in advance
UPDATE
Here is my row item xml :
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:background="#drawable/roundedshape"
android:alpha="0.7">
<ImageView
android:id="#+id/icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:contentDescription="desc"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:src="#drawable/ic_insert_emoticon_black_48dp"
/>
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/icon"
android:paddingBottom="5dp"
android:text="Assignments"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="#000"/>
<TextView
android:id="#+id/last_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/title"
android:layout_below="#+id/title"
android:text="Last Update : 01-01-2017"
android:textSize="13sp"
android:textColor="#696969"/>
<TextView
android:id="#+id/unread_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/title"
android:layout_alignBottom="#+id/title"
android:layout_alignParentRight="true"
android:text="04"
android:textSize="12sp"
android:background="#drawable/shape_circular"
android:padding="5dp"
android:textColor="#FFF"
android:textStyle="bold"
android:layout_marginRight="5dp"
/>
</RelativeLayout>

<ListView
...
android:scrollbars="none"
...
/>

try adding:
android:scrollbars="none"

user android:scrollbar="none" like this;
<ListView
android:id="#+id/list_view_meals_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none" />

To hide scrollbars
do this
android:scrollbars="none"
To disable scrolling do this
listView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return (event.getAction() == MotionEvent.ACTION_MOVE);
}
});
PS: I think you have messed up your item view which includes icon title and description.

Use this 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" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="your text view"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:id="#+id/text_ap"/>
<ListView
android:id="#+id/listLikeWhatsapp"
android:layout_width="match_parent"
android:layout_above="#id/text_ap"
android:layout_height="match_parent"/>
</RelativeLayout>

try this :
to hide the scrollbar
<ListView
android:id="#+id/listLikeWhatsapp"
android:layout_width="match_parent"
android:scrollbars="none" // add this
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:divider="#android:color/transparent"
android:dividerHeight="10.0sp"
/>
or in your code:
yourlistView.setVerticalScrollBarEnabled(false);
To disable Scrolling of listview Items try:
listView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
return true; // Action will not be forwarded
}
return false;
}
});
OR
yourListView.setScrollContainer(false);

I know it's an old question i was searching for solution and i remembered that i use it in another project and i forgot the owner of the solution so here is the code :
public static void justifyListViewHeightBasedOnChildren (ListView listView) {
ListAdapter adapter = listView.getAdapter();
if (adapter == null) {
return;
}
ViewGroup vg = listView;
int totalHeight = 0;
for (int i = 0; i < adapter.getCount(); i++) {
View listItem = adapter.getView(i, null, vg);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams par = listView.getLayoutParams();
par.height = totalHeight + (listView.getDividerHeight() * (adapter.getCount() - 1));
listView.setLayoutParams(par);
listView.requestLayout();
}

Related

HorizontalListView - how do I get the image view source

I hope someone can give me a hint how to resolve this issue:
My layout contains vertical scrolling view and HorizontalListView.
I can scroll up and down from the vertical list and scroll horizontal to view the image. When I click on the image from the horizontal list, I want to display full image. The problem is when the image is clicked on the horizontalListView, I don't know how to get the image source value. e.g.
/storage/emulated/0/ItineraryPlanner/imag-20180825-0.jpg
I found the example from the following to handle the horizontal list view
https://github.com/MeetMe/Android-HorizontalListView/blob/master/AndroidHorizontalListView/src/com/meetme/android/horizontallistview/HorizontalListView.java
I need to get the image value item from the onSingleTapConfirmed method. Can anyone advise, please. Below is my layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:widget="http://schemas.android.com/apk/res-auto"
android:id="#+id/itineary_row_layout"
android:scrollbarStyle="insideInset"
android:fadeScrollbars="true"
android:scrollbarSize="10dip"
android:fitsSystemWindows="true"
android:weightSum="1.0"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/product_list_item_bg"
android:padding="#dimen/space1"
android:textSize="18dip"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/row_1">
<TextView
android:id="#+id/icon_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.05"
android:drawableLeft="#drawable/ic_delete_grey"
android:drawablePadding="#dimen/space1"
android:paddingLeft="2dp"
android:paddingTop="7dp"
android:textColor="#color/colorPrimaryTrans"
fontPath="fonts/reg.otf"
tools:ignore="MissingPrefix"
android:layout_gravity="right" />
</LinearLayout>
<View
android:id="#+id/divider_line__header"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#+id/row_1"
android:layout_marginTop="10dp"
android:background="#color/view_divider_color" />
<LinearLayout
android:id="#+id/layout_horizontal"
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_marginLeft="5dp"
android:orientation="horizontal"
android:visibility="visible"
android:layout_below="#+id/row_6" >
<au.com.klau.itineraryplanner.HorizontalListView
android:id="#+id/list_images"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:gravity="center" >
</au.com.klau.itineraryplanner.HorizontalListView>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
unpressTouchedChild();
OnItemClickListener onItemClickListener = getOnItemClickListener();
final int index = getChildIndex((int) e.getX(), (int) e.getY());
if (index >= 0 && !mBlockTouchAction) {
View child = getChildAt(index);
int adapterIndex = mLeftViewAdapterIndex + index;
Object obj = HorizontalListView.this.mAdapter.getItemId(adapterIndex);
Object obj1 = HorizontalListView.this.mAdapter.getItemViewType(adapterIndex);
System.out.println("HorizontalListView.onSingleTapConfirmed - index >= 0 : getItemId obj[" + obj.toString() + "] | getItemViewType=>obj1.toString()[" + obj1.toString() + "]");
if (onItemClickListener != null) {
onItemClickListener.onItemClick(HorizontalListView.this, child, adapterIndex, mAdapter.getItemId(adapterIndex));
return true;
}
}
if (mOnClickListener != null && !mBlockTouchAction)
{
mOnClickListener.onClick(HorizontalListView.this);
}
return false;
}

Nested Listview only shows first item

I got a fragment in my tablayout displaying a listview:
<android.support.design.widget.CoordinatorLayout 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:background="#color/backgroundColor"
android:fitsSystemWindows="true"
tools:context=".activities.fragments.OrdersFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="#+id/orderListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#color/colorPrimaryDark"
android:dividerHeight="1px" />
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
This Listview has items which have the following layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/order_list_order"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<ImageView
android:id="#+id/arrowView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_weight="1"
app:srcCompat="#drawable/ic_downarrow" />
<LinearLayout
android:id="#+id/previewLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/order_list_storename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="Store Name"
android:textColor="#color/generalText"
android:textSize="24sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
some textview here
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
some textview here
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/extensionLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/previewLayout"
android:visibility="visible">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Produkte:"
android:textColor="#color/generalText"
android:textSize="18sp"/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/exampleArray"/>
</LinearLayout>
</RelativeLayout>
These items all have also a listview which has the exampleArray as items:
<string-array name="exampleArray">
<item>Donut</item>
<item>Pizza</item>
<item>blabla</item>
</string-array>
My Problem is, that only the first element of this inner listview is shown. Also the outer Listview is not clickable anymore. Does anyone know why this is the case? Am I doing anything wrong?
Its not a good idea to put a scrollable view inside another scrollable view. Try to solve your problem using getItemViewType method.
If you don't have any other option, then you have to measure inner ListView height. This way all item will be inflated at the same time, you can't take advantage of recycle property of ListView.
To measure inner ListView use this method
/**** 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();
}
Usage:
setListViewHeightBasedOnChildren(listView)
This way you inner LisView will lose recycle property.
There is other questions about this, You can check those too,
Check this and this
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">//changed here
<ListView
android:id="#+id/orderListView"
android:layout_width="match_parent"
android:layout_height="match_parent"//and here
android:divider="#color/colorPrimaryDark"
android:dividerHeight="1px" />
</RelativeLayout>
Please use 'match_parent' with 'ListView' else use RecyclerView

View width for dynamically added view

I want to show a notification counter like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center"
android:padding="#dimen/_5sdp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:alpha=".5"
android:id="#+id/gvIcon"
android:src="#drawable/ic_person_reading"
android:scaleType="centerCrop"
android:layout_width="#dimen/_70sdp"
android:layout_height="#dimen/_70sdp" />
<LinearLayout
android:id="#+id/llTexts"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
style="#style/gridItemsText"
android:id="#+id/gvText"
android:text="Guten Morgen"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/partNoticeTooltip"
android:orientation="vertical"
android:background="#drawable/bg_tooltip_red"
android:layout_width="#dimen/tooltipWH"
android:layout_height="#dimen/tooltipWH">
<TextView
android:id="#+id/tvCounter"
android:text="4"
android:textColor="#color/white"
android:textSize="#dimen/h6"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Which has an output like this:
This is my expectation, but with dynamic way. So I wrote this in the setOnItemClickListener of my GridView:
View tooltip = context.getLayoutInflater().inflate(R.layout.part_tooltip, null);
TextView tvCounter = (TextView) tooltip.findViewById(R.id.tvCounter);
tvCounter.setText("" + counter);
LinearLayout llText = (LinearLayout) view.findViewById(R.id.llTexts);
llText.addView(tooltip);
the part_tooltip has exactly the same code but with another layout. And here is the output:
The red layout does not being displayed with full width. What am I missing?
You need to use a global layout listener and also the view tree observer:
ViewTreeObserver mVTO = parentLayoutOfTheViewAddedDynamically.getViewTreeObserver();
mVTO.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if(viewAddedDynamically != null){
int viewWidth = viewAddedDynamically.getWidth();
// any operation based on viewWidth is to be done here
}
}
});

Fixed height and internal scrolling for expanablelistview

I have an expandable listview, which is populated programatically. i want to have fixed length for that expandable listview and have scrolling inside each group not for the whole list.
my layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/ll_price_list_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/theme_blue"
android:orientation="horizontal"
android:weightSum="8" >
<TextView
android:id="#+id/tv_pricelisthead_item"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:gravity="left"
android:text="PRICING"
android:textColor="#color/white"
android:textSize="18sp" />
<TextView
android:id="#+id/tv_pricelisthead_np"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="2"
android:gravity="center"
android:text="NORMAL"
android:textColor="#color/white"
android:textSize="18sp" />
<TextView
android:id="#+id/tv_pricelisthead_ep"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="2"
android:gravity="center"
android:text="EXPRESS"
android:textColor="#color/white"
android:textSize="18sp" />
</LinearLayout>
<!--
android:background="#drawable/hf_bg"
<TextView
android:id="#+id/tv_price_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="16dp"
android:text="Price List"/>
-->
<ExpandableListView
android:id="#+id/elv_pricelist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:transcriptMode="alwaysScroll" >
</ExpandableListView>
</LinearLayout>
with the above layout i could get a list view.
i want this listview to occupy fixed length. headers should always be shown and scrolling should be there for internal child items not for the whole list.
i have only 3 groups and each group may have around 10-30 child items.
I found this code on SO a while back, i tried searching for this, but couldn't find it. If someone does, please edit my answer and provide link to the original answer. Since ExpandableListView extends ListView you can use this code, but make sure in your adapter that the children are expanded already.
private void justifyListViewHeightBasedOnChildren(ListView listView) {
ListAdapter adapter = listView.getAdapter();
if (adapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0; i < adapter.getCount(); i++) {
View listItem = adapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams par = listView.getLayoutParams();
par.height = totalHeight + (listView.getDividerHeight() * (adapter.getCount() - 1));
listView.setLayoutParams(par);
listView.requestLayout();
}

Listview android - change height after data load dynamically

I have a android layout like this:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/headingView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:paddingTop="#dimen/value_5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_below="#+id/relativeLayout1"
android:layout_centerHorizontal="true"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:paddingLeft="10dp"
android:paddingTop="5dp"
android:visibility="gone" />
<LinearLayout
android:id="#+id/list_items"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#color/mt_gray_color"
android:fadeScrollbars="false" >
<TextView
android:id="#+id/itemHeading"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textStyle="bold"
android:background="#f2f2f2"
android:layout_marginTop="15dp"
android:visibility="gone"/>
<ListView
android:id="#+id/mylist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="#dimen/value_5dp"
android:minHeight="500dp" />
</LinearLayout>
</LinearLayout>
</ScrollView>
I am loading data dynamically in ListView after this page/fragment has been loaded.
The listview contains more than one element and I was expecting that it will show me list in the listview without scrolls as I have made it WRAP_CONTENT.
I might have wrong understanding and which what I want to understand and find a solution that how can i avoid the scroll bars and have all the items listed in the listview without scrolls.
Thanks in advance.
You could calculate the size off all children of the ListView at runtime and then set it's height to the total height of all the children combined. You can use this method:
public void justifyListView(ListView listView) {
ListAdapter adapter = listView.getAdapter();
if (adapter == null) {
return;
}
ViewGroup vg = listView;
int totalHeight = 0;
for (int i = 0; i < adapter.getCount(); i++) {
View listItem = adapter.getView(i, null, vg);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams par = listView.getLayoutParams();
par.height = totalHeight + (listView.getDividerHeight() * (adapter.getCount() - 1));
listView.setLayoutParams(par);
listView.requestLayout();
}
You can change the height of listView when the data changed.

Categories

Resources