Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make HorizontalScrollView RIGHT to LEFT Scroll android

By default HorizontalScrollView scrolls from Left to Right but I want to scroll from Right to Left.

How to do this? Any help will be appreciated.

like image 526
Muhammad Zahid Avatar asked Mar 02 '15 07:03

Muhammad Zahid


People also ask

How can I make my layout scroll vertically in Android?

Vertical scroll view going to scroll its child view in Vertical direction so we have created Linear layout as a child for Vertical scroll view and added child for linear layout.

Can scroll vertically Android?

In Android, a ScrollView is a view group that is used to make vertically scrollable views. A scroll view contains a single direct child only. In order to place multiple views in the scroll view, one needs to make a view group(like LinearLayout) as a direct child and then we can define many views inside it.


2 Answers

add layout_gravity RIGHT its helpful when items dose not wrap hole with

 <HorizontalScrollView
            android:id="@+id/scrollPartition"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="none">

            <LinearLayout
                android:id="@+id/lytPartition"
                android:layout_gravity="right"
                android:layout_width="wrap_content"
                android:layout_height="48dp"
                android:gravity="center_vertical"
                android:orientation="horizontal"
                android:paddingLeft="4dp"
                android:paddingRight="4dp"
                android:scrollbars="none">
            </LinearLayout>
        </HorizontalScrollView>

and in code need to scroll to the end of list

 findViewById(R.id.scrollPartition).post(new Runnable() {
    @Override
    public void run() {
      ((HorizontalScrollView) findViewById(R.id.scrollPartition)).fullScroll(View.FOCUS_RIGHT);
    }
  });

to set layout_gravity programmatically use this :

    HorizontalScrollView scrollView = new HorizontalScrollView(context);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    layoutParams.gravity = Gravity.RIGHT;
    scrollView.setLayoutParams(layoutParams);
like image 151
Criss Avatar answered Oct 21 '22 02:10

Criss


You can scroll it to the right edge of your scroll view in your code with something like this:

scrollView.postDelayed(new Runnable() {
    public void run() {
        scrollView.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
    }
}, 100L);

Related Q and A is https://stackoverflow.com/a/4720563/2511775

like image 30
Ashkan Sarlak Avatar answered Oct 21 '22 03:10

Ashkan Sarlak