Going crazy with this one here,(just to be clear: I looked at all the questions here on stackoverflow regarding "scrollview not working" and none of them helped). The following is a short version of what I'm trying to do
<ScrollView
android:id="#+id/week_View"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Monday" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Monday" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Monday" />.....
.............
</LinearLayout>
</ScrollView>
Textview is repeated multiple more times inside the linearLayout if that is not cleared and I want it to scroll horizontally. What I'm getting is:
You see the textView at the end getting "squeezed" and the rest of the textviews are not even displayed and scrollview doesn't go left or right. What am I doing incorrectly? (The scrollview is wrapped in a RelativeLayout).
ScrollViews are always vertical, meaning they go up and down. Use a HorizontalScrollView instead.
As you have got n number of textViews its better to use
a recyclerview with horizontal layout manager as it wont create memories of all textviews at one go
or,
you can use a horizontalScrollView to scroll them horizontally..
ScrollView scrolls vertically
Related
I am new to android programming. I am implementing a simple calculator and I am facing issues with horizontal scroll View. I am using an edit text within Horizontal scroll view. It works completely fine for the first time but as soon as I clear the screen it retains it scrolling limit, meaning that even though there are few digits on the screen I am able to scroll more than it. What I want to achieve is to limit its scrolling. Below are the screen shots and XML code.
First Time
[Second Time][2]
It is still scroll-able even though there are few digits
<HorizontalScrollView
android:id="#+id/hsvMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:scrollbars="none"
>
<EditText
android:id="#+id/mainEditText"
android:paddingTop="10dp"
android:paddingRight="2dp"
android:background="#ffff"
android:ellipsize="end"
android:focusable="false"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="55sp"
android:textStyle="bold"
/>
</HorizontalScrollView>
Try changing round the layout height/width attributes, make HorizontalScrollView layout_width="fill_parent" and EditText layout_width="wrap_content" and layout_height="wrap_content"
<HorizontalScrollView
android:id="#+id/hsvMain"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:scrollbars="none">
<EditText
android:id="#+id/mainEditText"
android:paddingTop="10dp"
android:paddingRight="2dp"
android:background="#ffff"
android:ellipsize="end"
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="55sp"
android:textStyle="bold"/>
</HorizontalScrollView>
Edit:
The reason why is wasn't working before because you were telling the scrollview to wrap the content regardless the size of the EditText, so it would only scroll around the EditText. But if you tell the ScrollView to match_parent, it'll always be the width of the screen and then the EditText can be scroll within the parent (ScrollView) regardless of the size.
Note: use match_parent instead of fill_parent, fill_parent is now deprecated. (Although still works)
I tried Expand ListView method from using the code from the following blog, https://wirasetiawan29.wordpress.com/2016/01/20/membuat-expand-listview-material-design-di-android/
Everything works fine. But if I add a button in FrameLayout then the touchevent for listview item not works properly. Also I tried changing FrameLayout to Relative & also to Linear, but still no success.
<FrameLayout
android:id="#+id/wrapper"
android:layout_below="#+id/rl_title_wrapper"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatTextView
android:id="#+id/deskripsi"
android:padding="16dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:text="Share"/>
</FrameLayout>
Thanks in advance.
According to Frame Layout description by Google's documentation...
FrameLayout is designed to block out an area on the screen to display
a single item. Generally, FrameLayout should be used to hold a single
child view, because it can be difficult to organize child views in a
way that's scalable to different screen sizes without the children
overlapping each other. You can, however, add multiple children to a
FrameLayout and control their position within the FrameLayout by
assigning gravity to each child, using the android:layout_gravity
attribute.
Hence, your original TextView is actually overlapping by the Button (Share). You can use android:layout_gravity="right" to position the button in the right end of the screen, however, then you will have to fix the maxium length of string for TextView, so that it doesn't get overlap by the Button on the right.
If you don't have any problem, might I suggest you to use LinearLayout? It's easier to handle and render by the GPU (As far as I know). Here's an example code of your item...
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/wrapper"
android:layout_below="#+id/rl_title_wrapper"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="#+id/deskripsi"
android:padding="16dp"
android:layout_weight="1"
android:layout_gravity="left|center"
android:text="This is a big chunk of description for your listView item. you can write as much as you want...."
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Share"
android:layout_gravity="right|center"/>
</LinearLayout>
You can also use RelativeLayout and GridLayout here.
I hope it answers your question. Cheers!
Please refer to example below. I want to have the top layout (below encased in red) to be unmoving in a scrollview in my activity. I have a scrollview as the parent layout and then I thought having a relative layout for the top one would work, and align it to the top, but that didn't really work out as it still remained within the scrollview. I would like to have the users have the red-layout box remain static when they scroll down.
I figure I would also have to put in a topMargin at the top of the scrollview or something in order to fit the redbox layout in.
XML Code posted here: http://pastebin.com/bxdREbeG
Do something like this (hand code, for reference only):
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/YourTopStaticView"
android:layout_width="match_parent"
android:layout_height="48dp"> //Or any other height you want
//Contents of the top view
</RelativeLyout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/YourTopStaticView">
//Contents of the ScrollView
</ScrollView>
</RelativeLayout>
As a side note, do not hardcode children into the ScrollView like that. Use the RecyclerView (which is an updated, modern replacement for ListView), which you will be expected to know how to use if you want to move into serious Android programming. It is actually super easy to use, once you get the hang of it :-)
You should use the ScrollView with only one child (official documentation - http://developer.android.com/reference/android/widget/ScrollView.html). According to your xml, your ScrollView is very complicated with a lot of child widgets.
The best option for you is to use a LinearLayout as the root for the whole container, a LinearLayout( or Relative) for the top layout containing the Reset and Save buttons, and a ListView for the long list that you have. ListView takes care of it's own scrolling. So you don't have to worry about that.
This will improve your code performance as well.
This should suit your needs:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout android:id="#+id/topPanel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<TextView android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="Multi TTS Implementation"/>
<Button android:id="#+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="SAVE"/>
<Button android:id="#+id/resetAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/save"
android:layout_centerVertical="true"
android:text="RESET ALL"/>
</RelativeLayout>
<ScrollView android:id="#id/scroll"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="#id/topPanel"
android:layout_alignParentBottom="true"
android:padding="5dp">
<!-- Your scrollable content here -->
</ScrollView>
</RelativeLayout>
My Current Implementation
I have a HorizontalScrollView which I create in XML that houses a few LinearLayout children. I have added this code below.
There are two LinearLayout containers with the id's group_one and group_two and these are populated programmatically at run time.
I also fix the width of the HorizontalScrollView at run time depending on the amount of View objects I will be inserting.
This solution works great for when the children fit in the HorizontalScrollView without the need to scroll.
The Issue
As soon as I need to scroll (there are more children than can be displayed within the fixed width HorizontalScrollView) then the scrollbar will not go all the way to the right, even though I can see that the child layout is of the correct width, and I can see the scrollbar just will not go any further.
My Question
Why would there be a limit on the scrollbar moving any further right?
My Code
HorizontalScrollView XML
<!-- THIS IS WHERE THE PLUGIN BUTTONS ARE HOUSED -->
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="horizontal"
android:id="#+id/map_plugin_scroll_view"
android:background="#color/map_plugin_background">
<!-- Enclosing box to layout the two groups.-->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="8dp"
android:id="#+id/group_container">
<!-- These layouts contain the map plugins. -->
<LinearLayout
android:id="#+id/group_one"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"/>
<LinearLayout
android:id="#+id/group_two"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"/>
</LinearLayout>
</HorizontalScrollView>
What Is Happening
This is an image of the correct scroll to the left. The edge of the scroll view starts on the right of the red bar. Notice the distance between the two.
This is an image of the incorrect scroll right. Compare the distances between the edges of the scroll view and where the scroll bar is stopping.
This is how I want it to look when I scroll at either end.
I have been playing with this for a while now and finally found the solution.
I was trying to add the left and right margins to the LinearLayout with the ID group_container. However for some reason the HorizontalScrollView was not respecting this and this is why I was seeing this issue.
Instead I added the left and right margins to the group_one and group_two LinearLayouts. Now the HorizontalScrollView respected these and it functions as I expected. Here is my modified code.
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="horizontal"
android:id="#+id/map_plugin_scroll_view"
android:background="#color/map_plugin_background">
<!-- Enclosing box to layout the two groups.-->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:id="#+id/group_container">
<!-- These layouts contain the map plugins. -->
<LinearLayout
android:id="#+id/group_one"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"/>
<LinearLayout
android:id="#+id/group_two"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"/>
</LinearLayout>
</HorizontalScrollView>
set padding right to your scrollview like this :
android:paddingRight="20dp"
Basically I have a horizontal scroll view which, by monitoring the onTouch event I'm paging (ie:each visible part of the scroll view (page) "clicks" into the next when scrolling, rather than just having a standard ScrollView. see paged scrollviews in iOS).
Now I want to find a way to have inner children inherit the same width as the scrollview (which is set at "fill_parent").
Here is my XML to help:
<HorizontalScrollView
android:id="#+id/scrollview"
android:layout_height="0dp"
android:layout_width="fill_parent"
android:layout_weight="1"
android:background="#drawable/scrollviewbg">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="fill_parent">
<ImageView
android:src="#drawable/content1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:src="#drawable/content2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:src="#drawable/content3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</HorizontalScrollView>
As you can see the scrollview is set to width fill_parent and there is a LinearLayout inside it. Now at the moment there are three images which have a set width and height similar to the width of the screen, but what I want to do is change that for 3 LinearLayouts.
Each LinearLayout needs to inherit the same width as the scroll view, so that each one takes up a whole "page" when my code is applied, as it were.
How would I do this? Is it something I will have to do using code? What code will I need?
Thank you.
I know its not direct answer, but its much easier to user ViewPager from Compatibility Package for paging functionality. You can find an example in samples folder (see src/com/example/android/supportv4/app/FragmentPagerSupport.java for source code).
Try this:
android:fillViewport="true"
<HorizontalScrollView
android:fillViewport="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_gravity="right"
android:background="#drawable/black_border"
android:id="#+id/displayText"
android:layout_width="match_parent"
android:scrollHorizontally="true"
android:layout_height="wrap_content"
android:gravity="right"
android:padding="20dp"
android:text=""
android:textSize="20sp" />
</HorizontalScrollView>
inside horizontal scroll view, i have a text view, you can try for image view
Set all the layout_height parameters as fill_parent. You should see the image in full screen then.