Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <android.support.v7.widget.Toolbar
- android:id="@+id/toolbar_top"
- android:layout_height="wrap_content"
- android:layout_width="match_parent"
- android:minHeight="?attr/actionBarSize"
- android:background="@color/action_bar_bkgnd"
- app:theme="@style/ToolBarTheme" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Toolbar Title"
- android:layout_gravity="center"
- android:id="@+id/toolbar_title" />
- </android.support.v7.widget.Toolbar>
- Toolbar toolbarTop = (Toolbar) findViewById(R.id.toolbar_top);
- TextView mTitle = (TextView) toolbarTop.findViewById(R.id.toolbar_title);
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.v7.widget.Toolbar
- style="@style/ToolBarStyle.Event"
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/toolbar"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="?attr/colorPrimary"
- android:minHeight="@dimen/abc_action_bar_default_height_material" />
- <style name="ToolBarStyle" parent="ToolBarStyle.Base"/>
- <style name="ToolBarStyle.Base" parent="">
- <item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item>
- <item name="theme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
- </style>
- <style name="ToolBarStyle.Event" parent="ToolBarStyle">
- <item name="titleTextAppearance">@style/TextAppearance.Widget.Event.Toolbar.Title</item>
- </style>
- <style name="TextAppearance.Widget.Event.Toolbar.Title" parent="TextAppearance.Widget.AppCompat.Toolbar.Title">
- <!--Any text styling can be done here-->
- <item name="android:textStyle">normal</item>
- <item name="android:textSize">@dimen/event_title_text_size</item>
- </style>
- private TextView getActionBarTextView() {
- TextView titleTextView = null;
- try {
- Field f = mToolBar.getClass().getDeclaredField("mTitleTextView");
- f.setAccessible(true);
- titleTextView = (TextView) f.get(mToolBar);
- } catch (NoSuchFieldException e) {
- } catch (IllegalAccessException e) {
- }
- return titleTextView;
- }
- public static TextView getToolbarTitleView(ActionBarActivity activity, Toolbar toolbar){
- ActionBar actionBar = activity.getSupportActionBar();
- CharSequence actionbarTitle = null;
- if(actionBar != null)
- actionbarTitle = actionBar.getTitle();
- actionbarTitle = TextUtils.isEmpty(actionbarTitle) ? toolbar.getTitle() : actionbarTitle;
- if(TextUtils.isEmpty(actionbarTitle)) return null;
- // can't find if title not set
- for(int i= 0; i < toolbar.getChildCount(); i++){
- View v = toolbar.getChildAt(i);
- if(v != null && v instanceof TextView){
- TextView t = (TextView) v;
- CharSequence title = t.getText();
- if(!TextUtils.isEmpty(title) && actionbarTitle.equals(title) && t.getId() == View.NO_ID){
- //Toolbar does not assign id to views with layout params SYSTEM, hence getId() == View.NO_ID
- //in same manner subtitle TextView can be obtained.
- return t;
- }
- }
- }
- return null;
- }
- <android.support.v7.widget.Toolbar
- android:id="@+id/toolbar"
- android:layout_width="match_parent"
- android:layout_height="?attr/actionBarSize"
- android:background="?attr/colorPrimary"
- app:popupTheme="@style/AppTheme.PopupOverlay">
- <TextView
- android:id="@+id/toolbar_title"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
- android:layout_gravity="center" />
- </android.support.v7.widget.Toolbar>
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
- TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
- setSupportActionBar(toolbar);
- mTitle.setText(toolbar.getTitle());
- getSupportActionBar().setDisplayShowTitleEnabled(false);
- public class TestActivity extends AppCompatActivity {
- private Toolbar toolbar;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- super.setContentView(R.layout.activity_test);
- toolbar = (Toolbar) findViewById(R.id.tool_bar); // Attaching the layout to the toolbar object
- setSupportActionBar(toolbar);
- customizeToolbar(toolbar);
- }
- public void customizeToolbar(Toolbar toolbar){
- // Save current title and subtitle
- final CharSequence originalTitle = toolbar.getTitle();
- final CharSequence originalSubtitle = toolbar.getSubtitle();
- // Temporarily modify title and subtitle to help detecting each
- toolbar.setTitle("title");
- toolbar.setSubtitle("subtitle");
- for(int i = 0; i < toolbar.getChildCount(); i++){
- View view = toolbar.getChildAt(i);
- if(view instanceof TextView){
- TextView textView = (TextView) view;
- if(textView.getText().equals("title")){
- // Customize title's TextView
- Toolbar.LayoutParams params = new Toolbar.LayoutParams(Toolbar.LayoutParams.WRAP_CONTENT, Toolbar.LayoutParams.MATCH_PARENT);
- params.gravity = Gravity.CENTER_HORIZONTAL;
- textView.setLayoutParams(params);
- // Apply custom font using the Calligraphy library
- Typeface typeface = TypefaceUtils.load(getAssets(), "fonts/myfont-1.otf");
- textView.setTypeface(typeface);
- } else if(textView.getText().equals("subtitle")){
- // Customize subtitle's TextView
- Toolbar.LayoutParams params = new Toolbar.LayoutParams(Toolbar.LayoutParams.WRAP_CONTENT, Toolbar.LayoutParams.MATCH_PARENT);
- params.gravity = Gravity.CENTER_HORIZONTAL;
- textView.setLayoutParams(params);
- // Apply custom font using the Calligraphy library
- Typeface typeface = TypefaceUtils.load(getAssets(), "fonts/myfont-2.otf");
- textView.setTypeface(typeface);
- }
- }
- }
- // Restore title and subtitle
- toolbar.setTitle(originalTitle);
- toolbar.setSubtitle(originalSubtitle);
- }
- }
- getSupportActionBar().setDisplayShowTitleEnabled(false);
- or
- getActionBar().setDisplayShowTitleEnabled(false);
- public void updateActionbar(String title){
- SpannableString spannableString = new SpannableString(title);
- spannableString.setSpan(new TypefaceSpanString(this, "futurastdmedium.ttf"),
- 0, spannableString.length(),
- Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
- mToolbar.setTitle(spannableString);
- }
- <android.support.v7.widget.Toolbar
- android:id="@+id/toolbar_top"
- android:layout_height="wrap_content"
- android:layout_width="match_parent"
- android:minHeight="?attr/actionBarSize"
- android:background="@color/action_bar_bkgnd"
- app:theme="@style/ToolBarTheme" >
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Toolbar Title"
- android:layout_gravity="center"
- android:gravity="center"
- android:id="@+id/toolbar_title" />
- </android.support.v7.widget.Toolbar>
- Toolbar mToolbar = parent.findViewById(R.id.toolbar_top);
- TextView mToolbarCustomTitle = parent.findViewById(R.id.toolbar_title);
- //setup width of custom title to match in parent toolbar
- mToolbar.postDelayed(new Runnable()
- {
- @Override
- public void run ()
- {
- int maxWidth = mToolbar.getWidth();
- int titleWidth = mToolbarCustomTitle.getWidth();
- int iconWidth = maxWidth - titleWidth;
- if (iconWidth > 0)
- {
- //icons (drawer, menu) are on left and right side
- int width = maxWidth - iconWidth * 2;
- mToolbarCustomTitle.setMinimumWidth(width);
- mToolbarCustomTitle.getLayoutParams().width = width;
- }
- }
- }, 0);
- static void centerToolbarTitle(@NonNull final Toolbar toolbar) {
- final CharSequence title = toolbar.getTitle();
- final ArrayList<View> outViews = new ArrayList<>(1);
- toolbar.findViewsWithText(outViews, title, View.FIND_VIEWS_WITH_TEXT);
- if (!outViews.isEmpty()) {
- final TextView titleView = (TextView) outViews.get(0);
- titleView.setGravity(Gravity.CENTER);
- final Toolbar.LayoutParams layoutParams = (Toolbar.LayoutParams) titleView.getLayoutParams();
- layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
- toolbar.requestLayout();
- //also you can use titleView for changing font: titleView.setTypeface(Typeface);
- }
- }
- <android.support.v7.widget.Toolbar
- android:id="@+id/top_actionbar"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:theme="@style/AppThemeToolbar">
- <TextView
- android:id="@+id/pageTitle"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- />
- </android.support.v7.widget.Toolbar>
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
- // loop through all toolbar children right after setting support
- // action bar because the text view has no id assigned
- // also make sure that the activity has some title here
- // because calling setText() with an empty string actually
- // removes the text view from the toolbar
- TextView toolbarTitle = null;
- for (int i = 0; i < toolbar.getChildCount(); ++i) {
- View child = toolbar.getChildAt(i);
- // assuming that the title is the first instance of TextView
- // you can also check if the title string matches
- if (child instanceof TextView) {
- toolbarTitle = (TextView)child;
- break;
- }
- }
- public static void applyFontForToolbarTitle(Activity a){
- Toolbar toolbar = (Toolbar) a.findViewById(R.id.app_bar);
- for(int i = 0; i < toolbar.getChildCount(); i++){
- View view = toolbar.getChildAt(i);
- if(view instanceof TextView){
- TextView tv = (TextView) view;
- if(tv.getText().equals(a.getTitle())){
- tv.setTypeface(getRuneTypefaceBold(a));
- break;
- }
- }
- }
- }
- tv.setGravity(Gravity.CENTER);
- <android.support.v7.widget.Toolbar
- android:id="@+id/toolbar"
- android:layout_width="match_parent"
- android:layout_height="?attr/actionBarSize"
- android:background="?attr/colorPrimary"
- app:popupTheme="@style/AppTheme.PopupOverlay" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Order History"
- android:layout_gravity="center"
- android:id="@+id/toolbar_title"
- android:textSize="17sp"
- android:textStyle="bold"
- android:textColor="@color/colorWhite"
- />
- </android.support.v7.widget.Toolbar>
- Toolbar toolbarTop = (Toolbar) findViewById(R.id.toolbar_top);
- <RelativeLayout
- android:orientation="horizontal"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="?attr/colorPrimary">
- <android.support.v7.widget.Toolbar
- android:theme="@style/ThemeOverlay.AppCompat.Dark"
- android:id="@+id/activity_toolbar"
- android:layout_width="match_parent"
- android:layout_height="?attr/actionBarSize"
- android:background="?attr/colorPrimary"
- android:titleTextAppearance="@style/AppTheme.TitleTextView"
- android:layout_marginRight="40dp"
- android:layoutMode="clipBounds">
- <android.support.v7.widget.SearchView
- android:id="@+id/search_view"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="right"
- android:layout_centerVertical="true"
- android:layout_alignParentRight="true"
- android:foregroundTint="@color/white" />
- </android.support.v7.widget.Toolbar>
- <TextView
- android:id="@+id/toolbar_title"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginRight="90dp"
- android:text="@string/app_name"
- android:textSize="@dimen/title_text_size"
- android:textColor="@color/white"
- android:lines="1"
- android:layout_marginLeft="72dp"
- android:layout_centerVertical="true" />
- </RelativeLayout>
- @Override
- public void onBackPressed() {
- if(getTitle().equals(getResources().getString(R.string.app_name))) {
- super.onBackPressed();}
- else {
- //set visiblity
- }
- }
- private void makeTitleCenter(String title, Toolbar toolbar) {
- if (title != null && !TextUtils.isEmpty(title.trim())) {
- final String tag = " ";
- if (getSupportActionBar() != null) {
- getSupportActionBar().setTitle(tag);
- }
- TextView titleTv = null;
- View leftBtn = null;
- for (int i = 0; i < toolbar.getChildCount(); i++) {
- View view = toolbar.getChildAt(i);
- CharSequence text = null;
- if (view instanceof TextView && (text = ((TextView) view).getText()) != null && text.equals(tag)) {
- titleTv = (TextView) view;
- } else if (view instanceof ImageButton) {
- leftBtn = view;
- }
- }
- if (titleTv != null) {
- final TextView fTitleTv = titleTv;
- final View fLeftBtn = leftBtn;
- fTitleTv.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
- @Override
- public void onGlobalLayout() {
- fTitleTv.getViewTreeObserver().removeOnGlobalLayoutListener(this);
- int leftWidgetWidth = fLeftBtn != null ? fLeftBtn.getWidth() : 0;
- fTitleTv.setPadding(DimenUtil.getResources().getDisplayMetrics().widthPixels / 2 - leftWidgetWidth - fTitleTv.getWidth() / 2, 0, 0, 0);
- fTitleTv.requestLayout();
- }
- });
- }
- }
- }
- <android.support.v7.widget.Toolbar
- android:id="@+id/htab_toolbar"
- android:layout_width="match_parent"
- android:layout_height="?attr/actionBarSize"
- android:layout_gravity="top"
- android:background="@color/partial_transparent"
- android:gravity="center"
- app:layout_collapseMode="pin"
- app:layout_scrollFlags="scroll|enterAlways"
- app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
- <android.support.v7.widget.Toolbar
- android:id="@+id/toolbar"
- android:layout_width="match_parent"
- android:layout_height="?attr/actionBarSize"
- android:background="?attr/colorPrimary"
- app:popupTheme="@style/AppTheme.PopupOverlay">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Toolbar Title"
- android:textSize="25sp"
- android:textStyle="bold"
- android:textColor="@color/white"
- android:layout_gravity="center"
- android:id="@+id/toolbar_title" />
- </android.support.v7.widget.Toolbar>
- Typeface face= Typeface.createFromAsset(getAssets(), "font/font.ttf"); // your custom font
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
- toolbar.setTypeface(face);
- setSupportActionBar(toolbar);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement