Search code examples
androidmvvmcross

Droid ValueConverter for LinearLayout Gravity


I'm developing an application that contains a ListView. The items on the ListView should be either on the left or on the right of the screen depending on the item's content. So i created a converter to check the item and set a LinearLayout gravity. The converter is being called and it's returning right or left "correctly", but the LinearLayout is not being set. The Linear Layout is the following:

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"
    android:background="#FFFFFF"
    local:MvxBind="Gravity Content, Converter=MessageAlign">

The converter I tried it in 2 different ways, one using GravityFlags and the other strings:

public class MessageAlignConverter : MvxValueConverter<string, GravityFlags>
{
    protected override GravityFlags Convert(string value, Type targetType, object parameter, CultureInfo culture)
    {
        if (My Test)
        {
            return GravityFlags.Right;
        }
        return GravityFlags.Left;
    }
}

public class MessageAlignConverter : MvxValueConverter<string, string>
{
    protected override string Convert(string value, Type targetType, object parameter, CultureInfo culture)
    {
        if (My Test)
        {
            return "right";
        }
        return "left";
    }
}

Neither one of them worked, so i checked the LinkerPleaseInclude. I tried adding in the LinkerPleaseInclude but i cannot get the current gravity, for a button (as example) i would do:

public void Include(Button button)
    {
        button.Click += (s,e) => button.Text = button.Text + "";
    }

So i imagined something like this for the LinearLayout:

public void Include(LinearLayout linearLayout)
    {
        linearLayout.SetGravity(linearLayout.Gravity?);
    }

Any ideas on how to do this?

Forgot to mention the output:

MvxBind:Warning:  6.00 Failed to create target binding for binding Gravity for Content
[0:] MvxBind:Warning:  6.00 Failed to create target binding for binding Gravity for Content
10-26 15:43:51.796 I/mono-stdout(11936): MvxBind:Warning:  6.00 Failed to create target binding for binding Gravity for Content

Thanks!


Solution

  • First, I think you should use the first version of the value converter (the one returning GravityFlags)

    Second, in the application output, do you see any debug text saying that it couldn't bind to Content? If you don't, then there's no reason to think about LinkerPleaseInclude.

    Third, you didn't show how you define the ListView in AXML, so make sure you don't have any issues with the UI. If you replace the Mvx.Bind binding with harcoding the android:gravity to a value, is the item displayed correctly?

    EDIT: Use define your own binding to set Gravity: In MvvmCross how do I do custom bind properties