How do I change the height of a Xamarin Forms ProgressBar in code? Am using Xamarin Forms V2.1.
.HeightRequest and .MinimumHeightRequest seem to have no effect. The default progress bar is so thin that it might not even be noticed.
.BackgroundColor does not seem to work either.
What am I missing here?
You need custom renderers for this:
First create a class for your custom progress bar:
public class CustomProgressBar :ProgressBar
{
public CustomProgressBar()
{
}
}
And then also add a new file for your custom progress bar renderer:
For iOS:
public class CustomProgressBarRenderer : ProgressBarRenderer
{
protected override void OnElementChanged(
ElementChangedEventArgs<Xamarin.Forms.ProgressBar> e)
{
base.OnElementChanged(e);
Control.ProgressTintColor = Color.FromRgb(182, 231, 233).ToUIColor();// This changes the color of the progress
}
public override void LayoutSubviews()
{
base.LayoutSubviews();
var X = 1.0f;
var Y = 10.0f; // This changes the height
CGAffineTransform transform = CGAffineTransform.MakeScale(X, Y);
Control.Transform = transform;
}
}
[EDIT: fixed code above as per comment]
For Android:
public class CustomProgressBarRenderer :ProgressBarRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.ProgressBar> e)
{
base.OnElementChanged(e);
Control.ProgressTintList = Android.Content.Res.ColorStateList.ValueOf(Color.FromRgb(182, 231, 233).ToAndroid()); //Change the color
Control.ScaleY = 10; //Changes the height
}
}
I hope this helps you!
To make the progress bar thicker, you just have to change the ScaleY property of the ProgressBar.
For example:
This code
<ProgressBar Progress=".5"/>
<ProgressBar ScaleY="2" Progress=".5"/>
<ProgressBar ScaleY="5" Progress=".5"/>
Produces this
Note that you may need to adjust your margins accordingly.
<ProgressBar
BackgroundColor="White"
ProgressColor="#BCC7EF"
Progress="0.7">
<ProgressBar.ScaleY>
<OnPlatform
x:TypeArguments="x:Double"
iOS="2"
Android="1" />
</ProgressBar.ScaleY>
#BillF is correct basically
In the iOS renderer code try using
this.Control.Transform = transform;
instead of
this.Transform = transform;
I faced the same need and on the latest version of Visual Studio, 16.5.2 I figured out that to get a bigger horizontal bar you just need to set ScaleY within the progressbar declaration inside the xml.
To avoid glitches on Android and be sure that the progress bar is not overwhelming other elements I added a margin as you can see from the declaration here below.
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/progressBar1"
android:layout_below="#+id/message"
android:scaleY="8"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"/>
For me, the most simple solution is to use Xamarin.Forms.Visual.Material
Then in your XAML, in progress bar set HeightRequest property to what you want and use Visual property as Material.
Related
Instead of having the standard NavigationBar on the mainpage, I want to hide that one and load a custom one. I think using a BoxView (edit:) frame is the best method for this. However, How do I bind the height(request) of a BoxView to a (hidden) NavigationBar, preferably in Xaml?
(frame of boxview should have the same method)
For two common controls, you can bind a height of one control to another control like this(take BoxView and a Button as an example):
<BoxView Color="Yellow"
x:Name="testbox"
WidthRequest="160"
HeightRequest="60"
VerticalOptions="Center"
HorizontalOptions="Center" />
<Button Text="test..."
BindingContext="{x:Reference testbox}"
WidthRequest="160"
HeightRequest="{Binding Path=HeightRequest}"/>
Update
You can get the NavigationBar height on each platform using custom renderer.
And Then binding the height of NavigationBar to Boxview by following the method above.
For Android:
public class NaviRendererForAndroid : NavigationPageRenderer
{
public CustomNaviForAndroid(Context context) : base(context)
{
}
protected override void
OnElementChanged(ElementChangedEventArgs<NavigationPage> e)
{
base.OnElementChanged(e);
var height = 0;
Resources resources = Context.Resources;
int resourceId = resources.GetIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0)
{
height = resources.GetDimensionPixelSize(resourceId);
}
}
}
For IOS:
public class NaviRendererForiOS : NavigationRenderer
{
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
var height = NavigationBar.Bounds.Height;
}
}
I'm following the article by Xamarin that describes how to customize a pin using an image here
protected override MarkerOptions CreateMarker(Pin pin)
{
var marker = new MarkerOptions();
marker.SetPosition(new LatLng(pin.Position.Latitude, pin.Position.Longitude));
marker.SetTitle(pin.Label);
marker.SetSnippet(pin.Address);
marker.SetIcon(BitmapDescriptorFactory.FromResource(Resource.Drawable.pin));
return marker;
}
I have a png image in resources/drawable. However, the map does not show any pins. I put a breakpoint in the custom renderer and it hits so I know its being called. Here is the code for the page that implements the custom map:
Xaml
<controls:CustomMap MapType="Street" x:Name="map" WidthRequest="150" IsVisible="True" HasZoomEnabled="True" HasScrollEnabled="True">
<controls:CustomMap.HeightRequest>
<OnIdiom>
<OnIdiom.Phone>
<OnPlatform
iOS="250"
Android="150" />
</OnIdiom.Phone>
</OnIdiom>
</controls:CustomMap.HeightRequest>
</controls:CustomMap>
and the code behind
var position = new Xamarin.Forms.Maps.Position(item.Latitude.Value, item.Longitude.Value); // Latitude, Longitude
var pin = new CustomPin
{
Type = PinType.Place,
Position = position,
Label = item.Name,
Address = item.AddressString
};
pin.Clicked += async (sender, e) =>
{
await Navigation.PushAsync(new RestaurantDetails(item));
};
Device.BeginInvokeOnMainThread(() =>
{
map.Pins.Add(pin);
map.CustomPins.Add(pin);
// map.MoveToRegion(MapSpan.FromCenterAndRadius(position, Distance.FromMiles(0.3)));
});
This is solved, didn't put the images also in the drawable-xxxx folders. UGH!
For me, the issue was the CustomMap class which I was adding the CustomPins to did not raise the CreateMarker overridden method.
It may help someone else to know that you may see this same issue where the Pins do not appear on your CustomMap if you are adding your CustomPin objects to the CustomPin collection. I was able to fix this by adding each of them to the Pins collection as well.
How to change the height of a Tab Bar in Xamarin.Forms (iOS)? Is it possible with TabbedRenderer?
Yeah this is possible to modify from a CustomRenderer.
You will need to subclass the TabbedPage in the Forms project and use this class to export the render.
Then in the CustomRenderer override the ViewWillLayoutSubviews method. Something like:
public class MyTabbedPageRenderer : TabbedRenderer
{
// Modify this variable with the height you desire.
private readonly float tabBarHeight = 55f;
public override void ViewWillLayoutSubviews()
{
base.ViewWillLayoutSubviews();
TabBar.Frame = new CGRect(TabBar.Frame.X, TabBar.Frame.Y + (TabBar.Frame.Height - tabBarHeight), TabBar.Frame.Width, tabBarHeight);
}
}
Hope this helps.-
How can I make Label text Underline in WinPhone using Xamarin Forms ?
You have top create a new control in your PCL/shared project inheriting from Label.
public class Exlabel : Label
{
}
In your windows phone project create a Custom Renderer for it as follows and use the TextBlock.TextDecorations Property to set the underline.
The label is rendered as TextBlock in windows.
Sample (untested):
[assembly: ExportRenderer(typeof(Exlabel), typeof(ExlabelRenderer))]
namespace CustomRenderer.WinPhone81
{
public class ExlabelRenderer : LabelRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
if (Control != null)
{
Control.TextDecorations = TextDecorations.UnderLine;
}
}
}
}
If you are using the windows phone check out this sample - How to format texts of TextBlock using xaml in Windows Phone.
For WinRT you can use this - TextBlock underline in WinRT.
In SilverLight WinPhone (the old and not so supported template), you can also use the Margin to achieve what you require, similar to How to make an underlined input text field in Windows Phone?.
Try using following xaml;
<StackLayout Orientation="Vertical">
<Label Text="SomeText"/>
<BoxView HeightRequest="1" HorizontalOptions="FillAndExpand" BackgroundColor="Black"/>
</StackLayout>
this should do it for all 3 platforms. :)
I think you need to create a custom view for this as a Layout/Grid that has a Label and a BoxView with a small heightRequest below the label to act as a line.
Create a label renderer in your WinPhone project:
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Documents;
[assembly: ExportRenderer(typeof(ExtendedLabel), typeof(ExtendedLabelRenderer))]
namespace SampleProject.WinPhone
{
public class ExtendedLabelRenderer: LabelRenderer
{
ExtendedLabel element;
TextBlock control;
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
if((ExtendedLabel)Element == null || Control == null)
return;
element = (ExtendedLabel)Element;
control = Control;
UnderlineText();
}
void UnderlineText()
{
control.Text = string.Empty;
Underline ul = new Underline();
Run run = new Run();
run.Text = element.Text;
ul.Inlines.Add(run);
control.Inlines.Add(ul);
}
}
}
My apps is planned to allow orientation change, however, I have background images (in pivot control that allow user to change the background images) that does not want to change the orientation. How should i implement that?
thanks for advice.
You might be able to add a Rotation Transform dynamically to offset the rotation of the background image when the orientation changes, as below :-
private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
{
if (e.Orientation == PageOrientation.PortraitUp)
{
PivotBackground.RelativeTransform = null;
}
else
{
RotateTransform aRotateTransform = new RotateTransform();
aRotateTransform.CenterX = 0.5;
aRotateTransform.CenterY = 0.5;
aRotateTransform.Angle = 90;
PivotBackground.RelativeTransform = aRotateTransform;
}
}
The XAML is defined as :-
<controls:Pivot Title="MY APPLICATION">
<controls:Pivot.Background>
<ImageBrush ImageSource="/Back.jpg"
x:Name="PivotBackground">
</ImageBrush>
</controls:Pivot.Background>
</controls:Pivot>
Hope this helps.
Paul Diston