13

I was recommended using a parent view to get horizontal scrolling right in my TextView:

<HorizontalScrollView android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:scrollHorizontally="true"
        android:gravity="center|right"
        android:text="123456789"/>

</HorizontalScrollView>

Horizontal scrolling works fine but it makes the content overflow to the right when it gets longer than it's parent's width:

-------
|123456|7
-------

However I'm working on a textview that holds numbers and since numbers are commonly aligned to the right I need the textview to overflow to the opposite side. (you should have to scroll left to see the beginning of the string).

 ------
1|4567|
 ------

I have tried multiple combinations of gravity="right" and widths but I cannot manage to do it. How can I align the text to the right and make it overflow to the left?


EDIT:

I tried doing this when the user types:

calc.setText( newNumber );
HorizontalScrollView hsv = (HorizontalScrollView) findViewById(R.id.hsv);
hsv.scrollTo(hsv.getRight(), hsv.getTop());

This scrolls to the right every time the user types a number, however the latest number is always left out of the screen (it's like it scrolled and THEN added the number).

lisovaccaro
  • 32,502
  • 98
  • 258
  • 410

7 Answers7

4

Inspired by this

You can make your own class derived from HorizontalScrollView

public class RightAlignedHorizontalScrollView extends HorizontalScrollView {
    public RightAlignedHorizontalScrollView(Context context) {
        super(context);
    }

    public RightAlignedHorizontalScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public RightAlignedHorizontalScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        scrollTo(getChildAt(0).getMeasuredWidth(), 0);
    }
}

I posted a complete simple project there

Community
  • 1
  • 1
Matthieu
  • 16,103
  • 10
  • 59
  • 86
1
<HorizontalScrollView 
    android:id="@+id/hsv1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:scrollHorizontally="true"
        android:gravity="center|right"
        android:text="123456789"/>

</HorizontalScrollView>    

Added id to the HorizontalScrollView

HorizontalScrollView hsv = (HorizontalScrollView) findViewById(R.id.hsv1);
hsv.scrollTo(hsv.getRight(), hsv.getTop());

This is untested as I made it on the fly. Tell me how it goes.

Adam
  • 2,532
  • 1
  • 24
  • 34
  • I added the ID, pasted the code inside the activity codeblock and I imported HorizontalScrollView but I get various syntax error notes in the second line. Should I have added the code somewhere else? – lisovaccaro Dec 31 '12 at 19:52
  • @Liso22 What syntax errors? The `hsv.scrollTo(int, int)` can be called wherever, but if you want to start hsv all the way to the right, use it in `onCreate(Bundle)` – Adam Dec 31 '12 at 22:04
  • I just checked and found out it only works partially. Here's the problem if I do `calc.setText("12345")` and then immediatelly run your code, it scrolls to the right but it doesn't use the correct width. It doesn't take into account the changes I had done to the textview. So if I type 12345, I actually end up width 1234 on screen. (The 5 is left out) – lisovaccaro Feb 01 '13 at 05:50
1

I think you must use android:fillViewport="true" property for scroll-view
Check This & This

Go through a Tutorial

   <ScrollView 
        android:fillViewport="true"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/scrollView"/>

Doubt: Where is your container layout for scroll-view??

Community
  • 1
  • 1
edwin
  • 7,985
  • 10
  • 51
  • 82
1

I have tested the code . So there are two ways to get around this problem First one with ScrollBars

new Handler().postDelayed(new Runnable(){   
@Override
public void run() {
    HorizontalScrollView hsv = (HorizontalScrollView) findViewById(R.id.hsv1);
        hsv.scrollTo(hsv.getRight(), hsv.getTop());
    }       

},100L);

Second one is without scrollBars

 <TextView 
 android:ellipsize="start"   //add this line
 ...
 />
manpreet singh
  • 1,029
  • 9
  • 15
  • as you pointed out the only way to get around this is to delay the scrolling. Change content > wait 1 ms > scroll to the right. Btw I don't think the option with the TextView would work, it will only add `...` before the text but you won't be able to scroll. Or will you? It seems a more simple solution but I've used ellipsie before and I don't think it only adds the three dots – lisovaccaro Feb 13 '13 at 01:38
  • In case you remove the scrollbars and use ellipsize="start" following shall happen : actual text: "12345667" displayed text: "...45667" So the overflow shall be on left to show only the last part so as to fit the text in display.(Though it won't be scrollable) – manpreet singh Feb 13 '13 at 06:18
0

Did you try assigning the ellipsize parameter to your textview?

android:singleLine="true"
android:ellipsize="start"

I hope this will solve your issue.

For setting the gravity of the text when not overflowing, try setting

android:gravity="center_vertical|right"
Aswathy P Krishnan
  • 11,728
  • 7
  • 27
  • 45
0

try this , this would work properly and will not give you any delays when you jump between threads

hor = (HorizontalScrollView) findViewById(R.id.horizontalScrollView1);
    hor.postDelayed(new Runnable() {
        public void run() {
            hor.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
        }
    }, 1L);
Krishna Avadhanam
  • 98
  • 1
  • 2
  • 10
0

I'm quite late at this, but here's a simple and easy approach by using rotation of the views.

horizontal_layout_id.setRotationY(180f)

The above code inverts the whole horizontal layout horizontally, this make the text start on the right instead of left. HOWEVER, this also inverts the text in the textview inside, which is obviously not good.

textview_inside_horizontal_layout_id.setRotationY(180f)

So, we invert the textview itself once more, this will mirror the mirrored text again! So, you'll end up having something like this-

enter image description here

This will make the text overflow to the right instead of left as well!

Chase
  • 5,315
  • 2
  • 15
  • 41