Q:

Implementing on click on a list item and longclick on the element in android

It is as simple as that. I want to implement onclick on list item of my listview and at the same time , long click on the textview of the listview. But it seems like that i can only apply one at a time.
It cant be that difficult.
Any help would be appreciated.

A:

This code below works fine for me.
Basically, you can add setOnLongClickListener to the textview in your getView method.
The code is taken from here. I just added textView.setOnLongClickListener.
When you click the list item, the toast shows up.
When you long-click the textview in the list, the log shows.
public class ListMobileActivity extends ListActivity {
static final String[] MOBILE_OS = new String[] { "Android", "iOS",
        "WindowsMobile", "Blackberry"};
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setListAdapter(new MobileArrayAdapter(this, MOBILE_OS));
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    String selectedValue = (String) getListAdapter().getItem(position);
    Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();
}
}

Here's the custom adapter:
public class MobileArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;

public MobileArrayAdapter(Context context, String[] values) {
    super(context, R.layout.list_mobile, values);
    this.context = context;
    this.values = values;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.list_mobile, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.label);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
    textView.setText(values[position]);
    textView.setOnLongClickListener(new OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            Log.i("MobileArrayAdapter", (String) ((TextView)v).getText());
            return false;
        }

    });
    // Change icon based on name
    String s = values[position];

    System.out.println(s);

    if (s.equals("WindowsMobile")) {
        imageView.setImageResource(R.drawable.windowsmobile_logo);
    } else if (s.equals("iOS")) {
        imageView.setImageResource(R.drawable.ios_logo);
    } else if (s.equals("Blackberry")) {
        imageView.setImageResource(R.drawable.blackberry_logo);
    } else {
        imageView.setImageResource(R.drawable.android_logo);
    }

    return rowView;
}
}

Hope this helps.

