I try to create a CustomRenderer for Android inherit to ButtonRenderer with an Image. I want change color of Image programmatically like TintColor on iOS.
For example, my CustomRenderer iOS :
public class IconButtonRenderer : ButtonRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
base.OnElementChanged(e);
var button = e.NewElement;
}
public override void Draw(CoreGraphics.CGRect rect)
{
base.Draw(rect);
// Here I change color of Image in Red.
Control.ImageView.Image = Control.ImageView.Image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate);
Control.ImageView.TintColor = UIColor.Red;
}
}
On Android:
Control is an Android.Widget.Button and not have Image property but Xamarin.Forms.Button has property Image and use Android.Widget.Button for Android.
How I can access image with format Android for change color?
Image tint Button Android Xamarin Forms
Orignal Image
Solution 1 :
As #Mathias Kirkegaard said, in your ButtonRenderer you could use SetColorFilter to change color :
Control.Background.SetColorFilter(Android.Graphics.Color.Blue, PorterDuff.Mode.SrcIn);
It did work in native Android, but has some problem in Xamarin.Forms, it only changes the Button background color like this: effect. Hope someone could find a solution, and if I find a solution to solve this issue I will come back and update my answer.
Solution 2 :
You could use the plugin : Tinted Image and add a click event to implement the same feature.
Install the Plugin.CrossPlatformTintedImage nuget package
Initialize the renderer in your iOS, Android, and UWP projects as shown below:
Xamarin.Forms.Init();
TintedImageRenderer.Init();
In Xaml:
<ContentPage
...
xmlns:controls="clr-namespace:Plugin.CrossPlatformTintedImage.Abstractions;assembly=Plugin.CrossPlatformTintedImage.Abstractions"
...>
...
<controls:TintedImage x:Name="buttonImage" TintColor="Blue" Source="redDis.png"/>
...
</ContentPage>
In code, Adding a Tap Gesture Gesture Recognizer :
var tapGestureRecognizer = new TapGestureRecognizer();
tapGestureRecognizer.Tapped += (s, e) =>
{
// handle the tap
};
buttonImage.GestureRecognizers.Add(tapGestureRecognizer);
Effect after adding a click event on Tinted Image
Solution 3 :
If you need to use ImageButton, you could refer to this solution. I didn't test it but you could use the ImageButton from XLab.
XLab ImageButton Source code
XLab ImageButtonRenderer Source code
On a button you can use "SetColorFilter(color)"
You might be able to get the color using the ContextCompat:
var color = new Color(ContextCompat.GetColor(Context, Resource.Color.Green));
and then apply it:
yourButton.SetColorFilter(color)
Not sure whether you need to use an ImageButton rather than a simple Button.
Related
I am using MediaElement for playing audio. I have a list of items and I need to play audio one after another. When playing audio I will change the background color of that list item. Also If I tap an item, the audio of that selected item starts playing, also the color of that item is modifying. These features are working fine in android. I have added a video of the android here.
Because of Native Crash Reporting of MediaElement, I have added a custom renderer in the ios platform. After adding the custom renderer, there are some issues while playing audio in ios. After playing the first audio, the second and third items background color is modifying. After that, the next 2 items color is modifying. I need to play items one by one.
My code:
//Xaml
<MediaElement
x:Name="audio_mediaelement"
MediaEnded="AudioEnded"
ShowsPlaybackControls="True">
<MediaElement.HeightRequest>
<OnIdiom x:TypeArguments="x:Double">
<OnIdiom.Phone>80</OnIdiom.Phone>
<OnIdiom.Tablet>120</OnIdiom.Tablet>
<OnIdiom.Desktop>80</OnIdiom.Desktop>
</OnIdiom>
</MediaElement.HeightRequest>
</MediaElement>
//Initial audio source set up
audio_mediaelement.Source = aduiourl
//When audio end
public void AudioEnded(object sender, EventArgs args)
{
audioOrder = audioOrder + 1;
if(audioOrder < listVerses.Count)
{
audio_mediaelement.Source = aduioFormat + listVerses[audioOrder].audioUrl;
ChapterList.ItemsSource = listVerses;
listVerses[audioOrder].BGColor = Color.FromHex("#f2ee71");
ChapterList.ScrollTo(((IList)ChapterList.ItemsSource)[audioOrder-1], ScrollToPosition.Start, true);
}
}
I have uploaded a sample project here for reference since it is very difficult to understand the issue without a sample. If I didn't add the MediaElement custom renderer, the app will hang after playing audio.
You could set the AutoPlay as False in default and play the video manually .
<MediaElement
HeightRequest="400"
AutoPlay="False"
x:Name="media"
MediaEnded="AudioEnded"
ShowsPlaybackControls="True">
</MediaElement>
in Code behind
Play the video after setting the url
media.Source = xxx;
media.Play();
And when want to play the next video
public void AudioEnded(object sender, EventArgs args)
{
media.Stop();
media.Source = new url;
media.Play();
}
Is there an Image Button Renderer in Xamarin.MacOS?
I am using Xamarin.Forms and all my image buttons in the common code are not shown at all in Xamarin.MacOS. Is Image Button supported in Xamarin.MacOS?
Is there an Image Button Renderer in Xamarin.MacOS?
Unfortunately, after checking the source code of Xamarin.Forms.Platform.macOS, there is no ImageButtonRenderer there now. I guess the reason that there is no control is the similar with ImageButton. I'm not sure whehter designer will add this in the future. Whatever, now it not exists.
Is Image Button supported in Xamarin.MacOS?
From the above said, MacOS has no control like ImageButton of Forms. Therefore, if used ImageButton in Xamarin Froms, it will not show in Xamarin.MacOS.
However, there is a workaround. You could use Button in Forms, and add Image in ButtonRenderer in Xamarin.MacOS. Because NSButton has a Image property.
For example, the code of ButtonRenderer as follows:
[assembly: ExportRenderer (typeof(MyButton), typeof(MyButtonRenderer))]
namespace CustomRenderer.MacOS
{
public class MyButtonRenderer : ButtonRenderer
{
protected override void OnElementChanged (ElementChangedEventArgs<Button> e)
{
base.OnElementChanged (e);
if (Control != null) {
// do whatever you want to the UITextField here!
Control.Image= NSImage.ImageNamed("tree")
Control.ImageScaling = NSImageScale.AxesIndependently;
}
}
}
}
The image source (tree.png) is the same way with iOS to be added in Resource folder, and set Propertied : Build Action -> BundleResource.
I am looking for "Highlight the Picker with Background color whenever it is clicked" and also "Title Color"
And when i click other picker the previous picker has to be change to default color, and current clicked picker has to be change color.
I tried with PickerName.BackgroundColor property in code behind, but its not working properly, sometimes its not changing. Is there any other way or how to achieve this using custom renderer or anything?. Thanks in advance.
We can change BackgroundColor in SelectedIndexChanged event in PCL.
picker.SelectedIndexChanged += (sender, args) =>
{
picker.BackgroundColor=Color.Red;
};
(or)
We can achieve this through CustomRenderer.
Example:
https://forums.xamarin.com/discussion/18563/custom-renderer-for-picker
Xamarin.iOS
http://www.c-sharpcorner.com/article/uipickerview-in-xamarin-ios/
Find Override method for Picker Selection in Xamarin.iOS and put that override method in CustomRenderer and in that change the background color
try below code
Device.BeginInvokeOnMainThread(() =>
{
picker.BackgroundColor = Color.Red;
picker.TextColor=Color.Pink;
});
I'd like to know if there is a way to add a button in scene view when in edit mode in unity.
I'd like to choose an option from the gameobject menu and then deactivate it when the button on scene is clicked (before pressing run)
Is this possible please?
I think you are asking about Unity3D game engine. Well, by means of Editor scripts you can add a button in Editor mode. For example:
using UnityEngine;
using UnityEditor;
using System.Collections;
public class LevelChunksAnalyser : MonoBehaviour {
[MenuItem("Level Chunks/Update Metadata")]
public static void UpdateLevelChunksMetadata()
{
Debug.Log("Yo");
}
}
I'm using JavaFx for a little app and a want to display a tooltip on a textArea when the user is clicking on a "help" button.
No problem for linking a tootltip to my textArea, but no way to activate it when the user click on the button. Is there a way to do that?
This is what you are looking for:
final Button helpButton = new Button("Help");
helpButton.setOnAction(new EventHandler()
{
public void handle(Event arg0)
{
showTooltip(stage, helpButton, "test tool tip", null);
}
});
public static void showTooltip(Stage owner, Control control, String tooltipText,
ImageView tooltipGraphic)
{
Point2D p = control.localToScene(0.0, 0.0);
final Tooltip customTooltip = new Tooltip();
customTooltip.setText(tooltipText);
control.setTooltip(customTooltip);
customTooltip.setAutoHide(true);
customTooltip.show(owner, p.getX()
+ control.getScene().getX() + control.getScene().getWindow().getX(), p.getY()
+ control.getScene().getY() + control.getScene().getWindow().getY());
}
Just pass the button as an input instead of control.
The ability to display a Tooltip on demand requires a resolution of RT-19538 Customizable visibility timing for Tooltip, which is not implemented in JavaFX 2.2.
As a workaround, you could try any of the possible strategies below:
Displaying your Tooltip data in a ContextMenu instead. With a ContextMenu, you have complete control over when it is shown.
You could create a custom PopupControl for your required functionality.
You could replace the default TooltipSkin with a custom implemented skin which allows you to control when the Tooltip is displayed.
You could implement RT-19538 and provide a patch to the Tooltip and TooltipSkin to the openjfx project.
3rd party libraries such as Jide's JavaFX Beta Release provide special classes like Decorator utilities, IntelliHints and ShapedPopups which might be useful in your case.
The following show a tooltip over control.
If controlhas a Tooltip assigned to, this tooltip is not chnaged.
public static void showOneTimeTooltip(Control control, String tooltipText) {
Point2D p = control.localToScreen(5 , 5);
final Tooltip customTooltip = new Tooltip(tooltipText);
customTooltip.setAutoHide(false);
customTooltip.show(control,p.getX(),p.getY());
PauseTransition pt = new PauseTransition(Duration.millis(2000));
pt.setOnFinished(e->{
customTooltip.hide();
});
pt.play();
}