onCreateDrawableState Never Called - c#

All,
I've got a ViewGroup subclass which overrides OnCreateDrawableState() (Xamarin.Android is in C# so forgive the Pascal Casing).
My override of OnCreateDrawableState() never gets called, however. I've tried calling RefreshDrawableState(), DrawableStateChanged(). RequestLayout(), and Invalidate().
Nothing seems to work. This is the method:
/// <summary>
/// Handles the create drawable state event by adding in additional states as needed.
/// </summary>
/// <param name="extraSpace">Extra space.</param>
protected override int[] OnCreateDrawableState (int extraSpace)
{
int[] drawableState = base.OnCreateDrawableState(extraSpace + 3);
if (Completed)
{
int[] completedState = new int[] { Resource.Attribute.completed };
MergeDrawableStates(drawableState, completedState);
}
if (Required)
{
int[] requiredState = new int[] { Resource.Attribute.required };
MergeDrawableStates(drawableState, requiredState);
}
if (Valid)
{
int[] validState = new int[] { Resource.Attribute.valid };
MergeDrawableStates(drawableState, validState);
}
Android.Util.Log.Debug("ROW_VIEW", "OnCreateDrawableState Called");
return drawableState;
}
I assume it'll work OK - but it just never gets called. The ViewGroup itself is nested in a ListView and/or a LinearLayout but nothing seems to help.
This related question has no answers that work for me.

Edit: (I edited my previous answer incorrect answer where assumed you were implementing the Checkable interface)
OnCreateDrawableState seems to be propagated from the leaf nodes up to it's parent nodes (ViewGroups) only if a child element (leaf) declares android:duplicateParentState for instance in a Textview. As I understand it RefreshDrawableState goes down to the leaves and OnCreateDrawableState up from the leaves if android:duplicateParentState="true" is set, I haven't done a thorough analysis though, would be interested in any good pointers to documentation.
The following layout as an item in a ListView will cause onCreateDrawableState on MyLinearLayout to be called:
<com.example.android.MyLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
...>
<TextView
...
android:duplicateParentState="true"/>
</com.example.android.MyLinearLayout>
This won't:
<com.example.android.MyLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
...>
<TextView
...
android:duplicateParentState="false"/>
</com.example.android.MyLinearLayout>
Neither will this (The LinearLayout won't propagate up):
<com.example.android.MyLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
...>
<LinearLayout
...
android:duplicateParentState="false">
<TextView
...
android:duplicateParentState="true"/>
</LinearLayout>
</com.example.android.MyLinearLayout>
This again will:
<com.example.android.MyLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
...>
<LinearLayout
...
android:duplicateParentState="true">
<TextView
...
android:duplicateParentState="true"/>
</LinearLayout>
</com.example.android.MyLinearLayout>
Note that there seems to be some amalgam with the implementation of the Checkable interface.
The Checkable interface can be implemented by the Views that are immediate children in a ListView, GridView etc. There is no need to have it implemented further down the view tree.
In this case the ListView when in choice mode will call setChecked on the Checkable interface:
http://androidxref.com/4.3_r2.1/xref/frameworks/base/core/java/android/widget/ListView.java#1899
if (mChoiceMode != CHOICE_MODE_NONE && mCheckStates != null) {
if (child instanceof Checkable) {
((Checkable) child).setChecked(mCheckStates.get(position));
}
The implementation of the Checkable usually adds a drawable state by overriding OnCreateDrawableState. See my other post for an example of this:
MvxListView checkable list item

Related

Visibility converter doesn't work even though binding works

I am trying to use the visibility converter from MvvmCross but for some reason the visibility of the view that I'm trying to edit doesn't change at all.
I am using fluent binding in order to bind the element:
var set = this.CreateBindingSet<NextStopFragment, NextStopViewModel>();
set.Bind(Header).For(v => v.Visibility).To(vm => vm.LayoutVisibility_0).WithConversion("Visibility");
set.Apply();
And this is the code from my viewmodel:
private bool _layoutVisibility_0 = false;
public bool LayoutVisibility_0
{
get { return _layoutVisibility_0; }
set { _layoutVisibility_0 = value; RaisePropertyChanged(() => LayoutVisibility_0); }
}
public override void Prepare(Parameter parameter)
{
LayoutVisibility_0 = true;
}
This is the layout of the view that I'm binding (please note that I am restricted to using fluent binding on this):
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/NextStopHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/border_bottom_gray"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:visibility="gone"/>
For the moment I am only trying to make the LinearLayout visible from the start, but it doesn't work. The binding works (I think) because the property gets updated correctly every time I am trying to edit it, but even if the property is set to true, the view remains "gone".
Have I missed something? Do I need to add anything else? Please note that this is ALL the code that I'm using for editing the visibility.

Android Xamarin - How To Add Animations To View's Position Changes?

So I'm a bit of a newb in Android. I've read a few tutorials but found it extensively complicated to understand. I was hoping someone can help me understand on how to implement Animations to views on the current context I'm working on (noob friendly).
Let's say this is my .axml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/top_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="vertical" />
Now this is my activity (Not exactly but enough information to know what I want to do:
private LinearLayout _topContainer;
private IDictionary<string, FrameLayout> _framelayoutViewsDictionary = new Dictionary<string, FrameLayout>();
private LinearLayout.LayoutParams layoutParams;
protected override void OnCreate(Bundle bundle)
{
//SetContentView and other stuff...
layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent, 1);
_topContainer = FindViewById<LinearLayout>(Resource.Id.top_container);
}
public void CreateFirstVideoPlayer
{
_framelayoutViewsDictionary.Add(Constants.Views.TOP_FRAMELAYOUT, new FrameLayout(this)
{
Id = 1,
LayoutParameters = layoutParams
});
var fragmentManager = FragmentManager.BeginTransaction();
fragmentManager.Add(_framelayoutViewsDictionary[Constants.Views.TOP_FRAMELAYOUT].Id, /*Create New MediaPlayer Here*/, Constants.FragmentTag.TOP_FRAGMENT);
fragmentManager.Commit();
_topContainer.AddView(_framelayoutViewsDictionary[Constants.Views.TOP_FRAMELAYOUT]);
}
public void CreateSecondVideoPlayer
{
_framelayoutViewsDictionary.Add(Constants.Views.BOTTOM_FRAMELAYOUT, new FrameLayout(this)
{
Id = 2,
LayoutParameters = layoutParams
});
var fragmentManager = FragmentManager.BeginTransaction();
fragmentManager.Add(_framelayoutViewsDictionary[Constants.Views.BOTTOM_FRAMELAYOUT].Id, /*Create New MediaPlayer Here*/, Constants.FragmentTag.BOTTOM_FRAGMENT);
fragmentManager.Commit();
_topContainer.AddView(_framelayoutViewsDictionary[Constants.Views.BOTTOM_FRAMELAYOUT]);
}
As you can see. I'm programmatically creating a new FrameLayout, adding a fragment on top. And then putting that FrameLayout on top view which is a LinearLayout. Now when I add the second FrameLayout. The first FrameLayout is halved and the second one is added. So now each FrameLayout is taking 50:50 space. So what I want to do is. When the second FrameLayout is added, I want the first FrameLayout to animate so it slowly moves upwards to the top half of the screen instead of instantly appearing on the top. And when I remove the second FrameLayout the first FrameLayout will slowly animate back to the centre of the screen.
Hope this all makes sense. If you need more info please comment!
Using the Support Libraries to perform a fragment slide in/out is as easy as setting the Fragment's custom animations to the built-in resource abc_slide_in_bottom and abc_slide_out_bottom animations.
Example:
SupportFragmentManager
.BeginTransaction()
.SetCustomAnimations(
Resource.Animation.abc_slide_in_bottom,
Resource.Animation.abc_slide_out_bottom,
Resource.Animation.abc_slide_in_bottom,
Resource.Animation.abc_slide_out_bottom)
.AddToBackStack(count.ToString())
.Add(Resource.Id.linearLayout1, new AFragmentSubClass(), count.ToString())
.Commit();

MvvmCross : Empty binding target passed to MvxTargetBindingFactoryRegistry

I created an Android application with Xamarin and MvvmCross.
I want to bind some views (text, editing text, button) to my ViewModel. Nothing strange well so far. But my bindings don’t apply… When I use typed FindViewById, I don’t get the traced error but bindings doesn’t apply.
When I run the application, I have the following trace:
MvxBind:Error: Empty binding target passed to MvxTargetBindingFactoryRegistry
MvxBind:Warning: Failed to create target binding for binding for TextProperty
My override of OnCreate(Bundle bundle) void is :
SetContentView(Resource.Layout.Reference);
var referenceTextView = FindViewById(Resource.Id.referenceEditView); // untyped FindViewById
var siteTextView = FindViewById<TextView>(Resource.Id.siteTextView); // typed FindViewById<T>
//var goButton = FindViewById<Button>(Resource.Id.goButton);
var bindingsSet = this.CreateBindingSet<ReferenceView, ReferenceViewModel>();
bindingsSet.Bind(referenceTextView).To(vm => vm.Reference).Mode(MvxBindingMode.TwoWay);
bindingsSet.Bind(siteTextView).To(vm => vm.Site);
//bindingsSet.Bind(goButton).To(vm => vm.GoCommand);
bindingsSet.Apply();
base.OnCreate(bundle);
I’ve tried to do in the AXML :
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/siteTextView"
android:text="####"
local:MvxBind="Text Site"
android:gravity="center" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/referenceTextView"
android:hint="Numéro de dossier"
local:MvxBind="Text Reference" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Accéder"
android:id="#+id/goButton"
local:MvxBind="Click GoCommand" />
The getters and setters of my properties use RaiseAndSetIfChanged method :
private string _reference;
public string Reference
{
get { return _reference; }
set { this.RaiseAndSetIfChanged(ref _reference, value, () => Reference); }
}
I’ve the same LinkerPleaseInclude class than LinkerPleaseInclude original class.
My setup inherits from MvxAndroidSetup class
And on other ViewModels, the bindings are applied correctly.
You need to call base.OnCreate(bundle); before SetContentView as the ViewModel is located and attached inside of that call. Failing to do so will obviously give you the exact errors you see. Source will be null and will not bind to the target.
So you can either do:
base.OnCreate(bundle);
SetContentView(Resource.Layout.Reference);
And have all your bindings in your AXML. Or you can do the other approach setting the bindings behind the scenes:
base.OnCreate(bundle);
SetContentView(Resource.Layout.Reference);
var referenceTextView = FindViewById<TextView>(Resource.Id.referenceEditView);
var siteTextView = FindViewById<TextView>(Resource.Id.siteTextView);
var bset = this.CreateBindingSet<ReferenceView, ReferenceViewModel>();
bset.Bind(referenceTextView).To(vm => vm.Reference);
bset.Bind(siteTextView).To(vm => vm.Site);
bset.Apply();
Just make sure to call base.OnCreate to begin with.
The warnings
MvxBind:Error: 2,20 Empty binding target passed to MvxTargetBindingFactoryRegistry
MvxBind:Warning: 2,20 Failed to create target binding for binding for Text
are caused by var referenceTextView = FindViewById(Resource.Id.referenceEditView); resulting in referenceTextView to be of type View.
MvvmCross is searching for the default binding target property of the type TTarget when calling Bind<TTArget> without For(targetProperty). This is just a look up in a table like:
TTarget Property
----------------------
TextView Text
Button Click
... ...
In your case TTarget is View instead of TextView, because you pass it into bindingsSet.Bind(referenceTextView) wich is the implicit call of bindings.Bind<View>(btnNumber). View has no default binding target property. You have to set it explicitly like
bindings.Bind(btnNumber).For("Text")
or use the typed FindViewById<TextView>.
I dont think you need to bind twice, remove these lines:
var referenceTextView = FindViewById(Resource.Id.referenceEditView); // untyped FindViewById
var siteTextView = FindViewById<TextView>(Resource.Id.siteTextView); // typed FindViewById<T>
//var goButton = FindViewById<Button>(Resource.Id.goButton);
var bindingsSet = this.CreateBindingSet<ReferenceView, ReferenceViewModel>();
bindingsSet.Bind(referenceTextView).To(vm => vm.Reference).Mode(MvxBindingMode.TwoWay);
bindingsSet.Bind(siteTextView).To(vm => vm.Site);
//bindingsSet.Bind(goButton).To(vm => vm.GoCommand);
bindingsSet.Apply();
So your on create is just this:
SetContentView(Resource.Layout.Reference);
base.OnCreate(bundle);
And keep the bindings in the axml file.
Make sure you have this at the top of your xaml file:
xmlns:local="http://schemas.android.com/apk/res-auto"
Also if you are doing bindings in the cs file, the MvvmCross binding mode is TwoWay by default. So you dont need .Mode(MvxBindingMode.TwoWay);

Replacing a fragment - New instance of fragment class isn't instantiated

I'm trying to replace a fragment with a another fragment with the following lines of code:
FragmentTransaction ft = Activity.FragmentManager.BeginTransaction();
UploadCompleted ucompleted = new UploadCompleted();
ft.Replace(Resource.Id.CameraFragmentContainer, ucompleted, "uploadcompleted");
ft.AddToBackStack(null);
ft.Commit();
The fragment is being replaced inside another fragment which uses the Activity's fragment manager. The issue is that the OnCreate and OnCreateView methods of the replaced fragment aren't invoked and an old instance of fragment (the fragment which was instantiated the first time I replaced it) get's instantiated which is why I'm not able to pass any new values to the replaced fragment. Any idea what I'm doing wrong? I'm new to android development so my knowledge of fragments isn't that great.
As per your logic, OnCreate and OnCreateView of the new Fragment should get invoked. It is a good practice to do FragmentTransaction at Activity level rather than, at Fragment level. In the above snippet you are creating an instance of the new Fragment from within the already displayed Fragment and replacing itself from the parent Activity. This is not a recommended approach. I would suggest the following change.
public class ParentActivity:Activity
{
public void ChangeToUploadCompleted()
{
FragmentTransaction ft = FragmentManager.BeginTransaction();
UploadCompleted ucompleted = new UploadCompleted();
ft.Replace(Resource.Id.CameraFragmentContainer, ucompleted, "uploadcompleted");
ft.AddToBackStack(null);
ft.Commit();
}
}
public FirstFragment:Fragment
{
void CaptureCompleted ()
{
((ParentActivity)Activity).ChangeToUploadCompleted();
}
}
This is the good practice to switch fragments. This might fix your problem too
ft.Replace(Resource.Id.CameraFragmentContainer, ucompleted, "uploadcompleted");
In this R.id is the layout that you place in Fragment Activity or Activity and UploadCompleted is Fragment class which you want to replace so use xml like this in FRagmentActivity or ACtivity class
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/lin"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"/>
</LinearLayout>
when you want to replace use this
FragmentTransaction ft = Activity.FragmentManager.BeginTransaction();
UploadCompleted ucompleted = new UploadCompleted();
ft.Replace(Resource.Id.lin, ucompleted, "uploadcompleted");
ft.AddToBackStack(null);
ft.Commit();

Load CheckedTextView items in ListView with checked states set

When my activity starts and I initially load my ListView with items, I want to be able to set some of those items to checked however that doesn't seem to be working.
I'm using the simple_list_item_multiple_choice layout so the items are CheckedTextView.
Here's the adapter. Text displays properly however the Checked property doesn't seem to be doing anything visually despite it being set to true/false.
public class VehicleSubListAdapter : BaseAdapter<string>
{
// ...
public override View GetView(int position, View convertView, ViewGroup parent)
{
View view = convertView ?? _context.LayoutInflater.Inflate(Android.Resource.Layout.SimpleListItemMultipleChoice, parent, false);
var checkedTextView = view.FindViewById<CheckedTextView>(Android.Resource.Id.Text1);
checkedTextView.Text = _items[position].Name;
// This doesn't seem to do anything
checkedTextView.Checked = _items[position].Checked;
// I've even tried this with no change visually
//checkedTextView.Checked = true;
return view;
}
}
Here's the activity which can be closed and re-opened but needs to resume from where it left off (some items checked, some items not). That is why things are stored in a global static variable.
If the global list is empty, manually update the lists. Else, load from the global list. It successfully loads from the global list and successfully stores the Checked property of that list as modified in the item click event. However, the checked items aren't shown as checked when the activity loads.
Also I've tried putting the call to FillList() in both OnCreate and OnResume and neither have worked.
public class VehicleConditionSubActivity : ListActivity
{
private List<VehicleConditionItemDTO> _items;
private VehicleSubListAdapter _vehicleAdapter;
//...
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetTheme(Resource.Style.MyAppTheme);
SetContentView(Resource.Layout.VehicleConditionItem);
// Set up action bar
ActionBar.SetDisplayHomeAsUpEnabled(true);
// Set up list view
ListView.ChoiceMode = ChoiceMode.Multiple;
_items = new List<VehicleConditionItemDTO>();
_vehicleAdapter = new VehicleSubListAdapter(this, _items);
ListAdapter = _vehicleAdapter;
ListView.ItemClick += ListView_ItemClick;
//...
// Fill list view
FillList();
}
private void FillList()
{
AddFluidItems();
_vehicleAdapter.NotifyDataSetChanged();
}
private void AddFluidItems()
{
// I've checked that this is being called correctly
if (Global.Conditions.FluidLevels.Count > 0)
{
foreach (var item in Global.Conditions.FluidLevels)
{
_items.Add(item);
}
}
else
{
_items.Add(new VehicleConditionItemDTO("1", "Engine oil"));
// ...
foreach (var item in _items)
{
Global.Conditions.FluidLevels.Add(item);
}
}
}
// ...
private void ListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
// Items already check/uncheck properly when tapped
// So I just update the global list items checked property
Global.Conditions.FluidLevels[e.Position].Checked = !Global.Conditions.FluidLevels[e.Position].Checked;
}
}
The layout for the activity is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px"
android:background="#color/background">
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#android:id/list" />
</LinearLayout>
Selecting Items Programmatically
Manually setting which items are ‘selected’ is done with the
SetItemChecked method (it can be called multiple times for multiple
selection)
Customizing A List View's Appearance
Note that SetItemChecked is on the ListView.
You can remove the code in GetView that sets Checked.
Then, after (or during the call to FillList) for each item that should be initially checked, call:
ListView.SetItemChecked (position, true);

Categories