0

I am having a trouble using Scrollview of linearlayout which contains some imageviews as i want to click on each imageview separately but the onclick method just gives me access to the linearlayout.

this is the layout for the scrollview which contains the linearlayout

        <HorizontalScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/horizontalScrollView"
            android:layout_below="@+id/seekBar"
            android:layout_alignParentLeft="true"
            android:layout_above="@+id/nextButton"
            android:layout_alignParentRight="true" >

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

and i add the imageviews programitically by this code

LinearLayout eyesScrollView = (LinearLayout) findViewById(R.id.eyesScrollView);
        for (int k = 0; k < 5; k++) {
            ImageView imageView2 = new ImageView(this);
            imageView2.setId(k);
            imageView2.setPadding(2, 2, 2, 2);
            imageView2.setImageBitmap(BitmapFactory.decodeResource(getResources(), eyesArrayList.get(k)));
            eyesScrollView.addView(imageView2);
        }

and this is the code which access the linearlayout onclick

@Override
    public void onClick(View view) {
        LinearLayout manyPics = (LinearLayout) view;
        manyPics.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view){
                Log.v("theID",String.valueOf(view.getId()));
            }
        });
    }

please can anyone helps me with that problem

1
  • 1
    You'll have to provide a lot more detail about what you are doing. Show us the layout and the relevant code, and describe the actual event handling behavior you are seeing. Commented Sep 8, 2016 at 15:29

2 Answers 2

1

You are assigning your click event to your LinearLayout therefor wherever you click inside that layout area it will trigger the onClick event from the LinearLayout and not the imageView itself. To accomplish what you want, in your for loop and before adding the view, you need to assign the onClick event to the ImageView once you create it.

Code example:

LinearLayout eyesScrollView = (LinearLayout) findViewById(R.id.eyesScrollView);
    for (int k = 0; k < 5; k++) {
        ImageView imageView2 = new ImageView(this);
        imageView2.setId(k);
        imageView2.setPadding(2, 2, 2, 2);
        imageView2.setImageBitmap(BitmapFactory.decodeResource(getResources(), eyesArrayList.get(k)));
        imageView2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view){
            doMyImageViewLogic(view);
        }
    });
        eyesScrollView.addView(imageView2);
    }

Finally do your logic

public void doMyImageViewLogic(View view){
    if(view.getId() == someId)
        doStuff();
    else if(view.getId() == otherId)
        doOtherStuff();
    .
    .
    .
}

And so on

Sign up to request clarification or add additional context in comments.

2 Comments

please can you explain how to use each imageview onclick method
Before "eyesScrollView.addView(imageView2);", you would do something like imageView2.setOnClickListener......". Obviously each image will have its behave, for that matters I would create a method, that depending on the imageView id, would do your logic
0

Although adding horizontal linear layout within horizontalScrollView let's you make a horizontally scroll-able listView, but this is really discourage and is bad in terms of performance.

The correct method for achieving horizontal ListView in to use RecyclerView and set its LinearLayoutManager in Horizontal Direction

Here is an overview for the task... When you use a RecyclerView, you need to specify a LayoutManager that is responsible for laying out each item in the view. The LinearLayoutManager allows you to specify an orientation, just like a normal LinearLayout would.

To create a horizontal list with RecyclerView, you might do something like this:

LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL,false);
RecyclerView myList = (RecyclerView) findViewById(R.id.my_recycler_view);
myList.setLayoutManager(layoutManager);

then for click listener, you will have to set listener on imageView within you adapter for RecyclerView.

Here you may find this link helpful...http://www.androidhive.info/2016/01/android-working-with-recycler-view/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.