Android-ParallaxHeaderViewPager icon indicating copy to clipboard operation
Android-ParallaxHeaderViewPager copied to clipboard

AppCompat v21 support?

Open nitrico opened this issue 10 years ago • 9 comments

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 avatar Nov 11 '14 03:11 nitrico

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.

nitrico avatar Nov 11 '14 05:11 nitrico

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

gfucka avatar Nov 11 '14 14:11 gfucka

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.

nitrico avatar Nov 11 '14 17:11 nitrico

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

gfucka avatar Nov 12 '14 09:11 gfucka

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 avatar Nov 12 '14 15:11 nitrico

thanks...

its perfectly working

jigar-sp avatar Nov 27 '14 13:11 jigar-sp

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

ed-george avatar Jan 09 '15 10:01 ed-george

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 avatar Jul 07 '15 14:07 cjgehinscott

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

cjgehinscott avatar Jul 07 '15 15:07 cjgehinscott