Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 55c43cbe authored by David Liu's avatar David Liu Committed by Android (Google) Code Review
Browse files

Merge "Added button in CollapsingToolbar" into main

parents 5ffd2215 7e207744
Loading
Loading
Loading
Loading
+19 −1
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@
            android:layout_height="@dimen/settingslib_toolbar_layout_height"
            app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
            app:toolbarId="@id/action_bar"
            app:maxLines="2"
            style="@style/SettingsLibCollapsingToolbarLayoutStyle.Expressive">

            <Toolbar
@@ -44,7 +45,24 @@
                android:layout_marginStart="@dimen/settingslib_expressive_space_extrasmall4"
                android:theme="?android:attr/actionBarTheme"
                android:transitionName="shared_element_view"
                app:layout_collapseMode="pin"/>
                app:layout_collapseMode="pin">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingStart="@dimen/settingslib_expressive_space_extrasmall6"
                    android:layout_marginEnd="@dimen/settingslib_expressive_space_small4"
                    android:layout_gravity="end">

                    <com.google.android.material.button.MaterialButton
                        android:id="@+id/action_button"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        style="@style/SettingsLibButtonStyle.Expressive.Filled"
                        android:visibility="gone"/>
                </LinearLayout>

            </Toolbar>

        </com.google.android.material.appbar.CollapsingToolbarLayout>
    </com.google.android.material.appbar.AppBarLayout>
+24 −0
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.Toolbar;

import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

@@ -132,6 +133,29 @@ public class CollapsingToolbarAppCompatActivity extends AppCompatActivity {
        setTitle(getText(titleId));
    }

    /**
     * Show/Hide the action button on the Toolbar.
     * @param enabled true to show the button, otherwise it's hidden.
     */
    public void setActionButtonEnabled(boolean enabled) {
        getToolbarDelegate().setActionButtonEnabled(enabled);
    }

    /** Set the icon to the action button */
    public void setActionButtonIcon(@DrawableRes int drawableRes) {
        getToolbarDelegate().setActionButtonIcon(this, drawableRes);
    }

    /** Set the text to the action button */
    public void setActionButtonText(@Nullable CharSequence text) {
        getToolbarDelegate().setActionButtonText(text);
    }

    /** Set the OnClick listener to the action button */
    public void setActionButtonListener(@Nullable View.OnClickListener listener) {
        getToolbarDelegate().setActionButtonOnClickListener(listener);
    }

    @Override
    public boolean onSupportNavigateUp() {
        if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
+24 −0
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.Toolbar;

import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;

@@ -124,6 +125,29 @@ public class CollapsingToolbarBaseActivity extends FragmentActivity {
        setTitle(getText(titleId));
    }

    /**
     * Show/Hide the action button on the Toolbar.
     * @param enabled true to show the button, otherwise it's hidden.
     */
    public void setActionButtonEnabled(boolean enabled) {
        getToolbarDelegate().setActionButtonEnabled(enabled);
    }

    /** Set the icon to the action button */
    public void setActionButtonIcon(@DrawableRes int drawableRes) {
        getToolbarDelegate().setActionButtonIcon(this, drawableRes);
    }

    /** Set the text to the action button */
    public void setActionButtonText(@Nullable CharSequence text) {
        getToolbarDelegate().setActionButtonText(text);
    }

    /** Set the OnClick listener to the action button */
    public void setActionButtonListener(@Nullable View.OnClickListener listener) {
        getToolbarDelegate().setActionButtonOnClickListener(listener);
    }

    @Override
    public boolean onNavigateUp() {
        if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
+41 −0
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.Toolbar;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
@@ -42,6 +43,7 @@ import com.android.settingslib.widget.SettingsThemeHelper;

import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.google.android.material.button.MaterialButton;

/**
 * A delegate that allows to use the collapsing toolbar layout in hosts that doesn't want/need to
@@ -80,6 +82,8 @@ public class CollapsingToolbarDelegate {
    private AppBarLayout mAppBarLayout;
    @NonNull
    private Toolbar mToolbar;
    @Nullable
    private MaterialButton mActionButton;
    @NonNull
    private FrameLayout mContentFrameLayout;
    @NonNull
@@ -154,6 +158,7 @@ public class CollapsingToolbarDelegate {
        }
        autoSetCollapsingToolbarLayoutScrolling();
        mContentFrameLayout = view.findViewById(R.id.content_frame);
        mActionButton = view.findViewById(R.id.action_button);
        if (activity instanceof AppCompatActivity) {
            Log.d(TAG, "onCreateView: from AppCompatActivity and sub-class.");
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
@@ -215,6 +220,42 @@ public class CollapsingToolbarDelegate {
        }
    }

    /**
     * Show/Hide the action button on the Toolbar.
     * @param enabled true to show the button, otherwise it's hidden.
     */
    public void setActionButtonEnabled(boolean enabled) {
        if (mActionButton == null) {
            return;
        }
        int visibility = enabled ? View.VISIBLE : View.GONE;
        mActionButton.setVisibility(visibility);
    }

    /** Set the icon to the action button */
    public void setActionButtonIcon(@NonNull Context context, @DrawableRes int drawableRes) {
        if (mActionButton == null) {
            return;
        }
        mActionButton.setIcon(context.getResources().getDrawable(drawableRes, context.getTheme()));
    }

    /** Set the text to the action button */
    public void setActionButtonText(@Nullable CharSequence text) {
        if (mActionButton == null) {
            return;
        }
        mActionButton.setText(text);
    }

    /** Set the OnClick listener to the action button */
    public void setActionButtonOnClickListener(@Nullable View.OnClickListener listener) {
        if (mActionButton == null) {
            return;
        }
        mActionButton.setOnClickListener(listener);
    }

    /** Return an instance of CoordinatorLayout. */
    @Nullable
    public CoordinatorLayout getCoordinatorLayout() {