Advertisement
Guest User

Untitled

a guest
Aug 26th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.11 KB | None | 0 0
  1. <android.support.v7.widget.Toolbar
  2. android:id="@+id/toolbar_top"
  3. android:layout_height="wrap_content"
  4. android:layout_width="match_parent"
  5. android:minHeight="?attr/actionBarSize"
  6. android:background="@color/action_bar_bkgnd"
  7. app:theme="@style/ToolBarTheme" >
  8.  
  9.  
  10. <TextView
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:text="Toolbar Title"
  14. android:layout_gravity="center"
  15. android:id="@+id/toolbar_title" />
  16.  
  17.  
  18. </android.support.v7.widget.Toolbar>
  19.  
  20. Toolbar toolbarTop = (Toolbar) findViewById(R.id.toolbar_top);
  21. TextView mTitle = (TextView) toolbarTop.findViewById(R.id.toolbar_title);
  22.  
  23. <?xml version="1.0" encoding="utf-8"?>
  24. <android.support.v7.widget.Toolbar
  25. style="@style/ToolBarStyle.Event"
  26. xmlns:android="http://schemas.android.com/apk/res/android"
  27. android:id="@+id/toolbar"
  28. android:layout_width="match_parent"
  29. android:layout_height="wrap_content"
  30. android:background="?attr/colorPrimary"
  31. android:minHeight="@dimen/abc_action_bar_default_height_material" />
  32.  
  33. <style name="ToolBarStyle" parent="ToolBarStyle.Base"/>
  34.  
  35. <style name="ToolBarStyle.Base" parent="">
  36. <item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item>
  37. <item name="theme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
  38. </style>
  39.  
  40. <style name="ToolBarStyle.Event" parent="ToolBarStyle">
  41. <item name="titleTextAppearance">@style/TextAppearance.Widget.Event.Toolbar.Title</item>
  42. </style>
  43.  
  44. <style name="TextAppearance.Widget.Event.Toolbar.Title" parent="TextAppearance.Widget.AppCompat.Toolbar.Title">
  45. <!--Any text styling can be done here-->
  46. <item name="android:textStyle">normal</item>
  47. <item name="android:textSize">@dimen/event_title_text_size</item>
  48. </style>
  49.  
  50. private TextView getActionBarTextView() {
  51. TextView titleTextView = null;
  52.  
  53. try {
  54. Field f = mToolBar.getClass().getDeclaredField("mTitleTextView");
  55. f.setAccessible(true);
  56. titleTextView = (TextView) f.get(mToolBar);
  57. } catch (NoSuchFieldException e) {
  58. } catch (IllegalAccessException e) {
  59. }
  60. return titleTextView;
  61. }
  62.  
  63. public static TextView getToolbarTitleView(ActionBarActivity activity, Toolbar toolbar){
  64. ActionBar actionBar = activity.getSupportActionBar();
  65. CharSequence actionbarTitle = null;
  66. if(actionBar != null)
  67. actionbarTitle = actionBar.getTitle();
  68. actionbarTitle = TextUtils.isEmpty(actionbarTitle) ? toolbar.getTitle() : actionbarTitle;
  69. if(TextUtils.isEmpty(actionbarTitle)) return null;
  70. // can't find if title not set
  71. for(int i= 0; i < toolbar.getChildCount(); i++){
  72. View v = toolbar.getChildAt(i);
  73. if(v != null && v instanceof TextView){
  74. TextView t = (TextView) v;
  75. CharSequence title = t.getText();
  76. if(!TextUtils.isEmpty(title) && actionbarTitle.equals(title) && t.getId() == View.NO_ID){
  77. //Toolbar does not assign id to views with layout params SYSTEM, hence getId() == View.NO_ID
  78. //in same manner subtitle TextView can be obtained.
  79. return t;
  80. }
  81. }
  82. }
  83. return null;
  84. }
  85.  
  86. <android.support.v7.widget.Toolbar
  87. android:id="@+id/toolbar"
  88. android:layout_width="match_parent"
  89. android:layout_height="?attr/actionBarSize"
  90. android:background="?attr/colorPrimary"
  91. app:popupTheme="@style/AppTheme.PopupOverlay">
  92.  
  93. <TextView
  94. android:id="@+id/toolbar_title"
  95. android:layout_width="wrap_content"
  96. android:layout_height="wrap_content"
  97. style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
  98. android:layout_gravity="center" />
  99.  
  100. </android.support.v7.widget.Toolbar>
  101.  
  102. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  103. TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
  104.  
  105. setSupportActionBar(toolbar);
  106. mTitle.setText(toolbar.getTitle());
  107.  
  108. getSupportActionBar().setDisplayShowTitleEnabled(false);
  109.  
  110. public class TestActivity extends AppCompatActivity {
  111. private Toolbar toolbar;
  112.  
  113. @Override
  114. protected void onCreate(Bundle savedInstanceState) {
  115. super.onCreate(savedInstanceState);
  116. super.setContentView(R.layout.activity_test);
  117.  
  118. toolbar = (Toolbar) findViewById(R.id.tool_bar); // Attaching the layout to the toolbar object
  119. setSupportActionBar(toolbar);
  120.  
  121. customizeToolbar(toolbar);
  122. }
  123.  
  124. public void customizeToolbar(Toolbar toolbar){
  125. // Save current title and subtitle
  126. final CharSequence originalTitle = toolbar.getTitle();
  127. final CharSequence originalSubtitle = toolbar.getSubtitle();
  128.  
  129. // Temporarily modify title and subtitle to help detecting each
  130. toolbar.setTitle("title");
  131. toolbar.setSubtitle("subtitle");
  132.  
  133. for(int i = 0; i < toolbar.getChildCount(); i++){
  134. View view = toolbar.getChildAt(i);
  135.  
  136. if(view instanceof TextView){
  137. TextView textView = (TextView) view;
  138.  
  139.  
  140. if(textView.getText().equals("title")){
  141. // Customize title's TextView
  142. Toolbar.LayoutParams params = new Toolbar.LayoutParams(Toolbar.LayoutParams.WRAP_CONTENT, Toolbar.LayoutParams.MATCH_PARENT);
  143. params.gravity = Gravity.CENTER_HORIZONTAL;
  144. textView.setLayoutParams(params);
  145.  
  146. // Apply custom font using the Calligraphy library
  147. Typeface typeface = TypefaceUtils.load(getAssets(), "fonts/myfont-1.otf");
  148. textView.setTypeface(typeface);
  149.  
  150. } else if(textView.getText().equals("subtitle")){
  151. // Customize subtitle's TextView
  152. Toolbar.LayoutParams params = new Toolbar.LayoutParams(Toolbar.LayoutParams.WRAP_CONTENT, Toolbar.LayoutParams.MATCH_PARENT);
  153. params.gravity = Gravity.CENTER_HORIZONTAL;
  154. textView.setLayoutParams(params);
  155.  
  156. // Apply custom font using the Calligraphy library
  157. Typeface typeface = TypefaceUtils.load(getAssets(), "fonts/myfont-2.otf");
  158. textView.setTypeface(typeface);
  159. }
  160. }
  161. }
  162.  
  163. // Restore title and subtitle
  164. toolbar.setTitle(originalTitle);
  165. toolbar.setSubtitle(originalSubtitle);
  166. }
  167. }
  168.  
  169. getSupportActionBar().setDisplayShowTitleEnabled(false);
  170. or
  171. getActionBar().setDisplayShowTitleEnabled(false);
  172.  
  173. public void updateActionbar(String title){
  174. SpannableString spannableString = new SpannableString(title);
  175. spannableString.setSpan(new TypefaceSpanString(this, "futurastdmedium.ttf"),
  176. 0, spannableString.length(),
  177. Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  178. mToolbar.setTitle(spannableString);
  179. }
  180.  
  181. <android.support.v7.widget.Toolbar
  182. android:id="@+id/toolbar_top"
  183. android:layout_height="wrap_content"
  184. android:layout_width="match_parent"
  185. android:minHeight="?attr/actionBarSize"
  186. android:background="@color/action_bar_bkgnd"
  187. app:theme="@style/ToolBarTheme" >
  188.  
  189. <TextView
  190. android:layout_width="match_parent"
  191. android:layout_height="wrap_content"
  192. android:text="Toolbar Title"
  193. android:layout_gravity="center"
  194. android:gravity="center"
  195. android:id="@+id/toolbar_title" />
  196. </android.support.v7.widget.Toolbar>
  197.  
  198. Toolbar mToolbar = parent.findViewById(R.id.toolbar_top);
  199. TextView mToolbarCustomTitle = parent.findViewById(R.id.toolbar_title);
  200.  
  201. //setup width of custom title to match in parent toolbar
  202. mToolbar.postDelayed(new Runnable()
  203. {
  204. @Override
  205. public void run ()
  206. {
  207. int maxWidth = mToolbar.getWidth();
  208. int titleWidth = mToolbarCustomTitle.getWidth();
  209. int iconWidth = maxWidth - titleWidth;
  210.  
  211. if (iconWidth > 0)
  212. {
  213. //icons (drawer, menu) are on left and right side
  214. int width = maxWidth - iconWidth * 2;
  215. mToolbarCustomTitle.setMinimumWidth(width);
  216. mToolbarCustomTitle.getLayoutParams().width = width;
  217. }
  218. }
  219. }, 0);
  220.  
  221. static void centerToolbarTitle(@NonNull final Toolbar toolbar) {
  222. final CharSequence title = toolbar.getTitle();
  223. final ArrayList<View> outViews = new ArrayList<>(1);
  224. toolbar.findViewsWithText(outViews, title, View.FIND_VIEWS_WITH_TEXT);
  225. if (!outViews.isEmpty()) {
  226. final TextView titleView = (TextView) outViews.get(0);
  227. titleView.setGravity(Gravity.CENTER);
  228. final Toolbar.LayoutParams layoutParams = (Toolbar.LayoutParams) titleView.getLayoutParams();
  229. layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
  230. toolbar.requestLayout();
  231. //also you can use titleView for changing font: titleView.setTypeface(Typeface);
  232. }
  233. }
  234.  
  235. <android.support.v7.widget.Toolbar
  236. android:id="@+id/top_actionbar"
  237. android:layout_width="match_parent"
  238. android:layout_height="wrap_content"
  239. android:theme="@style/AppThemeToolbar">
  240.  
  241. <TextView
  242. android:id="@+id/pageTitle"
  243. android:layout_width="match_parent"
  244. android:layout_height="wrap_content"
  245. android:layout_gravity="center"
  246. />
  247. </android.support.v7.widget.Toolbar>
  248.  
  249. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  250. setSupportActionBar(toolbar);
  251.  
  252. // loop through all toolbar children right after setting support
  253. // action bar because the text view has no id assigned
  254.  
  255. // also make sure that the activity has some title here
  256. // because calling setText() with an empty string actually
  257. // removes the text view from the toolbar
  258.  
  259. TextView toolbarTitle = null;
  260. for (int i = 0; i < toolbar.getChildCount(); ++i) {
  261. View child = toolbar.getChildAt(i);
  262.  
  263. // assuming that the title is the first instance of TextView
  264. // you can also check if the title string matches
  265. if (child instanceof TextView) {
  266. toolbarTitle = (TextView)child;
  267. break;
  268. }
  269. }
  270.  
  271. public static void applyFontForToolbarTitle(Activity a){
  272. Toolbar toolbar = (Toolbar) a.findViewById(R.id.app_bar);
  273. for(int i = 0; i < toolbar.getChildCount(); i++){
  274. View view = toolbar.getChildAt(i);
  275. if(view instanceof TextView){
  276. TextView tv = (TextView) view;
  277. if(tv.getText().equals(a.getTitle())){
  278. tv.setTypeface(getRuneTypefaceBold(a));
  279. break;
  280. }
  281. }
  282. }
  283. }
  284.  
  285. tv.setGravity(Gravity.CENTER);
  286.  
  287. <android.support.v7.widget.Toolbar
  288. android:id="@+id/toolbar"
  289. android:layout_width="match_parent"
  290. android:layout_height="?attr/actionBarSize"
  291. android:background="?attr/colorPrimary"
  292. app:popupTheme="@style/AppTheme.PopupOverlay" >
  293.  
  294. <TextView
  295. android:layout_width="wrap_content"
  296. android:layout_height="wrap_content"
  297. android:text="Order History"
  298. android:layout_gravity="center"
  299. android:id="@+id/toolbar_title"
  300. android:textSize="17sp"
  301. android:textStyle="bold"
  302. android:textColor="@color/colorWhite"
  303. />
  304.  
  305. </android.support.v7.widget.Toolbar>
  306.  
  307. Toolbar toolbarTop = (Toolbar) findViewById(R.id.toolbar_top);
  308.  
  309. <RelativeLayout
  310. android:orientation="horizontal"
  311. android:layout_width="match_parent"
  312. android:layout_height="wrap_content"
  313. android:background="?attr/colorPrimary">
  314.  
  315. <android.support.v7.widget.Toolbar
  316. android:theme="@style/ThemeOverlay.AppCompat.Dark"
  317. android:id="@+id/activity_toolbar"
  318. android:layout_width="match_parent"
  319. android:layout_height="?attr/actionBarSize"
  320. android:background="?attr/colorPrimary"
  321. android:titleTextAppearance="@style/AppTheme.TitleTextView"
  322. android:layout_marginRight="40dp"
  323. android:layoutMode="clipBounds">
  324.  
  325. <android.support.v7.widget.SearchView
  326. android:id="@+id/search_view"
  327. android:layout_width="wrap_content"
  328. android:layout_height="wrap_content"
  329. android:layout_gravity="right"
  330. android:layout_centerVertical="true"
  331. android:layout_alignParentRight="true"
  332. android:foregroundTint="@color/white" />
  333. </android.support.v7.widget.Toolbar>
  334.  
  335. <TextView
  336. android:id="@+id/toolbar_title"
  337. android:layout_width="match_parent"
  338. android:layout_height="wrap_content"
  339. android:layout_marginRight="90dp"
  340. android:text="@string/app_name"
  341. android:textSize="@dimen/title_text_size"
  342. android:textColor="@color/white"
  343. android:lines="1"
  344. android:layout_marginLeft="72dp"
  345. android:layout_centerVertical="true" />
  346.  
  347. </RelativeLayout>
  348.  
  349. @Override
  350. public void onBackPressed() {
  351. if(getTitle().equals(getResources().getString(R.string.app_name))) {
  352. super.onBackPressed();}
  353. else {
  354. //set visiblity
  355. }
  356. }
  357.  
  358. private void makeTitleCenter(String title, Toolbar toolbar) {
  359. if (title != null && !TextUtils.isEmpty(title.trim())) {
  360. final String tag = " ";
  361. if (getSupportActionBar() != null) {
  362. getSupportActionBar().setTitle(tag);
  363. }
  364. TextView titleTv = null;
  365. View leftBtn = null;
  366. for (int i = 0; i < toolbar.getChildCount(); i++) {
  367. View view = toolbar.getChildAt(i);
  368. CharSequence text = null;
  369. if (view instanceof TextView && (text = ((TextView) view).getText()) != null && text.equals(tag)) {
  370. titleTv = (TextView) view;
  371. } else if (view instanceof ImageButton) {
  372. leftBtn = view;
  373. }
  374. }
  375. if (titleTv != null) {
  376. final TextView fTitleTv = titleTv;
  377. final View fLeftBtn = leftBtn;
  378. fTitleTv.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
  379. @Override
  380. public void onGlobalLayout() {
  381. fTitleTv.getViewTreeObserver().removeOnGlobalLayoutListener(this);
  382. int leftWidgetWidth = fLeftBtn != null ? fLeftBtn.getWidth() : 0;
  383. fTitleTv.setPadding(DimenUtil.getResources().getDisplayMetrics().widthPixels / 2 - leftWidgetWidth - fTitleTv.getWidth() / 2, 0, 0, 0);
  384. fTitleTv.requestLayout();
  385. }
  386. });
  387. }
  388. }
  389. }
  390.  
  391. <android.support.v7.widget.Toolbar
  392. android:id="@+id/htab_toolbar"
  393. android:layout_width="match_parent"
  394. android:layout_height="?attr/actionBarSize"
  395. android:layout_gravity="top"
  396. android:background="@color/partial_transparent"
  397. android:gravity="center"
  398. app:layout_collapseMode="pin"
  399. app:layout_scrollFlags="scroll|enterAlways"
  400. app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
  401.  
  402. <android.support.v7.widget.Toolbar
  403. android:id="@+id/toolbar"
  404. android:layout_width="match_parent"
  405. android:layout_height="?attr/actionBarSize"
  406. android:background="?attr/colorPrimary"
  407. app:popupTheme="@style/AppTheme.PopupOverlay">
  408. <TextView
  409. android:layout_width="wrap_content"
  410. android:layout_height="wrap_content"
  411. android:text="Toolbar Title"
  412. android:textSize="25sp"
  413. android:textStyle="bold"
  414. android:textColor="@color/white"
  415. android:layout_gravity="center"
  416. android:id="@+id/toolbar_title" />
  417. </android.support.v7.widget.Toolbar>
  418.  
  419. Typeface face= Typeface.createFromAsset(getAssets(), "font/font.ttf"); // your custom font
  420. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  421. toolbar.setTypeface(face);
  422. setSupportActionBar(toolbar);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement