andineupert
andineupert

Reputation: 351

How can i use long-clicks in MonoDroid?

in the moment i'm using long-clicks this way:

button.SetOnLongClickListener(new MyLongClickListener());

public class MyLongClickListener : View.IOnLongClickListener
{
    public bool OnLongClick(View v)
    {
        //do something pretty cool
        return true;
    }

    public IntPtr Handle
    {
        get { throw new NotImplementedException(); }
    }
}

But writing a class just to do an easy one or two-liner in the OnLongClick-method seems not to be very smart. So i'm wondering if there is a better solution?

Upvotes: 1

Views: 3472

Answers (2)

Greg Shackles
Greg Shackles

Reputation: 10139

The approach of writing a listener class is the way to do it in Java, which is why it's exposed as such in Mono for Android. That said, in Mono for Android you can assign a delegate of type LongClickHandler to the LongClick property if you'd prefer that. if you'd prefer that.

view.LongClick = onLongClick;

private bool onLongClick(View view) 
{
    // do some stuff

    return true;
}

or

view.LongClick = (clickedView) =>
{
    // do some stuff

    return true;
};

Upvotes: 4

A.Quiroga
A.Quiroga

Reputation: 5722

See this example code:

[Activity(Label = "My Activity", MainLauncher = true)]
public class Activity1 : Activity
{
protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);            
    SetContentView(Resource.layout.main);

    Button button = FindViewById<Button>(Resource.id.button);
    TextView view = FindViewById<TextView>(Resource.id.text);

    button.Click += (s, args) => view.Text = "Clicked!";
    button.LongClick += (s, args) =>
                            {
                                view.Text = "Long click!";
                                args.ReturnValue = false;
                            };
}
}

Upvotes: 1

Related Questions