Android: Change top app bar color on scroll

This commit is contained in:
Charles Lombardo 2022-09-11 12:57:56 -04:00
parent 3420823002
commit eb6a548735
8 changed files with 67 additions and 8 deletions

View File

@ -8,6 +8,7 @@ import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.google.android.material.appbar.MaterialToolbar;
@ -50,6 +51,9 @@ public class ConvertActivity extends AppCompatActivity
ctb.setTitle(getString(R.string.convert_convert));
setSupportActionBar(tb);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
AppBarLayout appBarLayout = findViewById(R.id.appbar_convert);
ThemeHelper.enableScrollTint(tb, appBarLayout, this);
}
@Override

View File

@ -17,6 +17,8 @@ import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.MaterialToolbar;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import org.dolphinemu.dolphinemu.R;
@ -85,9 +87,12 @@ public class UserDataActivity extends AppCompatActivity
buttonExportUserData.setOnClickListener(view -> exportUserData());
Toolbar tb = findViewById(R.id.toolbar_user_data);
MaterialToolbar tb = findViewById(R.id.toolbar_user_data);
setSupportActionBar(tb);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
AppBarLayout appBarLayout = findViewById(R.id.appbar_user_data);
ThemeHelper.enableScrollTint(tb, appBarLayout, this);
}
@Override

View File

@ -12,6 +12,7 @@ import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.google.android.material.appbar.MaterialToolbar;
@ -86,6 +87,9 @@ public class RiivolutionBootActivity extends AppCompatActivity
ctb.setTitle(getString(R.string.riivolution_riivolution));
setSupportActionBar(tb);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
AppBarLayout appBarLayout = findViewById(R.id.appbar_riivolution);
ThemeHelper.enableScrollTint(tb, appBarLayout, this);
}
@Override

View File

@ -14,11 +14,12 @@ import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.ViewModelProvider;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.google.android.material.appbar.MaterialToolbar;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import org.dolphinemu.dolphinemu.NativeLibrary;
@ -88,10 +89,13 @@ public final class SettingsActivity extends AppCompatActivity implements Setting
mPresenter = new SettingsActivityPresenter(this, getSettings());
mPresenter.onCreate(savedInstanceState, menuTag, gameID, revision, isWii, this);
Toolbar tb = findViewById(R.id.toolbar_settings);
MaterialToolbar tb = findViewById(R.id.toolbar_settings);
mToolbarLayout = findViewById(R.id.toolbar_settings_layout);
setSupportActionBar(tb);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
AppBarLayout appBarLayout = findViewById(R.id.appbar_settings);
ThemeHelper.enableScrollTint(tb, appBarLayout, this);
}
@Override

View File

@ -6,6 +6,12 @@ import android.content.res.Configuration;
import android.os.Build;
import android.preference.PreferenceManager;
import androidx.annotation.ColorInt;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.MaterialToolbar;
import com.google.android.material.color.MaterialColors;
import org.dolphinemu.dolphinemu.R;
import org.dolphinemu.dolphinemu.ui.main.ThemeProvider;
@ -104,4 +110,37 @@ public class ThemeHelper
activity.recreate();
}
}
private static void setStatusBarColor(@ColorInt int color, Activity activity)
{
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
{
activity.getWindow()
.setStatusBarColor(activity.getResources().getColor(android.R.color.black));
}
else
{
activity.getWindow().setStatusBarColor(color);
}
}
public static void enableScrollTint(MaterialToolbar toolbar, AppBarLayout appBarLayout,
Activity activity)
{
appBarLayout.addOnOffsetChangedListener((layout, verticalOffset) ->
{
if (-verticalOffset >= (layout.getTotalScrollRange() / 2))
{
@ColorInt int color = MaterialColors.getColor(toolbar, R.attr.colorSurfaceVariant);
toolbar.setBackgroundColor(color);
setStatusBarColor(color, activity);
}
else
{
@ColorInt int color = MaterialColors.getColor(toolbar, R.attr.colorSurface);
toolbar.setBackgroundColor(color);
setStatusBarColor(color, activity);
}
});
}
}

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinator_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
@ -9,7 +9,8 @@
android:id="@+id/appbar_cheats"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
android:layout_alignParentTop="true"
app:elevation="0dp">
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar_cheats"

View File

@ -9,7 +9,8 @@
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar_main"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_height="wrap_content"
app:elevation="0dp">
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar_main"

View File

@ -10,7 +10,8 @@
android:id="@+id/appbar_settings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
android:layout_alignParentTop="true"
app:elevation="0dp">
<com.google.android.material.appbar.CollapsingToolbarLayout
style="?attr/collapsingToolbarLayoutMediumStyle"