From 80e9d769e4d54d759e274109aa06f75bebee56a3 Mon Sep 17 00:00:00 2001 From: TebbeUbben Date: Mon, 4 Jun 2018 21:34:21 +0200 Subject: [PATCH] Compact version of toolbar --- .../nightscout/androidaps/MainActivity.java | 77 +++++++++++-------- .../androidaps/tabs/NonConsumingToolbar.java | 28 ------- app/src/main/res/layout/activity_main.xml | 55 ++++++------- app/src/main/res/values/dimens.xml | 1 + 4 files changed, 69 insertions(+), 92 deletions(-) delete mode 100644 app/src/main/java/info/nightscout/androidaps/tabs/NonConsumingToolbar.java diff --git a/app/src/main/java/info/nightscout/androidaps/MainActivity.java b/app/src/main/java/info/nightscout/androidaps/MainActivity.java index b19fc80cb1..40dd2f64ab 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/MainActivity.java @@ -1,40 +1,35 @@ package info.nightscout.androidaps; -import android.app.Activity; import android.content.Context; -import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageManager; import android.graphics.Rect; import android.os.Bundle; import android.os.PersistableBundle; import android.os.PowerManager; -import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.design.widget.NavigationView; import android.support.design.widget.TabLayout; import android.support.v4.app.ActivityCompat; -import android.support.v4.app.Fragment; import android.support.v4.view.ViewPager; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; -import android.support.v7.widget.PopupMenu; +import android.support.v7.widget.Toolbar; import android.text.SpannableString; import android.text.method.LinkMovementMethod; import android.text.util.Linkify; import android.util.Log; +import android.util.TypedValue; import android.view.Menu; -import android.view.MenuInflater; import android.view.MenuItem; import android.view.MotionEvent; -import android.view.SubMenu; import android.view.View; import android.view.WindowManager; import android.view.inputmethod.InputMethodManager; import android.widget.EditText; -import android.widget.ImageButton; +import android.widget.LinearLayout; import android.widget.TextView; import com.joanzapata.iconify.Iconify; @@ -44,11 +39,6 @@ import com.squareup.otto.Subscribe; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.events.EventAppExit; import info.nightscout.androidaps.events.EventFeatureRunning; @@ -101,7 +91,8 @@ public class MainActivity extends AppCompatActivity { doMigrations(); registerBus(); - setUpTabs(false); + setupTabs(); + setupViews(false); } @Override @@ -154,29 +145,27 @@ public class MainActivity extends AppCompatActivity { public void onStatusEvent(final EventRefreshGui ev) { String lang = SP.getString("language", "en"); LocaleHelper.setLocale(getApplicationContext(), lang); - runOnUiThread(new Runnable() { - @Override - public void run() { - if (ev.recreate) { - recreate(); - } else { - try { // activity may be destroyed - setUpTabs(true); - } catch (IllegalStateException e) { - log.error("Unhandled exception", e); - } + runOnUiThread(() -> { + if (ev.recreate) { + recreate(); + } else { + try { // activity may be destroyed + setupTabs(); + setupViews(true); + } catch (IllegalStateException e) { + log.error("Unhandled exception", e); } - - boolean lockScreen = BuildConfig.NSCLIENTOLNY && SP.getBoolean("lockscreen", false); - if (lockScreen) - getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); - else - getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); } + + boolean lockScreen = BuildConfig.NSCLIENTOLNY && SP.getBoolean("lockscreen", false); + if (lockScreen) + getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); + else + getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); }); } - private void setUpTabs(boolean switchToLast) { + private void setupViews(boolean switchToLast) { TabPageAdapter pageAdapter = new TabPageAdapter(getSupportFragmentManager(), this); NavigationView navigationView = findViewById(R.id.navigation_view); navigationView.setNavigationItemSelectedListener(menuItem -> { @@ -200,12 +189,32 @@ public class MainActivity extends AppCompatActivity { } ViewPager mPager = findViewById(R.id.pager); mPager.setAdapter(pageAdapter); - TabLayout tabLayout = findViewById(R.id.tabs); - tabLayout.setupWithViewPager(mPager, true); if (switchToLast) mPager.setCurrentItem(pageAdapter.getCount() - 1, false); } + private void setupTabs() { + ViewPager viewPager = findViewById(R.id.pager); + TabLayout normalTabs = findViewById(R.id.tabs_normal); + normalTabs.setupWithViewPager(viewPager, true); + TabLayout compactTabs = findViewById(R.id.tabs_compact); + compactTabs.setupWithViewPager(viewPager, true); + Toolbar toolbar = findViewById(R.id.toolbar); + if (SP.getBoolean("short_tabtitles", false)) { + normalTabs.setVisibility(View.GONE); + compactTabs.setVisibility(View.VISIBLE); + toolbar.setLayoutParams(new LinearLayout.LayoutParams(Toolbar.LayoutParams.MATCH_PARENT, (int) getResources().getDimension(R.dimen.compact_height))); + } else { + normalTabs.setVisibility(View.VISIBLE); + compactTabs.setVisibility(View.GONE); + TypedValue typedValue = new TypedValue(); + if (getTheme().resolveAttribute(R.attr.actionBarSize, typedValue, true)) { + toolbar.setLayoutParams(new LinearLayout.LayoutParams(Toolbar.LayoutParams.MATCH_PARENT, + TypedValue.complexToDimensionPixelSize(typedValue.data, getResources().getDisplayMetrics()))); + } + } + } + private void registerBus() { try { MainApp.bus().unregister(this); diff --git a/app/src/main/java/info/nightscout/androidaps/tabs/NonConsumingToolbar.java b/app/src/main/java/info/nightscout/androidaps/tabs/NonConsumingToolbar.java deleted file mode 100644 index 93f08c6d43..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/tabs/NonConsumingToolbar.java +++ /dev/null @@ -1,28 +0,0 @@ -package info.nightscout.androidaps.tabs; - -import android.content.Context; -import android.support.annotation.Nullable; -import android.support.v7.widget.Toolbar; -import android.util.AttributeSet; -import android.view.MotionEvent; - -public class NonConsumingToolbar extends Toolbar { - public NonConsumingToolbar(Context context) { - super(context); - } - - public NonConsumingToolbar(Context context, @Nullable AttributeSet attrs) { - super(context, attrs); - } - - public NonConsumingToolbar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { - super(context, attrs, defStyleAttr); - } - - @Override - public boolean onTouchEvent(MotionEvent ev) { - if (ev.getAction() == MotionEvent.ACTION_DOWN || ev.getAction() == MotionEvent.ACTION_POINTER_DOWN) - return performClick(); - return false; - } -} diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 1da64832d1..c0f382fbb8 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -4,42 +4,37 @@ xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" - android:layout_height="match_parent"> + android:layout_height="match_parent" + android:clipChildren="false" + android:clipToPadding="false"> - + android:background="@color/colorPrimary" + app:contentInsetEndWithActions="48dp" + app:contentInsetStartWithNavigation="48dp" + android:elevation="4dp"> - - - - - - - + app:tabIndicatorColor="#FFFFFF" + app:tabMode="scrollable" /> + + + diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml index 47c8224673..9b772f77bd 100644 --- a/app/src/main/res/values/dimens.xml +++ b/app/src/main/res/values/dimens.xml @@ -2,4 +2,5 @@ 16dp 16dp + 30dp