From e95c4b2fc7fb0a8484b6f4fa3fc9209a5c62af6c Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 23 May 2020 10:57:57 +0200 Subject: [PATCH] Custom action bar view to ellipsize subtitle in the middle --- .../java/eu/faircode/email/ActivityView.java | 2 ++ .../java/eu/faircode/email/FragmentBase.java | 17 ++++++++--- app/src/main/res/layout/action_bar.xml | 30 +++++++++++++++++++ 3 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 app/src/main/res/layout/action_bar.xml diff --git a/app/src/main/java/eu/faircode/email/ActivityView.java b/app/src/main/java/eu/faircode/email/ActivityView.java index 05faf607d8..919ba4cc5d 100644 --- a/app/src/main/java/eu/faircode/email/ActivityView.java +++ b/app/src/main/java/eu/faircode/email/ActivityView.java @@ -164,6 +164,8 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB setContentView(view); getSupportActionBar().setDisplayHomeAsUpEnabled(true); + getSupportActionBar().setCustomView(R.layout.action_bar); + getSupportActionBar().setDisplayShowCustomEnabled(true); content_separator = findViewById(R.id.content_separator); content_pane = findViewById(R.id.content_pane); diff --git a/app/src/main/java/eu/faircode/email/FragmentBase.java b/app/src/main/java/eu/faircode/email/FragmentBase.java index 74acd18415..cfe7e199c3 100644 --- a/app/src/main/java/eu/faircode/email/FragmentBase.java +++ b/app/src/main/java/eu/faircode/email/FragmentBase.java @@ -37,6 +37,7 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.inputmethod.InputMethodManager; +import android.widget.TextView; import android.widget.Toast; import androidx.annotation.Nullable; @@ -59,6 +60,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import static android.app.ActionBar.DISPLAY_SHOW_CUSTOM; import static android.app.Activity.RESULT_OK; public class FragmentBase extends Fragment { @@ -255,10 +257,17 @@ public class FragmentBase extends Fragment { AppCompatActivity activity = (AppCompatActivity) getActivity(); if (activity != null && !isPane()) { ActionBar actionbar = activity.getSupportActionBar(); - if (actionbar != null) { - actionbar.setTitle(title == null ? getString(R.string.app_name) : title); - actionbar.setSubtitle(subtitle); - } + if (actionbar != null) + if ((actionbar.getDisplayOptions() & DISPLAY_SHOW_CUSTOM) == 0) { + actionbar.setTitle(title == null ? getString(R.string.app_name) : title); + actionbar.setSubtitle(subtitle); + } else { + View custom = actionbar.getCustomView(); + TextView tvTitle = custom.findViewById(R.id.title); + TextView tvSubtitle = custom.findViewById(R.id.subtitle); + tvTitle.setText(title == null ? getString(R.string.app_name) : title); + tvSubtitle.setText(subtitle); + } } } diff --git a/app/src/main/res/layout/action_bar.xml b/app/src/main/res/layout/action_bar.xml new file mode 100644 index 0000000000..2797d656a7 --- /dev/null +++ b/app/src/main/res/layout/action_bar.xml @@ -0,0 +1,30 @@ + + + + + + + \ No newline at end of file