Compact version of toolbar

This commit is contained in:
TebbeUbben 2018-06-04 21:34:21 +02:00
parent 6c1ccebb4a
commit 80e9d769e4
4 changed files with 69 additions and 92 deletions

View file

@ -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);

View file

@ -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;
}
}

View file

@ -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">
<FrameLayout
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:clipChildren="false"
android:clipToPadding="false">
android:background="@color/colorPrimary"
app:contentInsetEndWithActions="48dp"
app:contentInsetStartWithNavigation="48dp"
android:elevation="4dp">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:elevation="4dp"
app:contentInsetEnd="64dp"
app:contentInsetStart="64dp"
app:contentInsetLeft="64dp"
app:contentInsetRight="64dp"
app:contentInsetEndWithActions="64dp"
app:contentInsetStartWithNavigation="64dp">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
app:tabIndicatorColor="@color/colorAccent"
app:tabMode="scrollable" />
</android.support.v7.widget.Toolbar>
<info.nightscout.androidaps.tabs.NonConsumingToolbar
android:id="@+id/toolbar"
<android.support.design.widget.TabLayout
android:id="@+id/tabs_normal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:translationZ="4dp" />
</FrameLayout>
app:tabIndicatorColor="#FFFFFF"
app:tabMode="scrollable" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs_compact"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
app:tabMinWidth="0dp"
app:tabPadding="0dp"
app:tabIndicatorColor="#FFFFFF"
app:tabMode="scrollable" />
</android.support.v7.widget.Toolbar>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
@ -56,7 +51,7 @@
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:itemBackground="@android:color/transparent"
app:itemBackground="?selectableItemBackground"
app:itemTextColor="#FFFFFF"
android:layout_gravity="start" />
</android.support.v4.widget.DrawerLayout>

View file

@ -2,4 +2,5 @@
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="compact_height">30dp</dimen>
</resources>