Remove shadow from Android Button built in Xamarin forms - android

I am trying to get rid of the shadow that is being displayed at the bottom of a button in android built using xamarin forms. I have tried all that I could. But I have not achieved it.
I have attached an image for reference.
I request your'l to help me and put me out of my misery.
Thanks in advance

1) Create custom control and derive it from Button.
public class ButtonWithoutShadow : Button
{
}
2) Create custom renderer
[assembly: ExportRenderer(typeof(ButtonWithoutShadow), typeof(ButtonWithoutShadowRenderer))]
public class ButtonWithoutShadowRenderer : ButtonRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
base.OnElementChanged(e);
if (Control != null)
{
Control.Elevation = 0;
}
}
}
3) In xaml page use this button:
<controls:ButtonWithoutShadow TextColor="White" HorizontalOptions="Center" WidthRequest="185" HeightRequest="52" BackgroundColor="#ffcd00" Font="Roboto-Regular" FontSize="23" Text="Поиск" BorderRadius="0" BorderWidth="0" />

[assembly: ExportRenderer(typeof(Button),typeof(FlatButtonRenderer))]
namespace Project.Droid
{
public class FlatButtonRenderer : ButtonRenderer
{
protected override void OnDraw(Android.Graphics.Canvas canvas)
{
base.OnDraw(canvas);
}
}
}
<Button BackgroundColor="Transparent" Text="ClickMe"/>
Source : https://stackoverflow.com/a/39966574/7794690

For me, only thing that removed the shadow was doing this in a custom button render:
Control.StateListAnimator = null;
It may be dependent on API level, though, so might also require:
Control.Elevation = 0;

Related

how to do picker to use only one line to show the selected value?

I am working with xamarin.forms and I am using a picker.
But, depend of the selected value, on android it breaks the line...But I need it always use only one line. How can I do that?
You can create a custom renderer for the Picker as the comment suggested, and then in your renderer, you can for example code like this:
[assembly: ExportRenderer(typeof(MyPicker), typeof(MyPickerRenderer))]
namespace NameSpace.Droid
{
public class MyPickerRenderer : PickerRenderer
{
private bool isShown;
protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
var et = this.Control as EditText;
et.SetSingleLine(true);
}
}
}
}

How to set page icon in Xamarin Forms?

I'm trying to set page icon. There is App class of PCL below.
public App()
{
CustomMainPage mainPage = new CustomMainPage();
NavigationPage rootPage = new NavigationPage(mainPage);
this.MainPage = rootPage;
}
What I tried to do?
NavigationPage.SetTitleIcon(mainPage, "icon.png");
The second approach.
NavigationPage.SetTitleIcon(this, "icon.png");
The third approach.
NavigationPage.SetTitleIcon(rootPage, "icon.png");
File icon.png is situated into Resources/drawable.
And finally I decided to implement my custom renderer for the NavigationPage in Xamarin Forms. NavigationPage is setted to MainPage property into App class of PCL.
I created DroidNavigationRenderer class into Android project.
[assembly: ExportRenderer(typeof(NavigationPage), typeof(DroidNavigationPageRenderer))]
namespace App1.Droid.DroidRenderers
{
public class DroidNavigationPageRenderer : NavigationPageRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<NavigationPage> e)
{
base.OnElementChanged(e);
var actionBar = ((Activity)Context).ActionBar;
actionBar.SetIcon(Resource.Drawable.icon);
}
}
}
But actionBar is always returns as null.
What I do wrong and how to fix it?
In your ContentPage, you have the Icon property.
You can set it this way
Icon = Device.OnPlatform("Menu", "ic_fa_bars.png", "Assets/Icons/reload.png");
What is your CustomMainPage class? If I have TabbedPage and set one of its child I do it this way:
var navigationPage = new NavigationPage(mainPage);
if (Device.RuntimePlatform == Device.iOS)
{
navigationPage.Icon = "services.png";
}
hope it helps

Add Mapbox to Android View in Xamarin Forms Custom Renderer

I cannot figure out how to get a Mapbox map going in a custom view renderer on Android using Xamarin.Forms. It's driving me bonkers.
In my PCL, I have a map view.
public class MapView: View
{
public MapView() { }
}
For iOS, the "getting started" help was close enough to get it working on iOS, like so:
[assembly: ExportRenderer (typeof(Shared.Mobile.MapView), typeof(MapViewRenderer))]
namespace Clients.iOS
{
public class MapViewRenderer : ViewRenderer<Shared.Mobile.MapView, UIView>
{
protected override void OnElementChanged(ElementChangedEventArgs<Shared.Mobile.MapView> e)
{
base.OnElementChanged(e);
if (e.NewElement == null)
{
return;
}
var uiView = new UIView(new CoreGraphics.CGRect(0, 0, 500, 700));
SetNativeControl(uiView);
var mapView = new MapView(Control.Bounds);
mapView.SetCenterCoordinate(new CoreLocation.CLLocationCoordinate2D(40.81, -96.68), false);
mapView.SetZoomLevel(11, false);
mapView.AddAnnotation(new PointAnnotation
{
Coordinate = new CoreLocation.CLLocationCoordinate2D(40.81, -96.68),
Title = "Lincoln, NE",
Subtitle = "What-what"
});
uiView.AddSubview(mapView);
}
}
}
On the Android side, not so much. (https://components.xamarin.com/gettingstarted/mapboxsdk). They're putting in XML and an Activity of sorts, but my knowledge in mobile doesn't extend far from Xamarin.Forms at the moment, so I can't seem to bridge the gap between the two. My Android renderer looks like this:
public class MapViewRenderer : ViewRenderer<Shared.Mobile.MapView, Android.Views.View>
{
protected override void OnElementChanged(ElementChangedEventArgs<Shared.Mobile.MapView> e)
{
base.OnElementChanged(e);
var view = new Android.Views.View(Context);
SetNativeControl(view); // NullReferenceException will be thrown if the native control is not set
if (Control == null)
{
return;
}
var mapView = new MapView(Forms.Context, "thisismyaccesscode");
mapView.CenterCoordinate = new LatLng(41.885, -87.679);
mapView.ZoomLevel = 11;
mapView.SetMinimumHeight(250);
mapView.SetMinimumWidth(250);
mapView.AddMarker(new MarkerOptions().SetPosition(new LatLng(40.81, -96.68)).SetTitle("Lincoln, NE"));
view.AddSubview(mapView) // I wish this method existed
}
}
My final call to AddSubview(mapView) is not in fact a method of the View class as it is the UIView class on iOS. Here's where I'm stuck. I cannot figure out how to display the MapView. Please help.
As you have already mentioned you can't call AddSubview as it is an iOS method.
On Android its the equivalent of AddView.
However - You are attempting to do this type of operation on a Android View object and not on a ViewGroup object, so its not possible.
First instead of doing:-
var view = new Android.Views.View(Context);
try instantiating the MapView directly such like:-
var view = new MapView(Context, "thisismyaccesscode");
Your SetNativeControl call on the view is fine.
I haven't tried the Mapbox component, so I'm unclear on the exact parameter types it is expecting in the code above.
Should that not work, however, then do something like the following:-
var view = new Android.Widget.FrameLayout(Context);
var mapView = new MapView(Forms.Context, "thisismyaccesscode");
mapView.CenterCoordinate = new LatLng(41.885, -87.679);
mapView.ZoomLevel = 11;
mapView.SetMinimumHeight(250);
mapView.SetMinimumWidth(250);
mapView.AddMarker(new MarkerOptions().SetPosition(new LatLng(40.81, -96.68)).SetTitle("Lincoln, NE"));
view.AddView(mapView);
SetNativeControl(view);
You will have to change your first line to the following also:-
public class MapViewRenderer : ViewRenderer<Shared.Mobile.MapView, Android.Widget.FrameLayout>

Cocos2d-x Director replaceScene with Transition

i m using cocos2d-x version 3.2 and can't use Transition with animation between two scene.
ndk-stack show this error :
cocos2d::Scheduler::unscheduleAllForTarget(void*)+55
caused in this Class CCScheduler.cpp (Line 534)
HASH_FIND_PTR(_hashForTimers, &target, element);
is this a know issue ? or maybe i m doing some thing wrong in my code ?
thanks
class FirstScene : public Scene {
....
scene()...
init(){
auto overlay = NodeInFirstScene::create();
this->addChild(overlay);
}
}
Overlay
Class NodeInFirstScene : public Node {
void NodeInScene1::ButtonPressed() {
Director::sharedDirector()->replace(TransitionFade::create(1.2,SecondScene::scene());
}
}
SecondScene
Class SecondScene : public Scene {
//
scene()....
init()...
}
if i use
Director::sharedDirector()->pushScene it work fine !!
i resolve it,
First : Add method in header :
virtual void release();
virtual void onExit(); //
Second : Release any objects inside my custom class.
CC_SAFE_RELEASE(mObject);
unscheduleAllSelectors();
...
Voila,

How to custom view with BarcodeFragLibV2

I'm working on an Android application designed with Fragment that's why I'm using the BarcodeFragLib which use the Zxing library.
I'm looking for a way to custom the capture view, adding button and catch the event in my Activity.
If anyone has a hint or an ideas of how to solve my problem is welcome in advance :)
If you are using Xamarin, you could to use a event args static that when the code will be captured then you active the event and you will catch the code.
Is like:
public class GenericEventArgs<T>:EventArgs
{
public T Result;
public GenericEventArgs (T result)
{
Result = result;
}
}
and use this form:
public static event EventHandler<GenericEventArgs<string>> GetCodeBar_Completed;
if (GetCodeBar_Completed != null) {
GetCodeBar_Completed (this, new GenericEventArgs<string> (value));
}
and subcribe this.
PreferencesData.GetCodeBar_Completed += (s, a) => {/*content...*/};

Categories

Resources