I'm new to Xamarin Android. I've installed the AppCompatv7 Support Library from the GetComponents option.
But whenever I try and do anything with it, I get no intellisense, like it isn't actually added to the project. Like below:
When I look more into the assembly it comes up with an option like below, saying it might not be installed. But as you can see from the picture, it is installed under my references.
If I click Add Package in the below picture, nothing happens.
When I compile the code, it can't find functions in the ActionBarActivity base class, so I'm guessing it's not adding it properly into my project.
Anyone know why this is happening? Cheers
When I compile the code, it can't find functions in the ActionBarActivity base class, so I'm guessing it's not adding it properly into my project.
ActionBarActivity is obsolete.
To use Android.Support.V7.Widget.Toolbar, after installing the Xamarin.Android.Support.v7.AppCompat package, you can simply inherit your MainActivity from AppCompatActivity instead of ActionBarActivity.
Then for example my toolbar is like this:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:layout_width="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/toolbartitile"
android:text="Kodej" />
</android.support.v7.widget.Toolbar>
Include this toolbar in Main layout like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include android:id="#+id/toolbar"
layout="#layout/mytoolbar" />
</LinearLayout>
And finally in your MainActivity:
public class MainActivity : AppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
var toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
SetSupportActionBar(toolbar);
}
}
You just need to reference Android.Support.V7.App in your code:
using Android.Support.V7.App;
All references in my demo:
If you've installed the libs correctly and there is no no intellisense, you can try to rebuild your app, close and reopen your VS.
Install it using the NuGet Command Console :
Install-Package Xamarin.Android.Support.v7.AppCompat -Pre
Related
I gave an id attribute:
android: id="#+id/animation_layout"
in the default layout (numbered as 1) in the activity_main.xml
But when I tried to call it in the MainActivity.java by its id:
ConstraintLayout constraintLayout = findViewById(R.id.animation_layout);
It won't find it (numbered as 2)
What am I missing?
also attached the code:
<android.support.constraint.ConstraintLayout 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/animation_layout"
android:background="#drawable/gradients_list"
tools:context=".MainActivity">
and the Java code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Gradient animation
ConstraintLayout constraintLayout = findViewById(R.id.animation_layout);
AnimationDrawable animationDrawable = (AnimationDrawable) constraintLayout.getBackground();
animationDrawable.setEnterFadeDuration(2000);
animationDrawable.setEnterFadeDuration(4000);
animationDrawable.start();
Thank you for helping :)
It happens sometime since new android update! that you even provide ids in xml but still the java api of android does not fetch them! so you need to do this step!
1: clean the build removes the old built cache! and this should solve the problem
2: if that does not make your id read bale in java you can use the Rebuild option from the same menu!
3: if that does not work either you can go with the FILE-> invalidate cache and restart
I am designing an cross-platform app for iOS aswell Android with a shared logic using MVVMCross (5.1.1).
Throughout my app I have a fixed toolbar at the top displaying the current view's title aswell a button. Below the bar the interface is changing from view to view
The Android part:
On Android I created a reuseable layout which I embed in my current layout using include.
In my portable project I have a BaseViewModel which has the properties the reuseable toolbar layout binds to. Every other ViewModel derives from this base class. This way I can have all bindable properties of a displayed screen in one ViewModel without the need of nesting but see for yourself:
activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar_login" />
<RelativeLayout
android:id="#+id/parentLoginLayout"
android:clickable="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/toolbar">
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
app:MvxBind="Text Pin"
/>
</RelativeLayout>
</RelativeLayout>
toolbar_login.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar_login"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="0dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:minWidth="25px"
android:minHeight="25px">
<TextView
app:MvxBind="Click ToolbarMenuCommand"
/>
<!-- some other -->
</android.support.v7.widget.Toolbar>
ViewModels.cs
using System.Threading.Tasks;
using Mobile.Helpers;
using ViewModels.Base;
using MvvmCross.Core.Navigation;
using MvvmCross.Core.ViewModels;
using Plugin.MessageBox;
namespace Mobile.ViewModels
{
public abstract class BaseViewModel : MvxViewModel
{
protected void NavigateToMainView()
{
NavigateTo<MainViewModel>();
}
private readonly IMvxNavigationService _navigationService;
protected BaseViewModel(IMvxNavigationService navigationService)
{
_navigationService = navigationService;
}
public IMvxCommand ToolbarMenuCommand => new MvxCommand(OnMenuButtonClick);
protected abstract void OnMenuButtonClick();
}
public class LoginViewModel : BaseViewModel
{
private bool _menuVisibility;
private string _pin;
public LoginViewModel(IMvxNavigationService navigationService) : base(navigationService)
{
}
public bool MenuVisibility
{
get => _menuVisibility;
set => SetProperty(ref _menuVisibility, value);
}
public string Pin
{
get => _pin;
set => SetProperty(ref _pin, value);
}
protected override void OnMenuButtonClick()
{
MenuVisibility = !MenuVisibility;
}
}
}
The iOS part:
I am not entirely sure how to realise above behavior on iOS. I hope someone has a good idea or a good example project for me which I can take a look at. In general it is no problem to refactorise the ViewModels incase my idea is just not possible at iOS.
A few facts about the iOS project:
I am not using storyboards but single .xib's being independent
from each other
In my .xib's files I use autolayout constraints for positioning and
sizing entirely
A few ideas I already had (cant test them right now):
1. idea:
Create a base .xib with the above bar, the constraints aswell the
outlets
Create each new xib Design based on the previously created file
This would mean I need to adjust every view incase I decide to change something about the toolbar but so far I found no other way to embed a .xib in another .xib without having two different ViewControllers. Also I read that inheritance cause problems with outlets.
2. idea
Each .xib has an empty view at the top which acts as a container for
the toolbar
Have a Base ViewController which constructs the toolbar from code and
adds it as a child to the container view, and binds the properties
from the BaseViewModel
In a previous iOS project I noticed that adding views to the layout can cause problems with autolayout. Probably also a not that good solution?
3. idea
Create a xib with the toolbar and a container below and use it as a master page which would probably mean having a MasterViewModel with the toolbar properties and a nested ChildViewModel.
This is probably the way to go but I have to admit that I have no clue what is the best way to approach it (stil pretty new to iOS and MVVMCross).
Does someone have a few useful hints for me? Thanks a lot!
From what I understood I think you should try to use ScrollView for iOS part and try to imitate the ViewPager's behavior from Android, an example.
The app is running on Android 6 or below. But when running on Android 7 (Nougat) it throws runtime exception.
System.InvalidCastException: Unable to convert instance of type 'Android.Widget.LinearLayout' to type 'Android.Support.V7.Widget.Toolbar'.
The error is thrown in Xamarin (Visual studio 2017) at below code:
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
ToolbarResource = Resource.Layout.toolbar;
TabLayoutResource = Resource.Layout.tabs;
base.OnCreate(bundle); //Runtime error here
.... Other code ...
}
}
I've taken latest updates for All Nuget packages but not able to identify the issue. For testing I'm using Moto G4+ (Android 7.0).
Can anybody help please?
Edit 1:
Layout of toolbar is as : (Under resources > layout > toolbar.axml). This was already there when App version with Android 6 released. But Now visited app back for Android 7 issues.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
I solved this by just cleaning the entire solution and closing the emulator. Then deploy again and it should work.
I tried SeƱor Hernandez's answer without much success, then realized I had accidentally deleted Resources/Resource.designer.cs from my Android project. I had meant to delete it from disk only, expecting it to get regenerated on rebuild. It did get regenerated, but that did not add it back to the project. Doing so (right-click, include in project) solved the issue on my part.
It got solved by downloading android_m2repository_r29.zip repository from https://developer.xamarin.com/guides/android/troubleshooting/resolving-library-installation-errors/ with instruction given in same.
I also tested the issue by creating a new fresh solution and got some style "Resource not found error". That was also fixed and then the original problem also got fixed.
You get this kind of error when trying to assign a control (a UI element in your xml) on a variable that is not of the same type. I get this error frequently when I make a mess and try to assign an EditText to a TextView variable or vice versa, i. e
EditText myText = FindViewById<TextView>(Resource.Id.myText);
or
TextView myText = FindViewById<EditText>(Resource.Id.myText);
I'm pretty sure that in your case you are trying to assign an Android.Widget.LinearLayout to a variable of type Android.Support.V7.Widget.Toolbar.
I think in your case one of this variables have been defined as LinearLayout in your xml:
Resource.Layout.toolbar
Resource.Layout.tabs
Meanwhile, one of this variables have beeen defined as Toolbar in your C# code, so te cast is not possible:
ToolbarResource
TabLayoutResource
If the problem persist, you can also try to check the type Android.Support.V7.Widget.Toolbar, instead it could be Android.Widget.Toolbar in your xml or code.
Recently i have successfully implemented the ActionBarSherlock Demos sample in eclipse. But there is something that i am not able to understand it that how this library automatically creates the header with one icon and a text "ActionBarSharelock Demos" in tab_navigation.xml by using merely a LinearLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="20dip">
</LinearLayout>
This library is new for me and i am so anxious to know, how this library creates it?
I created a new android project on target api 15 and attach the ActionBarSherlock library to it. then i found that there some of the files are missing from my new android project when i compared it to demos sample project of android. Those files are listed below.
1.pom.xml
2.bin/classes.dex
3.bin/jarlist.cache
4.bin/resources.ap_
Moreover i got a new error in my activity file on eclipse "R cannot be resolved to a variable."
If you know anything about my problem then please share your views.
To use the simplest example:
public class Simple extends SherlockActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(SampleList.THEME); //Used for theme switching in samples
super.onCreate(savedInstanceState);
setContentView(R.layout.text);
((TextView)findViewById(R.id.text)).setText(R.string.simple_content);
}
}
All you have to do is let your activity inherit one of the Sherlock activites (e.g., SherlockActivity, SherlockFragmentActivity).
You can program the action bar by using the getSupportActionBar() method of the SherlockActivity, e.g. to set the title: getSupportActionBar().setTitle( R.string.my_title );
I'm sure this is something dead simple as it's my first android app with code (the hello world example I did was just assigning a value to a string in XML). My problem is when trying to get the reference for my button in my variable then the id as in R.id is not defined?
The compiler error is in a comment in the code below where it occurs:
package com.geeksonhugs.simplecalc;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
public class MainActivity extends Activity
{
private Button AddButton;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AddButton = (Button)findViewById(R.id.btnAdd);
//"Unknown member 'id' of 'com.geeksonhugs.simplecalc.R'"
//AddButton.setOnClickListener(this);
}
}
XML layout file:
<?xml version='1.0' encoding='utf-8' ?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LayoutRoot"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/txtFirstNum"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="#string/strFirstNum" />
<EditText
android:id="#+id/edtFirstNum"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="" />
<TextView
android:id="#+id/txtSecondNum"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="#string/strSecondNum" />
<EditText
android:id="#+id/edtSecondNum"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="" />
<Button
android:id="#+id/btnAdd"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="#string/strAdd"
android:gravity="center" />
<TextView
android:id="#+id/txtResult"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="" />
</LinearLayout>
I resolved the issue by deleting the bin folder and recompiling using the AIDE App.
The R class is code-generated for you by the Android build tools when you build your project. This means there are four possibilities:
You have not tried building the project yet, in which case, try that.
Eclipse doesn't think it needs to build the project for some reason -- try Project > Clean from the Eclipse menu (only relevant if you are using Eclipse, of course).
There is some bug in your manifest or one of your resources that is preventing R from being built. There should be Eclipse error indicators for this.
You do not have an android:id attribute in your layout, and therefore there is no R.id available.
Please make sure the xml file you pasted is called main.xml and under layout folder.
And try to generate the R file again.
That may help.
For those using AIDE having issues referencing Id, try deleting the "gen folder" located in the bin folder and recompile. The problem is the R.java class did not create a constructor called Id. So deleting the gen folder containing the R.java class and recompiling solves this issue.
You must add id to a component in the XML file. Like this:
android:id="#+id/buton1"
Now can you click mainActivity.java and write code "R.id." you a see hint to write "buton1" and no error of "id"