MVVMCross Databinding Lists with EditText - xamarin.android

I've been playing with the monodroid/monotouch for a while now and having decided to jump from monocross to mvvmcross, I've hit a bit of a problem.
I have a domain model which contains a sub property which is a list of another domain object. Has-Many if you will. What I'm trying to do is display the aggregate root object and allow the user to fill in the details of the sub object.
public class objA
{
public List<objB> mySubObjs
}
I pretty easily got this hooked up using the MvxBindableListView. Except I couldn't bind to objA.mySubObjs, I had to bring the list up a level in the ViewModel and bind to that? Anyway, the issue I faced is that my itemtemplate for the List looked a bit like this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_marginTop="2dip"
android:layout_marginBottom="2dip">
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="a)"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText xmlns:local="http://schemas.android.com/apk/res/x.x.x.x"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
local:MvxBind="{&apos;Text&apos;:{&apos;Path&apos;:&apos;Name&apos;}}" />
</RelativeLayout>
Which renders fine, but is just not useable. When I click on the editText control is brings up the softkeyboard and in doing so loses focus. I've read a couple of articles about EditText in ListView's don't work too well.
Therefore I tried building up the List view manually by binding to specific items i.e. objA.mySubObjs[0].Name, but this doesn't seem to reflect back in the ViewModel.
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Question"
android:textAppearance="?android:attr/textAppearanceSmall"
local:MvxBind="{&apos;Text&apos;:{&apos;Path&apos;:&apos;Items[0].Name&apos;}}" />
I'm at a bit of a loss and didn't really want to go down the route of flattening the List within my ViewModel to accomodate the Android View.
Any help would be appreciated

Does this help - Focusable EditText inside ListView
. Except I couldn't bind to objA.mySubObjs,
on other points, feel free to ask this as a separate question - it should be possible...

Related

Xamarin.Android: When I create a button programmatically, why does it have a margin?

EDIT: I think I figured it out. It was the background in the xml that was causing the difference.
Q:
I'm trying to add buttons to my app dynamically. (I need to create 1-6 buttons)
When I try to create a button in XML (to test the appearance), it looks like this:
Buttons created in XML
Here's the xml I used to create that:
<Button
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#android:color/darker_gray"
android:text="add button"
/>
<Button
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#android:color/darker_gray"
android:text="add button"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
/>
But when I try to create it programmatically, the buttons have a margin on the sides and they look a bit raised up.
How do I get then programmatic buttons to look the same as the XML buttons?
Is there some extra styling happening somewhere?
Dynamically created buttons (below XML buttons)
Here's the code I used to create the buttons dynamically:
var button = new Button(this);
var height = Utility.DpToPx(this, 100);
var width = ViewGroup.LayoutParams.MatchParent;
var layoutParameters = new LinearLayout.LayoutParams(width, height);
button.LayoutParameters = layoutParameters;
button.Text = "Test";
EDIT:
I think I figured it out. It's the background that I put in the xml that is causing the different effect:
android:background="#android:color/darker_gray"
My confusion was caused by the fact that I did not add a background color to the buttons that I created programatically. That is why the appearances were different.

Xamarin.Forms on iOS: Shrink height of page instead of shifting it up when the softkeyboard appears

Got the problem that the softkeyboard overlaps an entry field placed at the bottom.
XAML code:
<ContentPage.Content>
<AbsoluteLayout VerticalOptions="Fill">
<ScrollView AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0,0,1,0.9">
<Label Text="Heading" />
</ScrollView>
<Entry x:Name="SearchEntry" AbsoluteLayout.LayoutBounds=".5,1,1,.1" AbsoluteLayout.LayoutFlags="All" Placeholder="Suchen..." ReturnType="Done" />
</AbsoluteLayout>
</ContentPage.Content>
I installed the KeyboardOverlap! plugin via nuget. It shifts the whole page upwards till the entry is visible again. The entry is visible, but the top of the page isn't visible anymore because it's shifted up.
Another often mentioned solution is wrapping the pages's content in a ScrollView. Because I definitively need an AbsoluteLayout as root content, that's unfortunately not a solution.
I'm looking for a solution which shrinks the height of the page and don't just shifts it up when the softkeyboard appears.
Why I need this? The page is filled dynmically with search result based on the entrie's input. If there are only few result, they're not visible because of the page's upward shift. The user could think that there aren't any search results. Displaying a text like 'No search results.' above the entry would be a simple solution but is not an option here.
Using this CustomRenderer class in the iOS project instead of the KeyboardOverlap plugin did the trick.
Don't forget to customize line 14 with your concrete page classname(s) where you need the functionality. Otherwise the renderer is called for all pages in your app!
Many thanks to Jack Hua - MSFT!

MvxSpinner item should be blank until not select from dropdown list

I'm using MvxSpinner and just want to show drop down selected value over spinner.Currently its showing first item from dropdown list but i want to show blank as i didn't choose any value from dropdown.
Spinner
<MvvmCross.Binding.Droid.Views.MvxSpinner
android:id="#+id/mySpinner"
android:layout_width="match_parent"
android:layout_height="match_parent"
local:MvxDropDownItemTemplate="#layout/myspinner_item"
local:MvxItemTemplate="#layout/myspinner_item"
android:layout_marginLeft="0dp"
android:dropDownWidth="257dp"
android:dropDownSelector="#drawable/list_item_selector"
android:layout_centerInParent="true"
android:spinnerMode="dropdown"
local:MvxBind="ItemsSource MyItems; HandleItemSelected SelectCommand" />
myspinner_item
<?xml version="1.0" encoding="utf-8"?>
<MvvmCross.Binding.Droid.Views.MvxLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/list_item_selector"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="32dp"
android:textSize="15sp"
android:textColor="#de000000"
android:lineSpacingExtra="5sp"
android:singleLine="true"
android:text=""
android:layout_marginTop="6dp"
android:layout_marginBottom="6dp"
android:layout_marginLeft="24dp"
local:MvxBind="Text Description; Typeface StringToFont('Effra_Rg')" />
</MvvmCross.Binding.Droid.Views.MvxLinearLayout>
It's showing the first item because it's bound to the item list. Might be considered bad practice by some, but if your goal is for it to stay blank until chosen, have you tried adding a new item to the front of your list with a blank description?

How to enable Copy Paste for textview in Xamarin Android?

I have RecyclerView, using ViewHolder contains CardView with TextView.
For the textview, I have enabled 'textIsSelectable' property which allows you to select text and copy it.
Below is my textview:
<TextView
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txtNFDescription"
android:textColor="#3f4852"
android:bufferType="spannable"
android:textIsSelectable="true" />
It works for me if I land on this page for the first time.
But if i scroll up this recycler view
OR
Navigate to another page and then land to the same page where i have this implementation. It stops working.
And OUTPUT window gives me this error when I longpress on textview after navigating or scrolling:
08-17 12:05:20.031 W/TextView(10128): TextView does not support text selection. Selection cancelled.
Any Suggestions or solutions why is this happening?

How to highlight a pressed cardView on IceCreamSandwich (Android 4.0.4)

This is my CardView layout :
<it.gmariotti.cardslib.library.view.CardView
xmlns:card="http://schemas.android.com/apk/res-auto"
android:id="#+id/list_cardId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp"
card:card_layout_resourceID="#layout/cardview_history"
style="#style/list_card.base"/>
Based on that layout, my CardViews layout is correctly displayed but don't get highlighted when pressed.
On IceCreamSandwich (API 15 : 4.0.4) and unlike later OS versions, if we click on a CardView, it won't be highlighted. So I tried to explicitly add a background card_selector to my CardView.
android:background="#drawable/card_selector"
I'm using the default existing drawable available in the cardslib resource (card_selector.xml) :
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="#android:integer/config_mediumAnimTime">
<item android:state_activated="true" android:drawable="#drawable/activated_background_card"/>
<item android:state_pressed="true" android:drawable="#drawable/pressed_background_card"/>
<item android:drawable="#drawable/card_background"/>
</selector>
But here is how my CardViews layout become, and as you can see, one of them is pressed but not correctly highlighted.
Is there a solution to make my CardView highlight correctly when pressed?

Resources