Reputation: 1519
I need to start and end the content inside the Horizontal scrollview, like we had in Gallery (deprecated) widget. Contents inside the Horizontal scrollview should start from the center of the screen, and while scrolling, it should move to left from the center, the same as Gallery.
I have few hundred images, populated dynamically in the linearlayout inside the Horizontal scrollview. I acheived to auto scroll the horizontal scrollview with the time delay of one second. But I need to start the content from center and end in center.
public class ImageGalleryScrollActivity
extends AppCompatActivity {
HorizontalScrollView horizontalScrollView;
int maxCount=59;
Handler mHandler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
horizontalScrollView.smoothScrollBy(maxCount, 0);
mHandler.sendMessageDelayed(new Message(), 1000);
return false;
}
});
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.horizontal_scroll_activity);
horizontalScrollView=(HorizontalScrollView)findViewById(R.id.horizontalView);
mHandler.sendMessageDelayed(new Message(), 1000);
}
}
layout file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<HorizontalScrollView
android:id="@+id/horizontalView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:fillViewport="true">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
Upvotes: 1
Views: 1428
Reputation: 54204
You could combine left/right padding each equal to half the screen width with the android:clipToPadding="false"
attribute on your scrollview.
Since you won't know how much padding you'll need until runtime, you'll have to set the padding in Java. Put this in your onCreate()
:
scrollView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int padding = getResources().getDisplayMetrics().widthPixels / 2;
scrollView.setPadding(padding, 0, padding, 0);
scrollView.setClipToPadding(false);
}
});
Upvotes: 4