Reputation: 1299
I've noticed this is a very common issue, but none of the solutions have worked for me. This is the main thread that everyone seems to reference:
But I've tried an no luck. Am I missing something??
Here is my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto">
<uses-sdk />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
</manifest>
Here is my styles.xaml:
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
</style>
<style name="AppTheme" parent="AppBaseTheme">
<!-- Both of these are needed -->
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
Here is my base activity:
public abstract class ActivityBase : MvxCachingFragmentActivityCompat
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Make this activity fullscreen
this.Window.AddFlags(WindowManagerFlags.Fullscreen);
}
.
.
.
}
Here is my sample activity:
[Activity(Label = "Add Child", Theme = "@style/AppTheme")]
public class AddChildPage : ActivityBase, DatePickerDialog.IOnDateSetListener
{
.
.
.
}
I'm basically setting the theme directly on the activity I'm testing with. I'm not setting the theme in the manifest nor the base activity.
Am I doing something wrong?
UPDATE:
After following the steps, I'm happy to say the error is gone, but now I get a new error which I've never had before introducing the AppCompat base activity:
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.ViewGroup.getPaddingLeft()' on a null object reference
I'll investigate this a bit more, if I find no answer, I'll mark Daniele's answer as valid since it did fix my previous error.
Upvotes: 1
Views: 1637
Reputation: 2734
I was facing the same problem and here is how I solved.
In the manifest you should add:
<application android:label="yourApp" android:theme="@style/AppBaseTheme"></application>
Your base activity should extend
: AppCompatActivity
and you must remove this part
// Make this activity fullscreen this.Window.AddFlags(WindowManagerFlags.Fullscreen);
Also from all your activities remove
Theme = "@style/AppTheme"
(so that they will be like this)
[Activity(Label = "Add Child")]
Now, your styles.xaml should be:
<resources>
<style name="AppBaseTheme" parent="AppBaseTheme.Base">
</style>
<!-- Base theme applied no matter what API -->
<style name="AppBaseTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<!--If you are using revision 22.1 please use just windowNoTitle. Without android:-->
<item name="windowNoTitle">true</item>
<!--We will be using the toolbar so no need to show ActionBar-->
<item name="windowActionBar">false</item>
<!-- colorPrimary is used for the default action bar background -->
<item name="colorPrimary">#2196F3</item>
<!-- colorPrimaryDark is used for the status bar -->
<item name="colorPrimaryDark">#1976D2</item>
<!-- colorAccent is used as the default value for colorControlActivated
which is used to tint widgets -->
<item name="colorAccent">#40FF81</item>
<!-- You can also set colorControlNormal, colorControlActivated
colorControlHighlight and colorSwitchThumbNormal. -->
</style>
</resources>
But you also need (it's required or Appcompat will not work) to create a new folder under Resources called values-v21 that will be seen only from device with Android 5+ and inside you will have another styles.xaml with this content:
<resources>
<style name="AppTheme" parent="AppTheme.Base">
<item name="android:windowContentTransitions">true</item>
<item name="android:windowAllowEnterTransitionOverlap">true</item>
<item name="android:windowAllowReturnTransitionOverlap">true</item>
<item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
<item name="android:windowSharedElementExitTransition">@android:transition/move</item>
</style>
</resources>
Note that the style name and the parent needs to be exactly the same of the other style file and yet the same declared in the manifest.
Now you need to directly reference the new Toolbar widget from the Support Library. If you haven’t already create a new Android Layout file under Resources/Layout/toolbar.axml do it. The file will look like this:
<?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="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
With the Toolbar setup we you now integrate it into your layouts by using the include node to place the Toolbar. I know, there was no need to include the action bar inside each layout before, but there's nothing to do about it: App Compat requires you include the toolbar in the activity layout. this is an example:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/main_content"
android:layout_below="@id/toolbar">
</LinearLayout>
</RelativeLayout>
The important part you shouldn't change (or do it accordingly with your id and layout toolbar.xaml) is this one:
<include android:id="@+id/toolbar" layout="@layout/toolbar" />
We are almost done: to all your activities add:
using Toolbar = Android.Support.V7.Widget.Toolbar;
and your OnCreate method should start like this
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.main);
var toolbar = FindViewById<Toolbar> (Resource.Id.toolbar);
//Toolbar will now take on default actionbar characteristics
SetSupportActionBar (toolbar);
SupportActionBar.Title = "Hello from stackoverflow";
//..
}
Only after setting the ContentView you can set the toolbar.
These steps are working for me and helped me to update to AppCompatv7 r22.2 As far as I know AppCompatv7 r22.1 has some little bugs. From Sept. 29 there is also a new version of AppCompat v7 (r.23)
Upvotes: 1