Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android, make scrollable view overflow to the left instead of right?

Tags:

java

android

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).

like image 585
lisovaccaro Avatar asked Dec 31 '12 19:12

lisovaccaro


2 Answers

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

like image 194
Matthieu Avatar answered Sep 18 '22 18:09

Matthieu


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
 ...
 />
like image 43
manpreet singh Avatar answered Sep 17 '22 18:09

manpreet singh