Text is cutting off in a button - image

I'm displaying a button with image and text. I want to display the image first. For that, I used the content layout. But the text is displaying first. And the text is cutting off.
XAML code:
<StackLayout Orientation="Horizontal" Margin="12,20,12,20">
<Button Text="Daily Tasks" TextColor="{DynamicResource SecondaryTextColor}"
ContentLayout="Left,30" Image="{x:Static fontawesome:FontAwesomeIcons.Bars}" FontFamily="
{StaticResource FontAwesomeSolid}" FontAttributes="Bold" WidthRequest="150" BackgroundColor="
{DynamicResource ButtonBackgroundColor}" BorderRadius="20" HorizontalOptions="StartAndExpand">
</Button>
</StackLayout>
The button is displaying like below:
I need to display image first like below.

Related

Adding Xamarin.CommunityToolkit.Effects.TouchEffect disables button click

I have a Button inside a StackLayout.
<StackLayout>
<Button
Text="Button text"
Command="{Binding SelectClubCommand}" />
</StackLayout>
When clicking the button, SelectClubCommand is executed.
To have an indication that StackLayout was clicked, I want to add a TouchEffect (Xamarin.CommunityToolkit.Effects.TouchEffect) like so:
<StackLayout effects:TouchEffect.PressedScale="0.97">
<Button
Text="Button text"
Command="{Binding SelectClubCommand}" />
</StackLayout>
This effect needs to be on the StackLayout as I will add borders and BackgroundColor on it.
However, adding the TouchEffect disables the Command, it is never executed. Is there a way to prevent this?

Adding listview/button-like style behavior to layouts in a repeater in nativescript

I've got a facebook like activity feed, and I'm using a repeater because I want to have multiple clickable items within each "activity", ie, clicking on the top portion goes to the users profile, vs. the body of the activity opens the activity, plus some links for comments, likes below that.
<ScrollView orientation="vertical">
<Repeater items="{{ activities }}">
<Repeater.itemsLayout>
<StackLayout orientation="vertical" />
</Repeater.itemsLayout>
<Repeater.itemTemplate>
<StackLayout tap="goProfile" >
<Image src="{{image}}" height="35" verticalAlignment="top" />
<Label text="{{user_name}}" />
</StackLayout>
<StackLayout tap="goActivity">
...
</StackLayout>
<StackLayout orientation="horizontal">
<Image tap="addComment" src="{{comment}}" />
<Image tap="addLike" src="{{like}}" />
</StackLayout>
</Repeater.itemTemplate>
</Repeater>
</ScrollView>
This is basically working fine, except tapping on any of the tappable items does not give any visual feedback, ie. briefly turning the background of the item grey, like it does with a ListView....
I can't figure out how to get this behavior on clickable layouts within my repeater? is this possible? is there another approach I should be following?

Why does Xamarin Absolute Layout stacking not respect Children order

I am trying to achieve a layout where the content has an image on background (positioned in bottom left corner, expanding 100% vertically and 60% horizontally)
The whole content is wrapped in an AbsoluteLayout, the main content is then a child to this layout and consists of StackLayout (and some other content inside).
The previewer in Visual Studio (Visual Studio Community 2017) shows the layout correctly - the image is below the content, the content is placed on middle as intended. However, both VS Emulator and Genymotion show the image above the content.
I trimmed the code to be as readable as possible (stripping it of styling)
...
<Frame VerticalOptions="End" AbsoluteLayout.LayoutFlags="All" Margin="0" Padding="0" IsClippedToBounds="True" AbsoluteLayout.LayoutBounds="0,1,0.6,1">
<Image Source="Graphic_Anna.png" />
</Frame>
<!-- Start: Actual Page Content -->
<StackLayout VerticalOptions="Center" AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All" x:Name="ApplicationLayoutContentLevel">
<Label Text="This is some text in main content" />
</StackLayout>
<!-- End: Actual Page Content -->
</AbsoluteLayout>
</ContentPage.Content>
...
The expected result is shown on this screenshot (from Previewer): https://i.imgur.com/C8GBMSi.png
However, the result in both emulator seems to do this: https://i.imgur.com/NvJFZan.png
Unfortunately, I don't have the option to test the app on an actual Android phone at this moment
Do you want to achieved like the following screenshot?
I do not know which size that you used, so I cutout the picture from your screenshot.
If so, there is my code.If you want to achieve coveraged effect, I used two stacklayout, the first layout will be covered by the second layout.Notice:If you set the VerticalOptions will affect the AbsoluteLayout.LayoutBounds
<ContentPage.Content>
<AbsoluteLayout>
<StackLayout VerticalOptions="End" AbsoluteLayout.LayoutFlags="All" Margin="0" Padding="0" IsClippedToBounds="True" AbsoluteLayout.LayoutBounds="0,1,0.6,1">
<Image Source="Graphic_Anna.png" />
</StackLayout>
<!-- Start: Actual Page Content -->
<StackLayout AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0.8,0.7,0.5,0.5" x:Name="ApplicationLayoutContentLevel">
<Image Source="start.png" Scale="0.7"/>
<Label Text="10/10" TextColor="Black" FontSize="50" Margin="20,10,0,0"/>
<Label Text="Great job! you got everything" TextColor="Black" Margin="0,10,0,0" />
<Label Text="Back to games" TextColor="red" Margin="25,20,0,0" />
</StackLayout>
<!-- End: Actual Page Content -->
</AbsoluteLayout>
</ContentPage.Content>

How to set width of image inside button in Xamarin.Forms xaml?

I would like to know if it is possible to set the width and height of the image inside this button:
<Button Image="ic_music_white.png" BorderWidth="1" BorderColor="White" HeightRequest="56" BackgroundColor="PowderBlue" HorizontalOptions="Center" Clicked="Button_Clicked">
</Button>
Any solution?
You could always create an Image control and add a tap gesture recognizer to it. That way you have more control over the image size and placement.
XAML
<Image x:Name="myImage" Source="ic_music_white.png" HeightRequest="56" BackgroundColor="PowderBlue" HorizontalOptions="Center"/>
XAML.CS
TapGestureRecognizer tapEvent = new TapGestureRecognizer();
tapEvent.Tapped += Button_Clicked;
myImage.GestureRecognizers.Add(tapEvent);
On an iOS project, padding can adjust the size of the image within the button. If you have a width of 50, padding 10, the resulting image width will be 30.
No,you can't manage the height/width of image inside button .
The better option would be to create a stacklayout and then put text and image inside it.
The way i did it:
<StackLayout Grid.Row="2" Orientation="Horizontal" HorizontalOptions="FillAndExpand" VerticalOptions="End" HeightRequest="60">
<Label Text="Review task" HorizontalOptions="EndAndExpand" VerticalOptions="CenterAndExpand" FontSize="25" FontAttributes="Bold"/>
<Image Source="nextarrow_white.png" HorizontalOptions="StartAndExpand" VerticalOptions="CenterAndExpand" HeightRequest="32" Margin="20,4,0,0"/>
</StackLayout>
This is not possible from the Xamarin.Forms project but you can create custom renderers which will allow you to modify the properties of the native control.
In the case of the iOS you will be changing the ImageEdgeInsets.
For Android you might want to take a look at the Button renderer so you can get any ideas.
Hope this helps.-
Use <ImageButton Source"yourImage" Padding=10 Aspect="AspectFit"/>
You can controle the image size with the padding ;)
If you just want to place buttons inside a screen without adding text or other content, you could place your buttons inside a Grid layout as following
<Grid RowDefinitions="auto"
ColumnDefinitions="auto"
Margin="40 40 40 40">
<ImageButton Grid.Row="1"
Grid.Column="1"
Source="Resources/Images/installations.png"
HorizontalOptions="Center"
VerticalOptions="Center"/>
</Grid>
There, you apply a margin to the grid layout, this case images will automatically adjust their sizes to fit the available space.
You can now use scale to change the size of the content.
And padding to add the border around the image.
Example
<Button
Image="ic_music_white.png"
BorderWidth="1"
BorderColor="White"
HeightRequest="56"
BackgroundColor="PowderBlue"
HorizontalOptions="Center"
Clicked="Button_Clicked"
Scale = "0.7",
Padding = "10"
>
</Button>
You have missed to add WidthRequest. Can you add WidthRequest like below
<Button Image="ic_music_white.png" BorderWidth="1" BorderColor="White" HeightRequest="56" WidthRequest="56" BackgroundColor="PowderBlue" HorizontalOptions="Center" Clicked="Button_Clicked">
</Button>

Add logo at the middle of Navigation bar?

I want to add a logo in the middle of the navigation bar like this:
<ContentPage.ToolbarItems>
<ToolbarItem Icon="logo.png" Activated="RefButtonClicked"></ToolbarItem>
</ContentPage.ToolbarItems>
I also tried this:
var cancelItem = new ToolbarItem
{
Text = "Cancel",
Icon = "tabalilogo.png",
Order = ToolbarItemOrder.Secondary
};
Everything I tried positioned the logo on the right side of the navigation bar. How can I center it?
Recently After Updating to xamarin forms 3.2, You can customize the title bar using title bar property.
<ContentPage>
<NavigationPage.TitleView>
<StackLayout Orientation="Horizontal" VerticalOptions="Center" Spacing="10">
<Image Source="iconXamagon.png">
<Image.GestureRecognizers>
<Image.TapGestureRecognizer
Tapped="HandleTapped" />
</Image.GestureRecognizers>
</Image>
<Label Text="3.2.0" FontSize="16" TextColor="Black" VerticalTextAlignment="Center" />
</StackLayout>
</NavigationPage.TitleView>
....
For more details https://blog.xamarin.com/xamarin-forms-3-2-0-released-going-big-with-little-things/
I am not sure if that can be implemented using existing toolbar but I had similar requirement and I couldn't figure out how to achieve using the xamarin forms toolbar on a navigation page. therefore I decided to use a modal page and I disabled the navigation page in the code behind with the line of code below.
NavigationPage.SetHasNavigationBar(this, false);
I am using xaml and I created a custom navigator with absolutelayout as below
<AbsoluteLayout x:Name="slToolbar" Grid.Row="0" >
<Button x:Name="Cancel" BackgroundColor="#ADD8E6" Image="cancel.png" Command="{Binding CancelClick}" AbsoluteLayout.LayoutFlags="PositionProportional">
<AbsoluteLayout.LayoutBounds>
<OnPlatform x:TypeArguments="Rectangle" iOS="0, .5, 36, 36" Android="0, .5, 36, 36" WinPhone="0, .5, 50, 50" />
</AbsoluteLayout.LayoutBounds>
</Button>
<StackLayout Orientation="Horizontal" AbsoluteLayout.LayoutFlags="PositionProportional">
<AbsoluteLayout.LayoutBounds>
<OnPlatform x:TypeArguments="Rectangle" iOS="1,.5,110,50" Android="1,.5,50,36" WinPhone="1,.5,110,50" />
</AbsoluteLayout.LayoutBounds>
<ContentView >
<OnPlatform x:TypeArguments="View">
<OnPlatform.iOS>
<Button x:Name="NewIOS" BackgroundColor="#ADD8E6" Image="ic_add.png" Command="{Binding AddNew}" ></Button>
</OnPlatform.iOS>
<OnPlatform.WinPhone>
<Button x:Name="NewWP" BackgroundColor="#ADD8E6" Command="{Binding AddNew}" Image="ic_add.png"/>
</OnPlatform.WinPhone>
</OnPlatform>
</ContentView>
<Button x:Name="btnSave" BackgroundColor="#ADD8E6" Image="check.png" Command="{Binding SaveClick}" HorizontalOptions="End" ></Button>
</StackLayout>
</AbsoluteLayout>
this code will produce something like below. I have the icons in the corners but surely they can be placed anywhere. I used cancel icon instead of back button. Of course, in this case you need to handle back button(cancel button) click yourself. screenshot below is from uwp application but on android and ios I get similar results.

Resources