-2

I have a custom list view,List view item has a button, I can't click on the list item itself, the only thing is clickable is the button.

I want to be able to click the list item and the button separately.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View itemView = convertView;
    itemView = (itemView == null) ? inflater.inflate(R.layout.listview_layout, null) : itemView;
    TextView textViewName = itemView.findViewById(R.id.headtext);
    TextView textViewDescription = itemView.findViewById(R.id.disctext);
    ImageView stationImage = itemView.findViewById(R.id.image);
    //ImageButton favButton = itemView.findViewById(R.id.favbutton);
    FloatingActionButton favButton = itemView.findViewById(R.id.favbutton);
    Station selectedStation = stations.get(position);
    textViewName.setText(selectedStation.getStationName());
    textViewDescription.setText(selectedStation.getDescription());
    stationImage.setImageResource(selectedStation.getStationImg());
    checkFavForBtn(favButton, selectedStation);

    favButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                LibraryUtil.setFavoritesLibrary(selectedStation, context);
                System.out.println("Saving");
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
            checkFavForBtn(favButton, selectedStation);
            //notifyDataSetChanged();
        }
    });
    return itemView;
}

click listener for the listview -

       lst.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //my code
        }
    });

What should I do? Thank you in advance!

Found the solution

        itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent serviceIntent = new Intent(finalItemView.getContext(), BackgroundSoundService.class);
            serviceIntent.putExtra("position", position);
            serviceIntent.putExtra("whatlibrary", 1);
            finalItemView.getContext().startService(serviceIntent);

        }
    });

    favButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                LibraryUtil.setFavoritesLibrary(selectedStation, context);
                System.out.println("Saving");
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
            checkFavForBtn(favButton, selectedStation);
            //notifyDataSetChanged();
        }
    });
Tsur Yohananov
  • 533
  • 1
  • 7
  • 17

3 Answers3

1
You are handling favbutton on click listener .thats why only button click is working.

try to handle whole view onclickListiner

like below

 //  onClick listener for view
    itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // do something when the corky is clicked
        }
    });

second way :

implement like this public class BookListActivity extends Activity implements OnItemClickListener

and over ride the method like below

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {`\

//do your on click functionalities here }

Hope it is helpful for you.Let me know

prakash421
  • 139
  • 1
  • 8
0

Add this in your button

   android:focusable="false"
   android:focusableInTouchMode="false"

Since the button is clickable, the whole view will not be focused. Adding this line will help you to click both item view as well as button in view

Jyoti JK
  • 2,141
  • 1
  • 17
  • 40
0

A.F.A.I.K.

When you are using Button or ImageView in list_item,

you will not able to use OnItemClickListener properly.

because They have their own focus, so it will not let list_item focus.

You have to set OnClickListener separately on button and list_item's parent layout.

Rumit Patel
  • 8,830
  • 18
  • 51
  • 70