Skip to content
Snippets Groups Projects
Commit fbbc9a3d authored by fheyming's avatar fheyming
Browse files

Added dark mode

parent 74943f7f
No related branches found
No related tags found
1 merge request!1Mal aufn Master megren
package com.example.mampfmobil.ui; package com.example.mampfmobil.ui;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction; import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.ViewModelProvider; import androidx.lifecycle.ViewModelProvider;
...@@ -7,11 +8,13 @@ import androidx.lifecycle.ViewModelProvider; ...@@ -7,11 +8,13 @@ import androidx.lifecycle.ViewModelProvider;
import android.content.Intent; import android.content.Intent;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.content.res.Resources; import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
...@@ -43,6 +46,26 @@ public class DecideFragment extends Fragment { ...@@ -43,6 +46,26 @@ public class DecideFragment extends Fragment {
ImageButton deButton = rootView.findViewById(R.id.imageView); ImageButton deButton = rootView.findViewById(R.id.imageView);
ImageButton enButton = rootView.findViewById(R.id.imageView2); ImageButton enButton = rootView.findViewById(R.id.imageView2);
ImageButton darkMode = rootView.findViewById(R.id.imageButton);
darkMode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Den Dark Mode programmgesteuert aktivieren
setDarkMode(true);
}
});
ImageButton lightMode = rootView.findViewById(R.id.imageButton2);
lightMode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Den Dark Mode programmgesteuert aktivieren
setDarkMode(false);
}
});
deButton.setOnClickListener(new View.OnClickListener() { deButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
...@@ -112,12 +135,38 @@ public class DecideFragment extends Fragment { ...@@ -112,12 +135,38 @@ public class DecideFragment extends Fragment {
configuration.setLocale(locale); configuration.setLocale(locale);
resources.updateConfiguration(configuration, resources.getDisplayMetrics()); resources.updateConfiguration(configuration, resources.getDisplayMetrics());
// Hier müssen Sie den Fragments-Wechsel aktualisieren, um sicherzustellen, recreateActivity(); // Aktivität neu starten, um die Änderungen anzuwenden
// dass die Ansichten und Texte entsprechend der geänderten Sprache aktualisiert werden }
FragmentManager fragmentManager = requireActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); private void setDarkMode(boolean enabled) {
fragmentTransaction.replace(R.id.container, new DecideFragment());
fragmentTransaction.commit(); if (enabled) {
Log.d("myTag", "YES");
// Dark Mode aktivieren
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
} else {
Log.d("myTag", "NO ");
// Dark Mode deaktivieren
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
recreateActivity(); // Aktivität neu starten, um den Dark Mode anzuwenden
}
private void recreateActivity() {
// Aktivität neu starten
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
requireActivity().recreate();
} else {
Intent intent = requireActivity().getIntent();
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
requireActivity().finish();
requireActivity().overridePendingTransition(0, 0);
startActivity(intent);
requireActivity().overridePendingTransition(0, 0);
}
} }
} }
\ No newline at end of file
app/src/main/res/drawable/darkmode.png

2.85 KiB

app/src/main/res/drawable/lightmode.png

3.85 KiB

...@@ -3,8 +3,7 @@ ...@@ -3,8 +3,7 @@
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container" android:id="@+id/container"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent">
android:paddingTop="?attr/actionBarSize">
<com.google.android.material.bottomnavigation.BottomNavigationView <com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view" android:id="@+id/nav_view"
......
...@@ -68,5 +68,26 @@ ...@@ -68,5 +68,26 @@
app:layout_constraintStart_toEndOf="@+id/DelivererButton" app:layout_constraintStart_toEndOf="@+id/DelivererButton"
app:layout_constraintTop_toBottomOf="@+id/DelivererButton" /> app:layout_constraintTop_toBottomOf="@+id/DelivererButton" />
<ImageButton
android:id="@+id/imageButton"
android:layout_width="87dp"
android:layout_height="83dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:scaleType="fitCenter"
android:src="@drawable/darkmode"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="87dp"
android:layout_height="83dp"
android:layout_marginTop="16dp"
android:scaleType="fitCenter"
android:src="@drawable/lightmode"
app:layout_constraintEnd_toStartOf="@+id/imageButton"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout> </FrameLayout>
\ No newline at end of file
...@@ -7,4 +7,5 @@ ...@@ -7,4 +7,5 @@
<color name="teal_700">#FF018786</color> <color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color> <color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color> <color name="white">#FFFFFFFF</color>
<color name="dark_grey">#5A5A5A</color>
</resources> </resources>
\ No newline at end of file
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
<item name="colorPrimary">@color/purple_500</item> <item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item> <item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item> <item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. --> <!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item> <item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item> <item name="colorSecondaryVariant">@color/teal_700</item>
...@@ -12,5 +13,6 @@ ...@@ -12,5 +13,6 @@
<!-- Status bar color. --> <!-- Status bar color. -->
<item name="android:statusBarColor">?attr/colorPrimaryVariant</item> <item name="android:statusBarColor">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. --> <!-- Customize your theme here. -->
</style> </style>
</resources> </resources>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment