Xamarin.forms. Android: Button look on button image - android

I am making an app for Android and iOS. As far as I understand there is not possible to set a border on imagebuttons on Android. It is possible on iOS.
When I make a imagebutton it does not look like a button on Android. I cannot find a way to give it a button look. I can only find it on buttons with text.
See images for better understanding.
Anyone can help?
This is my code (new to Xamarin and xaml so it is probably not "good looking" code)
<Label Text="Velg betalingsmetode:"
TextColor="#002C6C"
FontSize="Medium"
VerticalOptions="Center"
HorizontalOptions="Center"/>
<Button x:Name="MobilePayButton"
HeightRequest="100"
WidthRequest="130"
HorizontalOptions="Center"
VerticalOptions="Center"
BackgroundColor="White"
BorderColor="#002C6C"
BorderWidth="2"
Clicked="ToPayConfirmationPage">
<Button.Image>
<OnPlatform x:TypeArguments="FileImageSource"
Android="pay_MobilePaylogo2.png"
iOS="Pay_MobilePaylogo2.png"/>
</Button.Image>
</Button>

To create a button with border, you need to create a customized button:
In PCL project add a custom button:
public class ButtonWithBorder:Button
{
}
Use it in Xaml:
<StackLayout
VerticalOptions="Center">
<local:ButtonWithBorder Clicked="ButtonWithBorder_Clicked">
<local:ButtonWithBorder.Image>
<OnPlatform x:TypeArguments="FileImageSource"
Android="gluehbirne.png"
iOS="Pay_MobilePaylogo2.png"/>
</local:ButtonWithBorder.Image>
</local:ButtonWithBorder>
</StackLayout>
In Android project, create a ButtonRenderer for our custom button:
[assembly: ExportRenderer(typeof(ButtonWithBorder),typeof(ButtonWithBorderRenderer))]
namespace ButtonWithBorderDemo.Droid
{
public class ButtonWithBorderRenderer:ButtonRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
{
base.OnElementChanged(e);
Control.Background=ContextCompat.GetDrawable(this.Context, Resource.Drawable.button_bg);
}
}
}
Add a button_bg.axml to Resources\drawable:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="3dp" />
<stroke android:width="5px" android:color="#000000" />
</shape>
And you will get a bordered button in android.

Related

Labels and Entries, by default, have no color in Xamarin.Forms

I have created a Xamarin Android Shell app. All the labels and Entries are in blank color and don't display on the android device until I change it to black or other colors. Below is a code snippet for the login page.
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="VaxineApp.Views.Login.LoginPage"
xmlns:local="clr-namespace:VaxineApp.Renderers"
Shell.FlyoutBehavior="Disabled"
Shell.NavBarIsVisible="False">
<ContentPage.Content>
<StackLayout Padding="40" VerticalOptions="Center" HorizontalOptions="FillAndExpand">
<Label Text="Welcome to Login Page" />
<Entry Placeholder="Email or Phone"></Entry>
<Entry Placeholder="Password"></Entry>
<Button Text="Sign in"></Button>
</StackLayout>
</ContentPage.Content>
</ContentPage>
I don't know whether I am missing something. That hasn't happened to me before.
See how this code renders on my Android device.

Adding background color to Xamarin Forms WebView has no impact

Page markup
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage NavigationPage.HasNavigationBar="false" xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="ConnectedLifeView.NativeApp.Pages.LoginSuccessPage">
<ContentPage.Content>
<StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" Margin="0" Padding="0">
<WebView x:Name="webView" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" BackgroundColor="#02114D"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>
Output
I tried adding background color to content page and setting WebView background color to Transparent but it has no effect either.
My all other pages have proper background but this page looks a bit weird as it shows white background while the URL loads in WebView.
Xamarin.Forms version: 4.2.0.815419
Platform: Android
If you want to change webview background color, another way is to use custom render.
public class TransparentWebViewRenderer:WebViewRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
{
base.OnElementChanged(e);
// Setting the background as transparent
this.Control.SetBackgroundColor(Android.Graphics.Color.AliceBlue);
}
}

ListView ContextAction not following layout

Wondering if anyone else has run into the following issue.
I've got my App.MainPage wrapped in a navigation page
MainPage = new NavigationPage(new MyPage());
From MyPage I navigate to a user account page which is a TabbedPage
await Navigation.PushAsync(new MyAccountTabbedPage());
and here's the xaml (pretty simple)
<TabbedPage.Children>
<userAccount:SitesTab />
<userAccount:ProjectsTab />
<userAccount:SettingsTab />
</TabbedPage.Children>
Now here's where it gets fun. From within my SitesTab, I need to show a ListView that contains ContextActions
<AbsoluteLayout>
<ListView AbsoluteLayout.LayoutBounds="0,0,1,1"
AbsoluteLayout.LayoutFlags="All"
ItemsSource="{Binding ListItems}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.ContextActions>
<MenuItem Text="Do Stuff"/>
<MenuItem Text="Delete Stuff" IsDestructive="True"/>
</ViewCell.ContextActions>
<StackLayout>
<Label Text="{Binding .}"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</AbsoluteLayout>
The issue I'm running up against is that the context action on Android doesn't cover/replace the NavigationBar, but rather pushes it down and screws everything up.
Before Context
After Context
So what's up here? Has anyone figured out a way around this? That context menu at the top of the page should cover over the NavigationBar instead of push everything down.
turns out I needed the following in my app compat theme
<style name="myActivityTheme" parent="Theme.AppCompat.NoActionBar">
<item name="windowActionModeOverlay">true</item>
</style>

Entry Custom Renderer(add all borders for Android) in Xamarin.Forms

I have a question. I need to add all borders to an entry in Android using Xamarin.Forms. I created the renderer class in the PCL and referenced it in the xaml file. Then I created the specific class for the renderer in the Android Project.
I have this:
[assembly: ExportRenderer (typeof(EntryCustom), ypeof(EntryRendererCustom))]
namespace InstagramApp.Droid.Renderers
{
public class EntryRendererCustom : EntryRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Entry>, e)
{
base.OnElementChanged(e);
if(Control != null)
{
}
}
}
}
Now i have to add the code in the IF statement but I'm new with xamarin and the renderers. Can someone help me? If someone can also explain me some basics on how to approach to the custom renderers it could be gold for me. Thanks all!
This is how you can do it :
[assembly: ExportRenderer(typeof(Entry), typeof(EntryRendererImplementation))]
namespace MyProject.Droid.Renderers
{
public class EntryRendererImplementation : EntryRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (Control != null)
{
Control.Background = this.Resources.GetDrawable(Resource.Drawable.RoundedCornerEntry);
Control.SetPadding(10,10,10,3);
}
}
}
}
You will have to create this file in Resources/drawable/RoundedCornerEntry.xml in your android project
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" >
<shape android:shape="rectangle">
<gradient
android:startColor="#color/entry_background"
android:endColor="#color/entry_background"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#color/entry_border" />
<corners
android:radius="6dp" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<gradient
android:startColor="#color/entry_background"
android:endColor="#color/entry_background"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#c6c6c6" />
<corners
android:radius="6dp" />
</shape>
</item>
</selector>
Of course, you need to update your Resources/values/colors.xml file
Something like :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="entry_background">#ffffff</color>
<color name="entry_border">#BDBDBD</color>
</resources>
There you go!
you have a lot of example for custom render in the page of xamarin.
Here is a long explanation :
https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/custom-renderer/
Here is an example for a hybridwebview:
https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/custom-renderer/hybridwebview/
Well to understand how to work with Custome Renderers there are plenty of tutorial both written and videos about that so it is better if you check that yourself ,
there are other ways to add the borders without the need of using custom renderer, and this depends on what is the image you have in mind when you say borders
Put the entry inside a frame
Put the entry above a boxView, with this you would have more control over the "Border" for example, thickness, opacity and even animation (maybe you want the borders to blink)
To do the second approach I would use a grid for that like this
<Grid>
<BoxView Color="Blue" />
<Entry/>
</Grid>
The order here is important in a grid for this case the elements which are defined first are placed below . Another obviouse point is the the height and width of the BoxView should be slightly larger than those for the Enrty, and the bigger the box the thicker the border (and I am still talking about code here ;) )

How to insert image in Toolbar in Appcelerator

I'm building an app with appcelerator studio for Android system.
So I want to display and customize a ToolBar.
So this is my index.xml layout:
<Alloy>
<Window class="container">
<ActionBar id="actionbar" title="" icon="/images/logo_decipher.png" />
<Menu>
<MenuItem id="item1" title="Settings" />
<MenuItem id="item2" title="Search" />
</Menu>
</Window>
</Alloy>
I want to display my personal Image in this Toolbar. I'm try to use tag icon but not works.
You can use actionbarExtras. https://github.com/ricardoalcocer/actionbarextras/blob/master/documentation/index.md
Ray

Categories

Resources