Skip to content

Commit

Permalink
implementing a dark theme
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiasBielefeld committed Jun 17, 2018
1 parent f35d27c commit 92f67e9
Show file tree
Hide file tree
Showing 23 changed files with 182 additions and 105 deletions.
3 changes: 1 addition & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
android:label="@string/app_name"
android:supportsRtl="true"
android:name="de.tobiasbielefeld.searchbar.MainApplication"
android:theme="@style/AppThemeWithActionBar">
android:theme="@style/AppTheme">

<activity
android:name=".ui.MainActivity"
android:launchMode="singleTask"
android:theme="@style/MainTheme"
android:windowSoftInputMode="stateVisible">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class SharedData {

public static String PREF_RECORD_LIST_SIZE;
public static String PREF_RECORD_ENTRY;
public static String PREF_DARK_THEME;
public static String PREF_SEARCH_URL;
public static String PREF_ORIENTATION;
public static String PREF_STATUS_BAR;
Expand All @@ -47,6 +48,7 @@ public class SharedData {
public static String DEFAULT_SEARCH_URL;
public static String DEFAULT_ORIENTATION;
public static boolean DEFAULT_STATUS_BAR;
public static boolean DEFAULT_DARK_THEME;

public static Records records;

Expand All @@ -65,11 +67,13 @@ public static void reinitializeData(Context context){
PREF_ORIENTATION = res.getString(R.string.pref_key_orientation);
PREF_STATUS_BAR = res.getString(R.string.pref_key_hide_status_bar);
PREF_LANGUAGE = res.getString(R.string.pref_key_language);
PREF_DARK_THEME = res.getString(R.string.pref_key_dark_theme);

int n = res.getInteger(R.integer.default_search_engine_v2);
DEFAULT_SEARCH_URL = res.getStringArray(R.array.search_engine_uris)[n];
DEFAULT_ORIENTATION = res.getStringArray(R.array.pref_orientation_values)[0];
DEFAULT_STATUS_BAR = res.getBoolean(R.bool.default_status_bar);
DEFAULT_DARK_THEME = res.getBoolean(R.bool.default_dark_theme);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@

package de.tobiasbielefeld.searchbar.classes;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.WindowManager;

import de.tobiasbielefeld.searchbar.R;
import de.tobiasbielefeld.searchbar.helper.LocaleChanger;

import static de.tobiasbielefeld.searchbar.SharedData.*;
Expand All @@ -35,11 +37,13 @@
* was paused for a longer time and the references got lost.
*/

@SuppressLint("Registered")
public class CustomAppCompatActivity extends AppCompatActivity {

@Override protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
reinitializeData(this);
applyDarkTheme();
}

@Override
Expand Down Expand Up @@ -91,4 +95,10 @@ public void showOrHideStatusBar(Activity activity) {
else
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}

protected void applyDarkTheme(){
if (getSavedBoolean(PREF_DARK_THEME, DEFAULT_DARK_THEME)) {
setTheme(R.style.AppTheme_Dark);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import android.content.res.Resources;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,24 @@
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
Expand All @@ -59,16 +64,25 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Toolbar toolbar = findViewById(R.id.toolbar);
ActionBar actionBar = getSupportActionBar();

assert actionBar != null;
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setCustomView(R.layout.main_toolbar);

searchText = findViewById(R.id.editTextSearch);
clearButton = findViewById(R.id.imageButtonClear);

searchText.addTextChangedListener(this);
searchText.setOnEditorActionListener(this);

setSupportActionBar(toolbar);

records = new Records(this, (LinearLayout) findViewById(R.id.record_list_container));

if (getSavedBoolean(PREF_DARK_THEME, DEFAULT_DARK_THEME)) {
LinearLayout textSearch = findViewById(R.id.linearLayoutSearchText);
textSearch.setBackgroundResource(R.drawable.widget_background_dark);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import com.astuetz.PagerSlidingTabStrip;
Expand All @@ -43,13 +44,12 @@ public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activty_about);

ActionBar actionBar = getSupportActionBar();

if (actionBar!=null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}

PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
ViewPager pager = (ViewPager) findViewById(R.id.pager);
PagerSlidingTabStrip tabs = findViewById(R.id.tabs);
ViewPager pager = findViewById(R.id.pager);
TabsPagerAdapter adapter = new TabsPagerAdapter(getSupportFragmentManager(), this);

pager.setAdapter(adapter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,13 @@
import android.view.View;
import android.view.ViewGroup;

import de.tobiasbielefeld.searchbar.R;
import de.tobiasbielefeld.searchbar.helper.LocaleChanger;

import static de.tobiasbielefeld.searchbar.SharedData.DEFAULT_DARK_THEME;
import static de.tobiasbielefeld.searchbar.SharedData.PREF_DARK_THEME;
import static de.tobiasbielefeld.searchbar.SharedData.getSavedBoolean;

/**
* A {@link android.preference.PreferenceActivity} which implements and proxies the necessary calls
* to be used with AppCompat.
Expand All @@ -47,6 +52,7 @@ public abstract class AppCompatPreferenceActivity extends PreferenceActivity {
protected void onCreate(Bundle savedInstanceState) {
getDelegate().installViewFactory();
getDelegate().onCreate(savedInstanceState);
applyDarkTheme();
super.onCreate(savedInstanceState);
}

Expand Down Expand Up @@ -131,4 +137,10 @@ private AppCompatDelegate getDelegate() {
}
return mDelegate;
}

private void applyDarkTheme(){
if (getSavedBoolean(PREF_DARK_THEME, DEFAULT_DARK_THEME)) {
setTheme(R.style.AppTheme_Dark);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,6 @@ protected void onCreate(Bundle savedInstanceState) {
putSavedString(PREF_LANGUAGE,getSavedString(PREF_LANGUAGE,"default"));
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
//only item is the back arrow
finish();
return true;
}

@Override
public boolean onIsMultiPane() {
return isXLargeTablet(this);
Expand Down Expand Up @@ -111,6 +104,13 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin
setOrientation();
} else if (key.equals(PREF_LANGUAGE)) {
restartApplication();
} else if (key.equals(PREF_DARK_THEME)) {
//restart activity
final Intent intent = new Intent(getApplicationContext(), Settings.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
finish();
startActivity(intent);
}
}

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/drawable/icon_close.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
android:width="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path android:fillColor="#000" android:pathData="M19,6.41L17.59,5L12,10.59L6.41,5L5,6.41L10.59,12L5,17.59L6.41,19L12,13.41L17.59,19L19,17.59L13.41,12L19,6.41Z" />
<path android:fillColor="?attr/colorText" android:pathData="M19,6.41L17.59,5L12,10.59L6.41,5L5,6.41L10.59,12L5,17.59L6.41,19L12,13.41L17.59,19L19,17.59L13.41,12L19,6.41Z" />
</vector>
2 changes: 1 addition & 1 deletion app/src/main/res/drawable/icon_plus.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
android:width="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path android:fillColor="#000" android:pathData="M19,13H13V19H11V13H5V11H11V5H13V11H19V13Z" />
<path android:fillColor="?attr/colorText" android:pathData="M19,13H13V19H11V13H5V11H11V5H13V11H19V13Z" />
</vector>
6 changes: 6 additions & 0 deletions app/src/main/res/drawable/widget_background_dark.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/lightGrey" />
<corners android:radius="4dp" />
</shape>
50 changes: 1 addition & 49 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,57 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/mainBackground">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:orientation="vertical"
android:padding="5dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/widget_background"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp">


<EditText
android:id="@+id/editTextSearch"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/transparent"
android:focusable="true"
android:focusableInTouchMode="true"
android:hint="@string/search_hint"
android:imeOptions="actionSearch"
android:singleLine="true" />


<ImageView
android:id="@+id/imageButtonClear"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:background="?attr/selectableItemBackground"
android:onClick="onClick"
android:visibility="gone"
app:srcCompat="@drawable/icon_close" />

</LinearLayout>

</android.support.v7.widget.Toolbar>

<View
android:layout_width="match_parent"
android:layout_height="@dimen/shadow_height"
Expand All @@ -61,7 +13,7 @@
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar">
android:background="?android:attr/windowBackground">

<LinearLayout
android:id="@+id/record_list_container"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/changelog_card_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
android:layout_width="match_parent"

android:layout_height="wrap_content"
card_view:cardBackgroundColor="@color/white"
card_view:cardBackgroundColor="?android:attr/colorBackground"
card_view:cardCornerRadius="2dp"
card_view:cardUseCompatPadding="true">

Expand Down
Loading

0 comments on commit 92f67e9

Please sign in to comment.