Custom action bar view to ellipsize subtitle in the middle

This commit is contained in:
M66B 2020-05-23 10:57:57 +02:00
parent 3d20c6cdd1
commit e95c4b2fc7
3 changed files with 45 additions and 4 deletions

View File

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

View File

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

View File

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<eu.faircode.email.FixedTextView
android:id="@+id/title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ellipsize="end"
android:singleLine="true"
android:text="Title"
android:textAppearance="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<eu.faircode.email.FixedTextView
android:id="@+id/subtitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ellipsize="middle"
android:singleLine="true"
android:text="Subtitle"
android:textAppearance="@style/TextAppearance.AppCompat.Widget.ActionBar.Subtitle"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/title" />
</androidx.constraintlayout.widget.ConstraintLayout>