2

Sorry for not posting any code. Searching on stackoverflow and google but could not find proper solution for this, so could you please help me out, if you have any idea or tricks for this.

Requirment:

I have many buttons create programmatically according to web's requirement.and set all button's background image with grayscale. I have done this part but when scrolling horizontal scollview then i want to change of that buttons(visible to that time when scrolling) background image colored( initial load grayscaled) and certain time let us assume 10 second buttons background set intial sate(grayscaled).

Main theme:

Change button background image when scolling on visiable view's button. for cetain time interval(10 second).

`

Problem:

I could not get buttons current visible in horizontal view and change of that buttons background colored image(first time view load). All i want to this programmatically, because all images getting from server side.

Any idea , how could do this job or any references for this task.

Note: I create button view and set image background and add images on linearlayout and add that linear layout other mainlinearlayout and add mainlinearlayout on horizontalview layout.

sample snipped of my code:

public class CustomAdWithTitle extends LinearLayout{
private LinearLayout LinearCollectionAds;
private List<CommericalClassifiedAds> listCommericalAds;

        HorizontalScrollView  commercialH = new HorizontalScrollView(mcontext);
        commercialH.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        LinearLayout    commercialL = new LinearLayout(mcontext);
        commercialL.setOrientation(LinearLayout.HORIZONTAL);
        commercialL.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

    LinearLayout    LinearCollectionAds= new LinearLayout(mcontext);
    LinearCollectionAds.setOrientation(LinearLayout.VERTICAL);
    LinearCollectionAds.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));
for (int i = 0; i < listCommericalAds.size(); i++) {
                try {
                    commercialL.addView(listCommericalAds.get(i));
                } catch (Exception e) {
                    // TODO: handle exception
                    e.printStackTrace();
                }

            }

            commercialH.addView(commercialL);
            LinearCollectionAds.addView(commercialH);
   this.addView(LinearCollectionAds);
}

...............

list of CommericalClassifiedAds it could return relativelayout with buttonview. i just add button background only and set grayscaled here first time load view.

public class CommericalClassifiedAds extends RelativeLayout  {
}
Murtaza Khursheed Hussain
  • 15,176
  • 7
  • 58
  • 83
Horrorgoogle
  • 7,858
  • 11
  • 48
  • 81

1 Answers1

2

Let's see if I got you right. For changing the image behind buttons you can use a StateListDrawable

StateListDrawable stateList = new StateListDrawable();
stateList.addState(new int[] { android.R.attr.state_selected }, bitmapFromServer);
stateList.addState(new int[] {}, bitmapGrayScale);

Add it to the each CommericalClassifiedAds background when created instead of the buttons background. Register a Scroll listener to commercialH and update UI in it.

private static boolean wait;
private static Handler handler = new Handler();
private static Runnable delayRunnable = new Runnable() {

            @Override
            public void run() {
                int childcount = commercialL.getChildCount();
                for (int i=0; i < childcount; i++){
                commercialL.getChildAt(i).setSelected(false);
            }
        };

@Override
public void onScrollChanged() {
    //this method can be called very frequently so only run selective
    if (!wait) {
        handler.removeCallbacks(delayRunnable);
        wait = true;
        int childcount = commercialL.getChildCount();
        for (int i=0; i < childcount; i++){
            commercialL.getChildAt(i).setSelected(true);
        }
        handler.postDelayed(new Runnable() {

            @Override
            public void run() {
                wait = false;
            }
        }, 2000);
        handler.postDelayed(delayRunnable, 10000);
    }
}

If it runs slow I would first rethink the layout structure. Looks like you have a couple more layouts than required and then optimize onScroll.

I'm leaving a reservation for errors, it's not tested but it can give you that idea of how to solve your problem.

Community
  • 1
  • 1
Adam Wigren
  • 383
  • 2
  • 11