0

I have ImageButton in view holder which is in base adapter. I set image for ImageButton in GetView. It is working fine. And I set onClickListener for that ImageButton. So, Now when i click that ImageButton I need to change the background image of that ImageButton.

package com.virtual.applets.moments.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public class MomentsAdapter extends BaseAdapter {
Context mContext;
LayoutInflater mLayoutInflater;
List<Moments> mReturnDataMomentsList;

private int[] mIcons = { R.drawable.like_grey, R.drawable.dislike_grey, R.drawable.abuse_grey };
private int[] mSelectedIcons = { R.drawable.like_selected, R.drawable.dislike_selected, R.drawable.abuse_selected };
ViewHolder holder;

public MomentsAdapter(FragmentActivity activity, List<Moments> momentsListt) {
this.mContext = activity;
this.mLayoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.mReturnDataMomentsList = momentsList;
}

@Override
public int getCount() {
return mReturnDataMomentsList.size();
}

@Override
public Object getItem(int arg0) {
return null;
}

@Override
public long getItemId(int arg0) {
return 0;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

if (convertView == null) {
holder = new ViewHolder();
convertView = mLayoutInflater.inflate(R.layout.moments_custom_screen, null);
holder.mLikeBtn = (ImageButton) convertView.findViewById(R.id.like_btn);
holder.mLikeBtn.setImageResource(mIcons[0]);
holder.mDisLikeBtn.setImageResource(mIcons[1]);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}



holder.mLikeBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
holder.mLikeBtn.setImageResource(mSelectedIcons[0]);
}
});
return convertView;
}

public class ViewHolder {

ImageButton mLikeBtn, mDisLikeBtn, mAbuseBtn, mCommentBtn;
}

}

In fragment i call this adapter like

MomentsAdapter momentsAdapter = new MomentsAdapter((FragmentActivity) getActivity(), mReturnDataMomentsList);
mListview.setAdapter(momentsAdapter);
  • 1
    You can use android selector drawable for imageButton source which has the set of conditions as activated,focused,pressed,default(android:drawble),etc. – Pravin Aug 08 '14 at 07:05
  • what do you want to do when your imageButton clicked, which background do you want to set ? and after another click what do you want to do? – mmlooloo Aug 08 '14 at 11:21

1 Answers1

0

Check out this tutorial, Here. It shows how to add multiple clickable items in a Listview item.

To change the background of the selected Button, you have to save which are the selected button since Listview will destroy its views when it goes out of screen. Follow the code for the custom adapter to get the result you are looking for.

Here the selected button positions are saved in a SpareseBooleanArray and based on its value setting the background changed.

private class CustomAdapter extends ArrayAdapter{

    private SparseBooleanArray selectedItems;

    public CustomAdapter(Context context, int resource) {
        super(context, resource);
        selectedItems = new SparseBooleanArray();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if(convertView == null){
             //Inflate your view here
        }

        ImageButton imageButton = (ImageButton) convertView.findViewById(R.id.imageButton);
        if (selectedItems.get(position)){
            //SET your selected background of imageButton here
        }else{
            //SET your unselected chackground of imageButton here
        }
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!selectedItems.get(position)){
                    selectedItems.put(position,true);
                    //SET your selected background of imageButton here
                }else{
                    selectedItems.delete(position);
                    //SET your unselected background of imageButton here
                }
            }
        });
        return super.getView(position, convertView, parent);
    }
}
SathMK
  • 1,171
  • 1
  • 8
  • 18
  • atleast give the reader a short overview with the link you provided. and some important concepts. – Ker p pag Aug 08 '14 at 07:06
  • I want to know how to set background image of ImageButton onClick of ImageButton. Example in FB android app when we click like button it will change that like button white colour to blue. That kind of function i am expecting here. – Siva Developer Android iOS Aug 08 '14 at 07:17