xamarin Flyoutpage change text color - xamarin

enter image description here
Hello friend
How can I change the color of the text in the title in the picture I am using flyoutpage
How can I change the color of the text in the title in the picture I am using flyoutpage

Do you mean the text color of tabs of TabbedPage in your screenshot?
If yes, then in android, you can try to create a Tabbar.xml in folder layout and adding following code:
<?xml version="1.0" encoding="utf-8" ?>
<com.google.android.material.tabs.TabLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:tabMode="fixed"
app:tabGravity="fill"
app:tabTextColor="#color/your_unselected_text_color"
app:tabSelectedTextColor="#color/your_selected_text_color"
app:tabIndicatorColor="#color/your_indicator_color"
/>
Then inflate this layout with code:
FormsAppCompatActivity.TabLayoutResource = Resource.Layout.Tabbar;
The whole code of MainActivity.cs
      
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate (Bundle bundle)
            {
FormsAppCompatActivity.TabLayoutResource = Resource.Layout.Tabbar;
base.OnCreate (bundle);
            global::Xamarin.Forms.Forms.Init (this, bundle);
            LoadApplication (new App ());
            }
      }
On IOS, you can use custom renderer to achieve this.
[assembly: ExportRenderer(typeof(TabbedPage), typeof(TabbedPageRenderer))]
namespace MyApp.iOS
{
public class TabbedPageRenderer : TabbedRenderer
{
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
TabBar.TintColor = UIColor.White;
TabBar.BarTintColor = UIColor.Black;
TabBar.BackgroundColor = UIColor.Gray;
}
}
}

Related

Can I have a click event for a complete Linear layout

I have a linear layout and some buttons inside the layout.When click on Linear layout it should navigate to new page1, When click on button it should navigate to page2.
Do you want to achieve the result like this GIF?
If so, yes you can add the click event for your LinearLayout.There is my code.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" android:id="#+id/my_ll">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click"
android:id="#+id/button1"
/>
</LinearLayout>
Here is background code.Add the different click listeners for linearlayout and button.
public class MainActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
LinearLayout my_ll = FindViewById<LinearLayout>(Resource.Id.my_ll);
Button button1 = FindViewById<Button>(Resource.Id.button1);
my_ll.Click += My_ll_Click;
button1.Click += Button1_Click;
}
private void Button1_Click(object sender, System.EventArgs e)
{
Intent intent = new Intent(this, typeof(Activity2));
StartActivity(intent);
}
private void My_ll_Click(object sender, System.EventArgs e)
{
Intent intent = new Intent(this,typeof(Activity1));
StartActivity(intent);
}
}

how open a pdf file from assents in an xamarin Android forms

I want to open a PDF file from assents folder to activity in Xamarin Android
in MainActivity.cs
using Com.Joanzapata.Pdfview;
using Com.Joanzapata.Pdfview.Listener;
namespace openpdf
{
[Activity(Label = "openpdf", MainLauncher = true)]
public class MainActivity : Activity
{
PDFView pdf;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Main);
**pdf = FindViewById<PDFView>(Resource.Id.pDFView1);
pdf.FromAsset("finished.pdf").Load();**
}
}
}
Main.Axml
<com.joanzapata.pdfview.PDFView
android:layout_width="match_parent"
android:layout_height="548.5dp"
android:id="#+id/pDFView1" />

Xamarin Forms WebView Crashes for Android when used with Forms Embedding

I have created a simple forms Content Page with a WebView in it. It points to "www.google.com".
I tried Forms Embedding to start the Forms page from Android as a Fragment and as a ViewController in iOS.
It works fine in iOS however it crashes in Android with the following error on the line where I create the fragment:
Value cannot be null.
Parameter name: startActivityForResult
The Code for android is shown below:
`
BaseActivity mainActivity = (BaseActivity)Activity;
FragmentManager fragmentManager = mainActivity.CurrentFragmentManager();
Forms.Init(mainActivity, null);
Fragment webViewPage = new WebViewPage().CreateFragment(mainActivity);
fragmentManager.BeginTransaction().Replace(Resource.Id.accountHelp_container, webViewPage, "Webview").AddToBackStack(null).Commit();`
Any Idea what I could be Doing wrong.
Xamarin Forms WebView Crashes for Android when used with Forms Embedding
I reproduced your problem when I transfer a Xamarin.Forms Page(which contain a WebView) to Fragment, and here is a workaround :
You could put a native android Android.Webkit.WebView in a Fragment, then you could display this Fragment to implement the same feature.
Create a WebViewFragment :
public class WebViewFragment : Fragment
{
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var view = inflater.Inflate(Resource.Layout.webview_in_fg, container, false);
WebView fg_webview = view.FindViewById<WebView>(Resource.Id.fg_webview);
fg_webview.SetWebViewClient(new MyWebViewClient());
fg_webview.Settings.JavaScriptEnabled = true;
fg_webview.LoadUrl("http://developer.xamarin.com");
return view;
}
}
public class MyWebViewClient : WebViewClient
{
public override bool ShouldOverrideUrlLoading(WebView view, string url)
{
view.LoadUrl(url);
return true;
}
public override void OnPageStarted(WebView view, string url, Android.Graphics.Bitmap favicon)
{
base.OnPageStarted(view, url, favicon);
}
public override void OnPageFinished(WebView view, string url)
{
base.OnPageFinished(view, url);
}
public override void OnReceivedError(WebView view, ClientError errorCode, string description, string failingUrl)
{
base.OnReceivedError(view, errorCode, description, failingUrl);
}
}
Create a WebView layout :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/fg_webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
Replace your WebViewPage with the WebViewFragment :
//Fragment webViewPage = new WebViewPage().CreateFragment(mainActivity);
Fragment webViewPage = new WebViewFragment();

Menu is created, but disappears after navigations

I created menu in Xamarin Android application. It is showing on first page, but after PushAsync it disappears. Also after returning to first page it is not showing any more. Here is a code:
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity, IOnMenuItemClickListener
{
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
var toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
toolbar.InflateMenu(Resource.Menu.action_menu);
toolbar.SetOnMenuItemClickListener(this);
}
public bool OnMenuItemClick(IMenuItem item)
{
...
return true;
}
}
public App()
{
InitializeComponent();
MainPage = new NavigationPage(new MainPage());
}
public partial class MainPage : ContentPage
{
...
private async void OnSecondPageClicked()
{
await Navigation.PushAsync(new SecondPage());
}
}
File action_menu.xml:
<?xml version="1.0" encoding="utf-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/item1"
android:title="new_game"
android:showAsAction="always"/>
<item android:id="#+id/item2"
android:title="help"
android:showAsAction="always"/>
</menu>
You have a mixture of a Xamarin.Android app and a Xamarin.Forms app. This answer assumes you want a forms app. You should remove the following lines from OnCreate
var toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
toolbar.InflateMenu(Resource.Menu.action_menu);
toolbar.SetOnMenuItemClickListener(this);
And add these lines to the constructor of MainPage
this.ToolbarItems.Add(new ToolbarItem("New Game", "", () => { }));
this.ToolbarItems.Add(new ToolbarItem("Help", "", () => { }));
If that works you should move MainPage and App into your Cross Platform project.

How to implement Recycler View in MvvmCross using MvxRecyclerView

In MvvmCross, I have an android listview within an activity that works.
I read somewhere that changing the listView to a recyclerView is as simple as changing MvxListView to MvxRecyclerView in my layout .axml file. Trying that gives me the following runtime exception:
Android.Views.InflateException: Binary XML file line #1: Binary XML file line #1: Error inflating class Mvx.MvxRecyclerView
Is there anything that I have to do differently in the code behind or view model when using MvxRecyclerView? Below is my code.
Layout files:
Main.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Mvx.MvxRecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:id="#+id/words_listview"
local:MvxItemTemplate="#layout/words_listview_row"
local:MvxBind="ItemsSource Words" />
</LinearLayout>
words_listview_row.axml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:p1="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
p1:minWidth="25px"
p1:minHeight="25px"
p1:layout_width="match_parent"
p1:layout_height="match_parent"
p1:id="#+id/relativeLayout1"
p1:background="#FFFFFF">
<TextView
p1:text="Word name"
p1:layout_width="wrap_content"
p1:layout_height="wrap_content"
p1:id="#+id/headingTextView"
p1:width="325dp"
p1:textColor="#000000"
p1:layout_alignParentLeft="true"
p1:textSize="18sp"
p1:paddingLeft="20dp"
p1:paddingTop="15dp"
local:MvxBind="Text Name" />
<TextView
p1:text="Word meaning"
p1:layout_width="match_parent"
p1:layout_height="wrap_content"
p1:layout_below="#id/headingTextView"
p1:id="#+id/detailTextView"
p1:textColor="#8f949a"
p1:layout_alignParentLeft="true"
p1:textSize="16sp"
p1:paddingLeft="20dp"
p1:paddingRight="20dp"
p1:paddingBottom="15dp"
local:MvxBind="Text Meaning" />
</RelativeLayout>
WordViewModel.cs
using MvvmCross.Core.ViewModels;
using VocabBuilder.Core.Models;
using VocabBuilder.Core.Services.Interfaces;
namespace VocabBuilder.Core.ViewModels
{
public class WordViewModel : MvxViewModel
{
private IWordService _wordService;
public MvxObservableCollection<Word> Words { get; set; }
public WordViewModel(IWordService wordService)
{
Words = new MvxObservableCollection<Word>();
_wordService = wordService;
Words.ReplaceWith(_wordService.GetAllWords());
}
}
}
Codebehind/ WordView.cs
using Android.App;
using Android.OS;
using MvvmCross.Droid.Views;
using VocabBuilder.Core.ViewModels;
namespace VocabBuilder.UI.Droid.Views
{
[Activity(Label="Vocab Builder", MainLauncher=true)]
public class WordView : MvxActivity<WordViewModel>
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
}
}
}
Thanks.
EDIT:
Here is my Setup.cs file:
using Android.Content;
using MvvmCross.Core.ViewModels;
using MvvmCross.Droid.Platform;
namespace VocabBuilder.UI.Droid
{
public class Setup : MvxAndroidSetup
{
public Setup(Context applicationContext) : base(applicationContext)
{
}
protected override IMvxApplication CreateApp()
{
return new Core.App();
}
}
}
OK, I got RecyclerView to work easily after replacing MvxListView with MvxRecyclerView. Not sure what the particular problem with your implementation is but i will give you the tidbits of mine and you can try it all out.
My RecyclerView lives in a Fragment inheriting from MvxFragment - a bit different than yours but should not be the determining factor. Here are some Fragment snippets:
public class ListsFragment : MvxFragment<ListsViewModel>
{
...
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView(inflater, container, savedInstanceState);
var view = this.BindingInflate(Resource.Layout.fragment_listsfragment, null);
return view;
}
...
}
Here is the axml for fragment_listsfragment:
<?xml version="1.0" encoding="utf-8"?>
<MvvmCross.Droid.Support.V7.RecyclerView.MvxRecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:MvxItemTemplate="#layout/item_list"
app:MvxBind="ItemsSource Lists; ItemClick ShowListItemsCommand" />
I do see a bit of difference in your ViewModel implementation - mine is like this:
public class ListsViewModel : BaseViewModel
{
...
private readonly IListService _listService;
private ObservableCollection<Models.List> _lists;
public ObservableCollection<Models.List> Lists
{
get { return _lists; }
set
{
_lists = value;
RaisePropertyChanged(() => Lists);
}
}
public MvxCommand<Models.List> ShowListItemsCommand
{
get
{
return new MvxCommand<Models.List>(selectedList =>
{
ShowViewModel<ListItemsViewModel>
(new { listId = selectedList.Id });
});
}
}
...
public override async void Start()
{
base.Start();
await InitializeAsync();
}
protected override async Task InitializeAsync()
{
await _listService.InitializeAsync();
Lists = (await _listService.GetListsAsync())
.ToObservableCollection();
}
}
Hope this helps.

Resources