Scroll text in a horizontal scroll view - android

I have seen a few posts similar to my issue. However, the solutions mentioned in those posts did not help me fix the problem. Here is the scenario. I have a TextView in a horizontal scroll view. The idea is as soon as a text (larger than the view) is appended to the TextView, it must be automatically scrolled to end. Here is the layout snippet.
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#color/subTitleColor"
android:fillViewport="true"
android:scrollbars="none" >
<TextView
android:id="#+id/drillPath"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:text="this is a such a long text that scrolling is needed, scroll more"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/textColorWhite" />
</HorizontalScrollView>
Upon an event, I do the following.
textView.append("do auto scroll");
Effectively, it should scroll the text to the end. But it isn't! What is it I am lacking?

First of all, your scrollview will never scroll since its content view (the TextView) is in fill_parent mode. it should wrap its content in order to leave space for the text and be bigger than its parent. (change your textView width to : android:layout_width="wrap_content" )
Then, there is nothing here to request the scroll, I don't think it should happen automatically.
Add something like this to request the scroll to the end of your scroll view :
textView.append("do auto scroll");
scrollView.post(new Runnable() {
#Override
public void run() {
scrollView.fullScroll(View.FOCUS_RIGHT);
}
});
Let me know when you've tried it.

Let try to fix your TextView height and change this width layoutParam to wrap_content
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
android:layout_width="match_parent" <-- Here
android:layout_height="10dp" <-- Here
android:layout_margin="5dp"
android:background="#color/subTitleColor"
android:fillViewport="true"
android:scrollbars="none" >
<TextView
android:id="#+id/drillPath"
android:layout_width="wrap_content" <-- Here
android:layout_height="10dp" <-- Here
android:gravity="bottom"
android:text="this is a such a long text that scrolling is needed, scroll more"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/textColorWhite" />
</HorizontalScrollView>
Because of this layoutParam, Your text do not have enough size to scroll. It only have max width is screen width.
Hope that help.

Related

How to center a layout inside a scrollView on click?

So I have this layout here:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollView"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/instructions">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Sample text 1"/>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Sample text 2"/>
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Sample text 3"/>
</LinearLayout>
</ScrollView>
(I just made it up to textView3 for simplicity's sake, but say there's more and the scrollView is scrollable) then there is a button outside this scrollView. Every time the button is clicked the scrollView scrolls and centers the view to the next textView. How can I achieve this?
I've tried getting the height of the views then adding it up to use on the scrollView.scrollTo(y), but retrieving the height gave me lots of problems, I used the ViewTreeObserver process but I couldn't retrieve the height inside the
.addOnGlobalLayoutListener(New ViewTreeObserver...
and place it on a variable. but I failed with this method. What am I doing wrong? or are there any easier approach?
You might consider using RecyclerView and add new items to the list and then call the scrollToPosition(position) function to scroll to the latest one.
Don't forget to add paddingBottom to the RecyclerView to be able to scroll the last view into to center. (Use the screenHeight / 2 for the paddingBottom)

Horizontal Scroll View, Scrolling Issue Android

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)

HorizontalScrollView don't scroll left

I have a horizontalscrollview which inside it has a LinearLayout with TextView.
I am setting dynamically String that are longer every time to the TextView (like a calculator), But when the TextView gets wider, I can't scroll to the begining of it(to the left side in my handy), but only to the right side, which is just some empty space which I have no idea how it came.
In general, just can't scroll to one side(left) to see some of the TextView in the layout.
This is the xml of the scrollview:
<HorizontalScrollView
android:id="#+id/Hscrollbar"
android:layout_width="match_parent"
android:layout_height="35dp"
android:foregroundGravity="right"
android:fillViewport="true"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_gravity="center">
<TextView
android:id="#+id/logs"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:scrollHorizontally="true"
android:text="v"
android:textColor="#ffffff"
android:textSize="30dp" />
</LinearLayout>
</HorizontalScrollView>
Also in my activity, this is what I modified to the HorizontalScrollView:
final HorizontalScrollView Hscrollbar = (HorizontalScrollView)findViewById(R.id.Hscrollbar);
Hscrollbar.post(new Runnable() {
#Override
public void run() {
Hscrollbar.fullScroll(View.FOCUS_RIGHT);
}
});
How can I make it scroll to the left side (which I want to see the whole TextView)?
Remove android:foregroundGravity="right" from HorizontalScrollView.
Make LinearLayout width wrap_content and same for TextView.
Set TextView android:layout_gravity="right"
That might work.

Smooth scrolling Textview

I have a TextView, defined like this:
<TextView
android:id="#+id/play_info"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#color/play_info_background"
android:ellipsize="none"
android:maxLines="8"
android:orientation="vertical"
android:scrollbars="none"
android:singleLine="false"
android:textColor="#color/play_info_text_color"
android:textSize="#dimen/play_info_font_size"
android:overScrollMode="never" >
</TextView>
I set the TextView to be scrollable in code, like this:
cardInfo = (TextView) play.findViewById(R.id.play_info);
cardInfo.setMovementMethod(new ScrollingMovementMethod());
The TextView scrolls, but not smoothly, like anything inside a ScrollView.
I would like for it to scroll smoothly.
I have already tried putting the TextView (with scrolling disabled) inside a ScrollView, but this messes with the height of the TableRow it's contained in, and there doesn't seem to be a way to correct that.
The problem is if you want "smooth scroll" you have to place the TextView in a ScrollView. And when you do that, you can no longer
set the height of the TextView to "match_parent". Like, for example, when you need a large TextBox that is filled dynamically with text,
but not resized. And when you try by setting the ScrollView's attribute to "match_parent", the child TextView still wraps around the content.
I was able to do it like this:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:drawable/edit_text">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#null"
android:gravity="bottom"
android:singleLine="false"
android:typeface="serif"/>
</ScrollView>
</RelativeLayout>
A RelativeLayout container that serves as a new TextView, with the desired background, the original TextView with background set to null. So when the TextView
expands, with new text appended, it's just like text volume increasing. The text is aligned with the layout attributes if the ScrollView.
You may also need to disable scrolling for the TextView, to avoid collision with the ScrollView's scrolling in certain scenarios.
You probably missing focus attribute, Textview is not in focus so its not scrolling.
<TextView
android:id="#+id/title_itemcount"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:focusable="true"
android:focusableInTouchMode="true"
android:maxLines="3"
android:padding="4dp"
android:scrollbars="vertical"
android:textColor="#android:color/background_dark" />
Hope it helps... :)

horizontalscrollview set child elements to fill horizontalscrollview width

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.

Categories

Resources