Android.Content.Res.Resources+NotFoundException - xamarin.android

I have an aplication written in Mono Android that has a form and displays data in a list. The application works fine.
But now I am trying to add another field to the layout:
<TextView
android:id="#+id/textDeliveryNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="12345"
android:textStyle="bold"
android:textColor="#color/title"
android:textSize="20px"
/>
I modified the activity acordingly to set this field with its value:
//Get our object for this position
var item = items[position];
var view = (convertView ??
context.LayoutInflater.Inflate(
Resource.Layout.DeliveriesListItem, parent,
false)) as LinearLayout;
//Find references to each subview in the list item's view
var imageItem = view.FindViewById(Resource.Id.imageItem) as ImageView;
var textDeliveryNo = view.FindViewById(Resource.Id.textDeliveryNo) as TextView;
var textDeliveryName = view.FindViewById(Resource.Id.textDeliveryName) as TextView;
var textDeliveryAddress = view.FindViewById(Resource.Id.textDeliveryAddress) as TextView;
var textDeliveryCityAndZip = view.FindViewById(Resource.Id.textDeliveryCityAndZip) as TextView;
//Assign this item's values to the various subviews
imageItem.SetImageResource(item.Image);
ERROR LINE> textDeliveryNo.SetText(item.DeliveryNo, TextView.BufferType.Normal);
textDeliveryName.SetText(item.LocationName, TextView.BufferType.Normal);
textDeliveryAddress.SetText(item.Address, TextView.BufferType.Normal);
textDeliveryCityAndZip.SetText(item.City + ", " + item.PostalCode, TextView.BufferType.Normal);
the debugger stops at the ERROR LINE with the error:
Android.Content.Res.Resources+NotFoundException:
I tryied to clean, rebuild the project... restart VS 2010 but nothing.
If I comment this line it will compile but it will show "12345" or whatever I put on the android:text property.
I would apreciate any help I can get.
Thank you very much!

The overload of SetText you are using wants a resource id, when you just want to set it to a string (that happens to be a number).
Use this instead:
txtDeliveryNo.Text = item.DeliveryNo.ToString ();

Related

TextSelection in a TextField issue

After selecting the whole text in a TextField using TextSelection() it does indeed select the whole text but after pressing a key on the keyboard, it starts adding pressed letters/numbers to the start of the text as opposed to deleting the old one and replacing it with the newly typed letters/numbers.
Is this expected behavior? If so, is there any way I can programatically select the text and then replace it upon pressing a key on the keyboard?
This is how I select the text:
manualEditorNode.addListener(() {
if (manualEditorNode.hasFocus) {
manualInputController.selection = TextSelection(
baseOffset: 0, extentOffset: manualInputController.text.length);
}
});
The following works for me in my program. Maybe you can try something like this?
var cursorPos = textInputController.selection;
setState(() {
textInputController.text = newInput;
if (cursorPos.start > newInput.length) {
cursorPos = new TextSelection.fromPosition(
new TextPosition(offset: newInput.length));
}
textInputController.selection = cursorPos;
});

Xamarin Android Widget: How to change Height of ImageView programmatical?

I have a widget.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/widget"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
style="#style/WidgetBackground">
<ImageView
android:id="#+id/myimage"
android:layout_width="20dip"
android:layout_height="20dip"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/myimage" />
<TextView
android:id="#+id/mytext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/WidgetText.Title" />
How can I change the size of the image?
[Service]
public class UpdateService : Service
{
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
var updateViews = new RemoteViews(context.PackageName, Resource.Layout.widget);
// changing textsize works perfectly:
updateViews.SetTextViewTextSize(Resource.Id.mytext, (int)ComplexUnitType.Sp, 14);
updateViews.SetViewVisibility(Resource.Id.myimage, ViewStates.Gone);
updateViews.SetTextViewText(Resource.Id.text, "new text");
// but changing imagesize does not work, I didn't find a right way to set the size, need something like this:
updateViews.SetInt(Resource.Id.myimage, "layout_width ???", 200dip ???);
ComponentName thisWidget = new ComponentName(this, Java.Lang.Class.FromType(typeof(Widget)).Name);
AppWidgetManager manager = AppWidgetManager.GetInstance(this);
manager.UpdateAppWidget(thisWidget, updateViews);
}
updateViews has several setInt, setFloat, Set.. but I didn't find the right method. Any hint please?
It's impossible to change the height or width of the ImageView in code, but there is some workaround.
You could try to create a new widget layout that the size of your ImageView is what you want. When you call RemoteViews() use it instead of the original one. And when you update the widget, it will be redrawn.
For example:
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
//var updateViews = new RemoteViews(this.PackageName, Resource.Layout.widget);
//Using another layout instead.
var updateViews = new RemoteViews(this.PackageName, Resource.Layout.layout1);
ComponentName thisWidget = new ComponentName(this, Java.Lang.Class.FromType(typeof(MyWidget)).Name);
AppWidgetManager manager = AppWidgetManager.GetInstance(this);
manager.UpdateAppWidget(thisWidget, updateViews);
///...
///...
}
Or you could also try to add manyImageView of different size in your layout and set the Visibility to Gone. Show the one you want to display in the code by calling SetViewVisibility().
You can try this method:
var videoContainer = FindViewById<RelativeLayout>(Resource.Id.local_video_container);
var parameters = videoContainer.LayoutParameters;
parameters.Height = (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, containerHeight, Resources.DisplayMetrics);
parameters.Width = (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, containerWidth, Resources.DisplayMetrics);
videoContainer.LayoutParameters = parameters;

Swift 2 - Cant Loop UIView with Containing UITextField and Label

I am using swiftyjson to grab some json data and display it in a message box UITextView and UILabel. I have a for LOOP but i can not get the contents to loop inside of my UIScrollview.
I am creating a message box that displays username, date and message inside of a UIView.
My code:
for (_, subJson) in json["messageData"] {
self.outterMessageBox.hidden = false
self.usernameBox.text = subJson["user"].string
self.messageTXTBOX?.text = subJson["message"].string
self.dateBox.text = subJson["datetime"].string
}
And this is the output. Looks fine, just will not loop inside of my scroll view. I've also tried to programmatically create these views with the below code... It still does not loop. What can I be doing wrong?
Programmatically Created Views (i've displays only one iteration of the message box and does not loop as expected).
let myField: UITextField = UITextField (frame: self.ms.bounds)
myField.text = subJson["message"].string! + "\n"
self.ms.addSubview(myField)
let myNameLabel: UILabel = UILabel (frame: self.nm.bounds);
myNameLabel.text = subJson["user"].string
self.nm.addSubview(myNameLabel)
let myDt: UILabel = UILabel (frame: self.dt.bounds)
myDt.text = subJson["datetime"].string
self.dt.addSubview(myDt)
Any help with this would be greatly appreciated.

How to enable hyperlink in UILabel

What I have is a UILabel (loaded with html) that I put on a UIScrollView.
In the html I have lots of links that I can´t get to "fire-up" when clicked.
I have added UserInteractionEnabled = true both on the UILabel and on the UIScrollview but I can´t get any reaction from the taps on the hyperlinks.
This is the code that create the label with the content.
var attr = new NSAttributedStringDocumentAttributes();
var nsError = new NSError();
attr.DocumentType = NSDocumentType.HTML;
var entry = "some text and url <a href='http://www.google.com'>url</a>";
var myHtmlData = NSData.FromString(entry, NSStringEncoding.Unicode);
contentLabel.AttributedText = new NSAttributedString(myHtmlData, attr, ref nsError);
contentLabel.Frame = new CGRect(0, 20, scrollView.Frame.Size.Width , scrollView.Frame.Size.Height);
contentLabel.LineBreakMode = UILineBreakMode.WordWrap;
contentLabel.Lines = 0;
contentLabel.SizeToFit();
Maybe this is just wrong.. putting html content into a label? But I don´t want to use a webview since it does not seem to scale that well.
Why not using a UITextView instead ? Enable Links detection and disable editable and Selectable option.
Adding HTML to UILabel will not make it interactive.
Use UITextView or https://github.com/TTTAttributedLabel/TTTAttributedLabel

When I move a view's frame it doesn't move: why?

I am trying to pin a bar (amongst other things) to the bottom of the screen. The code is:
var screen = UIScreen.mainScreen()
var bottom_view = view.viewWithTag(bottom_panel_tag)!
var pad = bottom_view.frame.minX // is 0
var screen_h = screen.bounds.maxY
var screen_w = screen.bounds.maxX
var b = CGRectMake( pad, screen_h - ( pad + bottom_view.frame.height ), screen_w - 2*pad, bottom_view.frame.height )
bottom_view.frame = b
bottom_view.setNeedsDisplay()
This called in viewDidLoad and I have stepped it in the debugger so I know it runs. The view doesn't move (or any other I change). There are no constraints. What am I doing wrong?
Try this:
self.view.removeConstraints(self.view.constraints())
bottom_view.setTranslatesAutoresizingMaskIntoConstraints(true)
add this line in viewDidLoad() before you change its frame.
Explanation: Even if you don't add any Autolayout constraint it will
add at runtime if you create views using Storyboard & if you don't
want to use this solution then you should create view programatically
then it will change frame using your code only.

Resources