How to set programatically a selector to a checkbox - xamarin

I need to set a selector to checkboxes that are created programatically, this way:
var cbAll = new CheckBox(Activity);
LinearLayout.LayoutParams llAll = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
cbAll.Text = "All zones";
llAll.LeftMargin = 27;
llAll.TopMargin = 24;
cbAll.ButtonDrawable = //what to put here?
My selector (named cb_edit.xml) in drawable folder:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/cb_active" android:state_checked="true" />
<item android:drawable="#drawable/cb_default" android:state_checked="false"/>
</selector>

i don't know what exactly you trying.but if u setting background then try this
cbAll.SetBackgroundResource( Resource.Drawable.cb_edit);

First, add an XML into your drawable folder:
CheckBoxDrawable.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="#drawable/CheckedImage" />
<item android:state_checked="false" android:drawable="#drawable/UnCheckedImage" />
</selector>
Then declare the checkbox and set the button drawable to it something like this:
Checkbox btnOption = new Checkbox(context);
btnOption.SetButtonDrawable(Resource.Drawable.CheckBoxDrawable);
That should make it work in case it does not revert

Related

Android: Failed to find configured root that contains /data/data/com.app.myapp/files/myfile.pdf

I have a PDF file I download from an API and save to internal storage using Environment.SpecialFolder.MyDocuments. When I attempt to open the file I get the error in the title, Failed to find configured root that contains /data/data/com.app.myapp/files/myfile.pdf.
Code that saves files:
var filePath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), fileName);
System.IO.File.WriteAllBytes(filePath, fileBytes);
LaunchIntent(filePath);
Code where error occurs:
public static void LaunchIntent(string fileLocation)
{
// now create an activity which points to the file
var localPath = global::Android.Net.Uri.Parse(fileLocation).Path;
File javaFile = new File(localPath);
//error occurs at below code
var filePath = global::Android.Support.V4.Content.FileProvider.GetUriForFile(
CrossCurrentActivity.Current.Activity,
CrossCurrentActivity.Current.Activity.PackageName + ".fileprovider",
javaFile);
var actionView = new Intent(Intent.ActionView);
actionView.AddFlags(ActivityFlags.GrantReadUriPermission);
var extension = localPath.Substring(localPath.LastIndexOf('.') + 1);
var mimeType = MimeTypeMap.Singleton.GetMimeTypeFromExtension(extension);
actionView.SetDataAndType(filePath, mimeType);
CrossCurrentActivity.Current.Activity.StartActivity(actionView);
}
My file paths file:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external" path="." />
<external-files-path name="external_files" path="." />
<cache-path name="cache" path="." />
<external-cache-path name="external_cache" path="."/>
</paths>
According to your description, you pdf is saved in internal storage, Provider path for a specific path as followings:
<files-path/> --> Context.getFilesDir()
<cache-path/> --> Context.getCacheDir()
<external-path/> --> Environment.getExternalStorageDirectory()
<external-files-path/> --> Context.getExternalFilesDir(String)
<external-cache-path/> --> Context.getExternalCacheDir()
<external-media-path/> --> Context.getExternalMediaDirs()
You can refer to: https://developer.android.com/reference/androidx/core/content/FileProvider
So change your resources/xml/file_paths.xml
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
<files-path name="internal_files" path="." />
</paths>
And adding a provider in the AndroidManifest.xml file Application tag.
<provider android:name="androidx.core.content.FileProvider" android:authorities="${applicationId}.fileprovider" android:exported="false" android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="#xml/file_paths"></meta-data>
</provider>
</application>

How to edit vsixmanifest using groovy without loosing content?

My question may sound stupid but I am struggling to achieve my result. I would like to edit and save version tage of vsixmanifest file without loosing any content. This article almost solved my purpose but it removes some of the tags.
<?xml version="1.0" encoding="utf-8"?>
<Vsix xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Version="1.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2010">
<Identifier Id="Visual Test">
<Name>Visual Gallery Test</Name>
<Author>Visual Studio Demo</Author>
<Version>XXXXX</Version>
<Description xml:space="preserve">Visual Studio Gallery Demo</Description>
<Locale>1033</Locale>
<AllUsers>true</AllUsers>
<InstalledByMsi>false</InstalledByMsi>
<Icon>Resources/Icon.png</Icon>
<PreviewImage>Resources/Preview.png</PreviewImage>
<SupportedProducts>
<IsolatedShell Version="7.0">Visual Studio</IsolatedShell>
</SupportedProducts>
<SupportedFrameworkRuntimeEdition MinVersion="4.6" MaxVersion="4.9" />
</Identifier>
<Content>
<VsPackage>XX.pkgdef</VsPackage>
</Content>
</Vsix>
Here is my gradle script
task updateExtensionManifest{
def vsixmanifestFile = "build/source.extension.vsixmanifest"
def vsixmanifest = new XmlParser().parse(vsixmanifestFile)
vsixmanifest.Identifier[0].Version[0].value="YYYYY"
def nodePrinter = new XmlNodePrinter(new PrintWriter(new FileWriter(vsixmanifestFile)))
nodePrinter.preserveWhitespace = true
nodePrinter.expandEmptyElements = true
nodePrinter.print(vsixmanifest)
}
When I execute the script it removes some of the tags defines manifest file, this is how it looks after task is getting executed:
<Vsix xmlns="http://schemas.microsoft.com/developer/vsx-schema/2010" Version="1.0.0">
<Identifier Id="Visual Test">
<Name>Visual Gallery Test</Name>
<Author>Visual Studio Demo</Author>
<Version>YYYYY</Version>
<Description xml:space="preserve" xmlns:xml="http://www.w3.org/XML/1998/namespace">Visual Studio Gallery Demo</Description>
<Locale>1033</Locale>
<AllUsers>true</AllUsers>
<InstalledByMsi>false</InstalledByMsi>
<Icon>Resources/Icon.png</Icon>
<PreviewImage>Resources/Preview.png</PreviewImage>
<SupportedProducts>
<IsolatedShell Version="7.0">Visual Studio</IsolatedShell>
</SupportedProducts>
<SupportedFrameworkRuntimeEdition MinVersion="4.6" MaxVersion="4.9"></SupportedFrameworkRuntimeEdition>
</Identifier>
<Content>
<VsPackage>XX.pkgdef</VsPackage>
</Content>
</Vsix>
Some unwanted edits:
Line1 removed: <?xml version="1.0" encoding="utf-8"?>
Line2 modified to <Vsix xmlns="http://schemas.microsoft.com/developer/vsx-schema/2010" Version="1.0.0">
Tag "Description" got edited too..
How could I avoid this edits? I just expected the version to get modified from XXXXX to YYYYY without changing any other content through my gradle build script.
That is because XmlNodePrinter.
If xml markup declaration is needed, use XmlUtil.serialize().
def vsixmanifest = new XmlSlurper().parse(vsixmanifestFile)
vsixmanifest.Identifier[0].Version.replaceBody ( "YYYYY" )
println groovy.xml.XmlUtil.serialize(vsixmanifest)
You can quickly try the demo

How to add cq:listener to a component

I am trying to add cq:listener https://docs.adobe.com/docs/en/cq/5-5/developing/components/edit_config.html#cq:listeners to my component so that after selecting an asset and clicking "ok", the page refreshes.
This is how the component looks on the screen where I select an asset and click ok.
In my code, the dialog.xml looks like this
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
jcr:primaryType="cq:Dialog"
xtype="dialog">
<items
jcr:primaryType="cq:Widget"
xtype="tabpanel">
<items jcr:primaryType="cq:WidgetCollection">
<tab1
jcr:primaryType="cq:Panel"
title="Tab">
<items
jcr:primaryType="cq:WidgetCollection">
<asset-reference
jcr:primaryType="cq:Widget"
fieldLabel="Foo Bar:"
fieldDescription="Select the asset under /content/dam/foo-sync"
name="./fileReference"
xtype="pathfield"
rootPath="/content/dam/evernote-sync"/>
</items>
</tab1>
</items>
</items>
</jcr:root>
_cq_editConfig.xml looks like this
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0"
jcr:primaryType="cq:EditConfig">
<cq:dropTargets jcr:primaryType="nt:unstructured">
<fileReference
jcr:primaryType="cq:DropTargetConfig"
accept="[text/.*]"
groups="[media]"
propertyName="./fileReference"/>
</cq:dropTargets>
</jcr:root>
Create a node with name cq:listeners and type as cq:EditListenersConfig as a child of the cq:EditConfig node. Add the property afteredit to the newly created node with value REFRESH_PAGE, to refresh the page once the component is edited. By default this value is REFRESH_SELF, hence only the component is refreshed after edit and not the entire page.
The _cq_editConfig.xml would look something like this.
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0"
jcr:primaryType="cq:EditConfig">
<cq:listeners jcr:primaryType="cq:EditListenersConfig"
afteredit="REFRESH_PAGE"/>
<cq:dropTargets jcr:primaryType="nt:unstructured">
<fileReference
jcr:primaryType="cq:DropTargetConfig"
accept="[text/.*]"
groups="[media]"
propertyName="./fileReference"/>
</cq:dropTargets>
</jcr:root>
More details can be found in the link mentioned in the question itself.
You might try the following in your _cq_editConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0" jcr:primaryType="cq:EditConfig">
<cq:listeners
jcr:primaryType="cq:EditListenersConfig"
afterdelete="REFRESH_PAGE"
afteredit="REFRESH_PAGE"
afterinsert="REFRESH_PAGE"
afterMove="REFRESH_PAGE"/>
</jcr:root>

Add comment nodes outside root with ruby-libxml

I am writing an xml exporter in ruby and I am using libxml package for it.
I want to write some comment nodes outside the root element
<?xml version="1.0" encoding="UTF-8"?>
<!-- comment -->
<root>
<childnode />
</root>
How do I accomplish export to above format?
Sample ruby code to generate the above (without accounting for comment node)
doc = XML::Document.new()
rootNode = XML::Node.new('root')
doc.root = rootNode
childNode = XML::Node.new('childnode')
childnode << rootNode
ended up editing the xml string manually to add the comments outside the root node (for both libxml and nokogiri
<?xml version="1.0" encoding="UTF-8" ?>
<List type = "" =”00:75:00” =”00:00:05”>
</List>
Yes
<?xml version="1.0" encoding="UTF-8" ?>
<List type = "update" >
</List>

Can't get XPathSelectElements to work with XElement

I am creating an in-memory Xml tree using XElement. Here is a sample of my xml:
<Curve>
<Function>createCurve</Function>
<Parameters>
<Input>
<BaseCurve>
<CurveType Type="String">16fSmoothCurve</CurveType>
<Ccy Type="String">USD</Ccy>
<Tenors>
<Item Type="String">1M</Item>
<Item Type="String">3M</Item>
<Item Type="String">1U</Item>
<Item Type="String">Z1</Item>
</Tenors>
<Rates>
<Item Type="String">.02123</Item>
<Item Type="String">.02214</Item>
<Item Type="String">.021234</Item>
<Item Type="String">.02674</Item>
</Rates>
</BaseCurve>
</Input>
</Parameters>
</Curve>
I am creating the xml by chaining together XElements. For example,
var root = new XElement("Curve",
new XElement("Function", "createCurve"),
new XElement("Parameters"), etc);
I would then like to query the XElement via XPath. For example,
var tenors = root.XPathSelectElements("//Tenors/Item");
var rates = root.XPathSelectElements("//Rates/Item");
I can successfully select a single element, for example,
var firstTenor = root.XPathSelectElement("//Tenors/Item");
var firstRate = root.XPathSelectElement("//Rates/Item");
However, trying to select multiple elements give me 0 results.
I've tried creating an XDocument and querying off of that however I get the same results. I've also tried adding an XDeclaration to the beginning of the tree but no luck.
Why can I not query multiple elements from my XElement tree?
Thanks!
Drew
Use XmlNodeList:
XmlNodeList nodesXml = root.SelectNodes("//Tenors/Item");
foreach (XmlNode item in nodList)
{
var tenors = item.InnerText;
}
That what I do, and it works perfect.

Resources