0

i am designing an app for android tablets. I have created a layout file which contains the application bar, next is some buttons. In the middle of the screen i want to have a horizontal scroll view inside which i need to show some images.

The number of images inside the scroll view depends upon the return data from the url. For this i am maintaining an array list. According to the size of the array i need to create the image views withing the scroll view.

I have placed all other buttons, textviews in the layout file and i need to make the above said view alone through coding, how to do this.

If the array size is 19, then the list of images within scroll view to be shown in the following order only

1  4  7  10  13  16  19
2  5  8  11  14  17    
3  6  9  12  15  18      

In iPad iBook apps library page, the books will be listed out in this way.

how to do this....

Siva K
  • 4,968
  • 14
  • 82
  • 161

2 Answers2

1

not sure but can try approach like this

1- Create/inflate a horizontal scrollview ..
2- make for loop running i= 0 to x 
        where x= (totalCount/3)+(totalCount%3>0?1:0)
3- Create a  Linear layout with orientation vertical  
4- create one more loop form j=0 to 3 or (i+1)*3+j< totalCount
5- add your element layout in  Linear layout 
6 after the inner loop closed add Linear layout in horizontal scroll-view
  • loop termination condition like the value of x may not be exact please check them

For making item clickable

1- take any view from  element layout like in you case image-view is good option  
 2- creates a class in you activity or better to extend you activity with clickListner.
 3- while creating the imageView for each element set this listener to all
 4- Set the data object or index  with element with image-view in tad using SetTag
 5- in Onclick function you will get image-view as argument and use getTag to get that data of attached with clicked element   
Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36
  • i have implemented as you have said. I have got the same view multiple times as i need. But when is click on those i am not getting the toast msg. I have added the complete solution below – Siva K Jun 15 '12 at 10:56
  • The toast msg appears only for the last view with the correct tag number and not for the others... – Siva K Jun 15 '12 at 11:03
0

Thanks to Dheeresh Singh

Following is my main.xml file

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/linear_1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal" >
        </LinearLayout>
</HorizontalScrollView>

i have my Horizontal scroll view in my main.xml file

public class LayoutActivity extends Activity 
//implements OnClickListener
{
    ViewGroup layout;
    LinearLayout lr;

    int x;
    int total = 4;
    int count = 0;
    LinearLayout lay;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        count = 0;
        layout = (ViewGroup)findViewById(R.id.linear_1);
        x = (total/3)+(total%3 > 0?1:0);;
        for(int i = 0; i < x; i++)
        {
            lr = new LinearLayout(this);
            lr.setOrientation(LinearLayout.VERTICAL);
            layout.addView(lr);
            for(int j = 0; j < 3; j++ )
            {
                count++;
                final View child = getLayoutInflater().inflate(R.layout.inflateview, null);
                lay = (LinearLayout)child.findViewById(R.id.threeByThree_tableRow1_1_Layout1);
                lay.setOnClickListener(new View.OnClickListener() 
                {   
                    @Override
                    public void onClick(View v) 
                    {
                        Toast.makeText(getApplicationContext(), "selected  id  is "+child.getId(), Toast.LENGTH_SHORT).show();
                    }
                });
                lr.addView(child);
                child.setId(count);
                if(i == total)
                {
                    break;
                }
            }
        }      
    }
}

in the above code i have lr is the LinearLayout to display it in vertical order and View child is the data which i need to show in both vertical and horizontal order.

Thanks a lot Dheeresh Singh

Community
  • 1
  • 1
Siva K
  • 4,968
  • 14
  • 82
  • 161
  • 1
    a small mistake .... child.setOnClickListener(new View.OnClickListener() should be inside the inner loop :) – Dheeresh Singh Jun 15 '12 at 11:07
  • I have one query where you initalized layout variable of "layout.addView(lr);" – Dheeresh Singh Jun 15 '12 at 11:10
  • no use.. now where ever i click the toast msg seems to be the fianl number – Siva K Jun 15 '12 at 11:13
  • no need Xml jasu asking where is findViewById for layout variable of "layout.addView(lr);" – Dheeresh Singh Jun 15 '12 at 11:22
  • 1
    can you implement the listener as I said in my answer implement View.OnClickListener in your activty and pass this in child.setOnClickListener() and setTag and getTag from image view – Dheeresh Singh Jun 15 '12 at 11:24
  • there is error in the following line if(obj instanceof Integer) { position = (Integer(obj )).intValue(); } saying that Create method 'Integer(Object)' – Siva K Jun 15 '12 at 12:05
  • i have added the method private Object Integer(Object obj) { return null; } and now it shows error in ".intValue();" saying Add cast to Integer(obj) – Siva K Jun 15 '12 at 12:06
  • 1
    it's Integer class there no need to make ............. where it requires ? must typo error – Dheeresh Singh Jun 15 '12 at 12:08
  • Hai i copy this code in my proj but final View child = getLayoutInflater().inflate(R.layout.inflateview, null); lay = (LinearLayout)child.findViewById(R.id.threeByThree_tableRow1_1_Layout1); this line is underlined by redline, so please send the xml inflateview layout and contents – Kevin Rameshwaran Dec 31 '12 at 09:52