I am new to Android programming, now I am trying to place images inside the horizontal scrollview.
Actually what I am trying to do is, I want to display array of images which needs to be scrolling automatically from right to left.
So far I have tried as below
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/image2"
android:layout_width="150dp"
android:layout_height="150dp"
android:src="@drawable/icon"
android:layout_centerHorizontal="true"
android:layout_marginRight="5px"/>
</LinearLayout>
</HorizontalScroll>
Activity file-
package com.myhorizontalScroll;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ImageView;
public class MyHorizontalScrollActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// declare a class field:
final Handler h = new Handler();
// later:
final ImageView scroll=(ImageView)findViewById(R.id.image2);
Thread t = new Thread(){
public void run(){
int y = scroll.getScrollY();
int x = scroll.getScrollX();
while(y<1600){
// need final values to create anonymous inner class
final int X = x;
final int Y = y;
h.post(new Runnable() {
public void run() {
scroll.scrollTo(X, Y);
}
});
x++;
try {
sleep(1000/12);
} catch (InterruptedException e) {
}
}
}
};
t.start();
}
}
Now the imageview goes from left to left; the imageview actually positioned in the left side.
How to place the imageview in right side and I want this scrolling needs to done continuously.
How can I proceed with that?
This is how I do it in one of my apps. This is essentially used to display the Profile Pictures of User’s who like a post. You will naturally have to figure out a bit for yourself, but I will try and help where I can.
First, the XML:
In my Java code, I declare and cast them globally, so you will have to do a little figuring out.
The JAVA Code:
A little explanation:
arrLikesis anArrayListthat holds the Profile Picture’s. I usearrLikes.size()in thefor loopto get the correct number.ImageViewsand then setting theMarginbetween them.ImageViews.Hope this helps. And feel free to ask if you have any difficulties with any of the above.