0

I am trying to create a cell which has 2 buttons side by side.

I have done this in MT.D : How can I get an element with 2 buttons side by side?

I now need to do this for the droid version.

Looking at the source for ButtonElement in the MvvmCross code I can see that Element has consideration for 1 click event only.

What is the best way of going about this for MD.D? It looks completely different (obviously) to the touch counterpart.

I can create a layout that has the 2 buttons in but I have no way of hooking the buttons up to click events.

Here is the layout code (dialog_double_button.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:id="@+id/LinearLayout02" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:layout_alignParentBottom="true">
    <Button 
        android:id="@+id/Button02" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" android:text="button 1">
    </Button>
    <Button 
        android:id="@+id/Button03" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_weight="1" 
        android:text="button 2">
    </Button>
</LinearLayout>

And my element looks like :

public class DoubleButton : Element, View.IOnClickListener
{
    public DoubleButton (): base("", "dialog_double_button")
    {

    }

    protected override View GetViewImpl (Context context, ViewGroup parent)
    {
        var view = DroidResources.LoadButtonLayout(context, parent, LayoutName);

        if (view != null)
        {
            var buttons = view.FindViewById<Button> (Resource.Layout.dialog_double_button);
        }


        return view;
    }

    public void OnClick(View v)
    {
        if (Click != null)
            Click(this, EventArgs.Empty);

        if (SelectedCommand != null)
        {
            // TODO should we have a SelectedCommandParameter here?
            if (SelectedCommand.CanExecute(null))
            {
                SelectedCommand.Execute(null);
            }
        }
    }
}

When I run this code with:

[Activity(Label = "Login")]
public class Login : MvxDialogActivity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        Root = new RootElement{ 
            new Section{
                new DoubleButton()
            }
        };
    }
}

I can see the layout is added to the UI but inside my elements c# code calling var buttons = view.FindViewById<Button> (Resource.Layout.dialog_double_button); yields nothing - so I can't hook the click events up.

Any ideas?

Community
  • 1
  • 1

1 Answers1

0

view.FindViewById will only work if view its the parent of that buttons. In your case it is not, it's the parent of "LinearLayout02", so you should get it first and use FindViewById on it.

(OP amending this answer as it was nearly what I ended up with)

I actually fixed it using:

var button1 = view.FindViewById<Button>(context.Resources.GetIdentifier("Button02", "id", context.PackageName));
var button2 = view.FindViewById<Button>(context.Resources.GetIdentifier("Button03", "id", context.PackageName));
Ilya Gazman
  • 31,250
  • 24
  • 137
  • 216