Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AppCompat v21 support? #9

Open
nitrico opened this issue Nov 11, 2014 · 9 comments
Open

AppCompat v21 support? #9

nitrico opened this issue Nov 11, 2014 · 9 comments

Comments

@nitrico
Copy link

nitrico commented Nov 11, 2014

Is there any way to get it working with the new appcompat support library? They introduced many changes on it and I don't find how to solve the problem of getActionBarIconView() always returning null.

@nitrico
Copy link
Author

nitrico commented Nov 11, 2014

Ok, I managed it by adding a toolbar to the activity layout and setting it as the ActionBar. This toolbar contains an ImageView and a TextView to replace the former ActionBar's icon and title.

@gfucka
Copy link

gfucka commented Nov 11, 2014

Hi, please can you explain me how you solve it??
I have to add a toolbar in activity_main.xml and then?? How i set it as ActionBar??

Thank you so much

@nitrico
Copy link
Author

nitrico commented Nov 11, 2014

Sure :) That's what I did:

In TransparentTheme style:

  • remove: <item name="homeAsUpIndicator">@drawable/abc_ic_ab_back_holo_dark</item>
  • add: <item name="windowActionBar">false</item>

Add this toolbar at the end of the activity_main.xml but inside the main root FrameLayout. Of course you can tweak it:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="56dp" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >
        <ImageView
            android:id="@+id/icon"
            android:layout_marginLeft="12dp"
            android:layout_marginTop="6dp"
            android:layout_marginBottom="6dp"
            android:layout_width="44dp"
            android:layout_height="44dp" />
        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title" />
    </LinearLayout>
</android.support.v7.widget.Toolbar>

Then, in the MainActivity.java:

  • Add this attributes:

    private Toolbar toolbar;
    private TextView title;
    private ImageView icon;
    
  • In onCreate method, after setContentView, add:

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    icon = (ImageView) findViewById(R.id.icon);
    title = (TextView) findViewById(R.id.title);
    
  • Replace getActionBarIconView method with this one:

    private ImageView getActionBarIconView() {
        return icon;
    }
    
  • Replace setTitleAlpha method with this one:

    private void setTitleAlpha(float alpha) {
        mAlphaForegroundColorSpan.setAlpha(alpha);
        mSpannableString.setSpan(mAlphaForegroundColorSpan, 0, mSpannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        title.setText(mSpannableString);
    }
    

And I think that's all.

@gfucka
Copy link

gfucka commented Nov 12, 2014

Hi, I do all but now I have NullPointerException on line 156 of MainActivity
float scaleY = 1.0F + interpolation * (mRect2.height() / mRect1.height() - 1.0F);

why??

Thanks again

@nitrico
Copy link
Author

nitrico commented Nov 12, 2014

Hmm... don't know why. I have made all the modifications again, thinking I could forget anything but it just works fine for me. You can post the trace of the exception to try to find the problem.

@nitrico nitrico closed this as completed Nov 12, 2014
@nitrico nitrico reopened this Nov 12, 2014
@jigar-sp
Copy link

thanks...

its perfectly working

@ed-george
Copy link

Thank you @nitrico, I can also confirm that this work around does rectify the issues with AppCompat v21

@cjgehinscott
Copy link

I can confirm that this fix does not work anymore...

07-07 09:56:15.633 3430-3430/com.bah.is.esd.zoneapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.bah.is.esd.zoneapp, PID: 3430
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bah.is.esd.zoneapp/com.bah.is.esd.zoneapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setAlpha(float)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setAlpha(float)' on a null object reference
at com.bah.is.esd.nineoldandroids.view.ViewHelper$Honeycomb.setAlpha(ViewHelper.java:185)
at com.bah.is.esd.nineoldandroids.view.ViewHelper.setAlpha(ViewHelper.java:19)
at com.bah.is.esd.zoneapp.MainActivity.layoutView(MainActivity.java:525)
at com.bah.is.esd.zoneapp.MainActivity.onCreate(MainActivity.java:135)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

@cjgehinscott
Copy link

Make your theme a child of Theme.AppCompat.Light.NoActionBar like so:
screen shot 2015-07-07 at 11 23 32 am

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants