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

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

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


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

Tags:

Java

Android