Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #11053 from t895/top-app-bar
Android: Change top app bar color on scroll
  • Loading branch information
JosJuice committed Sep 14, 2022
2 parents 3420823 + eb6a548 commit 7eece76
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 8 deletions.
Expand Up @@ -8,6 +8,7 @@

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;

Expand Down Expand Up @@ -50,6 +51,9 @@ protected void onCreate(Bundle savedInstanceState)
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
Expand Down
Expand Up @@ -17,6 +17,8 @@
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;
Expand Down Expand Up @@ -85,9 +87,12 @@ protected void onCreate(Bundle savedInstanceState)

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
Expand Down
Expand Up @@ -12,6 +12,7 @@
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;

Expand Down Expand Up @@ -86,6 +87,9 @@ protected void onCreate(Bundle savedInstanceState)
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
Expand Down
Expand Up @@ -14,11 +14,12 @@
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;
Expand Down Expand Up @@ -88,10 +89,13 @@ protected void onCreate(Bundle savedInstanceState)
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
Expand Down
Expand Up @@ -6,6 +6,12 @@
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;

Expand Down Expand Up @@ -104,4 +110,37 @@ public static void setCorrectTheme(Activity activity)
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);
}
});
}
}
7 changes: 4 additions & 3 deletions Source/Android/app/src/main/res/layout/activity_cheats.xml
@@ -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">
Expand All @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion Source/Android/app/src/main/res/layout/activity_main.xml
Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion Source/Android/app/src/main/res/layout/activity_settings.xml
Expand Up @@ -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"
Expand Down

0 comments on commit 7eece76

Please sign in to comment.