Skip to content

Commit 04a57e7

Browse files
committed
Finishing my second pass through chapter 6
1 parent be40c74 commit 04a57e7

File tree

5 files changed

+30
-35
lines changed

5 files changed

+30
-35
lines changed

images/chapter6/android/2.png

268 KB
Loading

images/chapter6/android/3.png

276 KB
Loading

images/chapter6/ios/3.png

311 KB
Loading

index.html

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1743,16 +1743,15 @@ <h4 class="exercise-start">
17431743
</blockquote>
17441744
<p>Let’s move onto how to accomplish this same hint color task on Android.</p>
17451745
<h3 id="accessing-android-apis">Accessing Android APIs</h3>
1746-
<p>Talk about Googling the same sort of thing for Android APIs (setting the hint color).</p>
1746+
<p>Much like with iOS, if you’ve not a native Android developer, figuring out how to accomplish a task on Android often starts with a web search. If you run a <a href="https://www.google.com/#q=style%20android%20text%20field%20hint%20text">search for “style Android text field hint text”</a>, you’ll end up on a <a href="http://stackoverflow.com/questions/6438478/sethinttextcolor-in-edittext">Stack Overflow answer</a> that recommends using a <a href="http://developer.android.com/reference/android/widget/TextView.html#attr_android:textColorHint"><code>android.widget.TextView</code>’s <code>setTextHintColor()</code> method</a>. Let’s alter our code to do that.</p>
17471747
<h4 class="exercise-start">
1748-
<b>Exercise</b>: ???
1748+
<b>Exercise</b>: Change hint colors on Android
17491749
</h4>
17501750

17511751
<p>Open <code>app/utils/hint-util.ts</code> and replace the existing contents of the file with the following code:</p>
17521752
<pre><code class="lang-TypeScript">import {Color} from &quot;color&quot;;
17531753
import {TextField} from &quot;ui/text-field&quot;;
17541754

1755-
declare var android: any;
17561755
declare var NSAttributedString: any;
17571756
declare var NSDictionary: any;
17581757
declare var NSForegroundColorAttributeName: any;
@@ -1773,41 +1772,37 @@ <h4 class="exercise-start">
17731772
</code></pre>
17741773
<div class="exercise-end"></div>
17751774

1776-
<p>Talk about what just happened.</p>
1777-
<p>TODO: Insert gifs</p>
1778-
<p>Transition to status bar discussion</p>
1775+
<p>Remember from the previous section that NativeScript makes native objects available via a <code>android</code> property. In this case <code>args.view.android</code> refers to a <a href="http://developer.android.com/reference/android/widget/TextView.html"><code>TextView</code></a>, and therefore has the <code>setHintTextColor()</code> method that the Stack Overflow post said to call.</p>
1776+
<p>One other thing to notice is the if checks that you added around each of the native calls. Your TypeScript code runs across both platforms, and iOS APIs are not available on Android (and vice versa). Testing for the existence of the native object properties is a common way to fork your code in NativeScript to avoid errors. And with this change in place, your hint colors on Android are now far more legible.</p>
1777+
<p><img src="images/chapter6/android/2.png" alt="Better contrast on Android"></p>
1778+
<p>Let’s look at one last way we can improve the look of this app with native code.</p>
17791779
<h3 id="customizing-the-status-bar">Customizing the status bar</h3>
1780-
<p>Talk about status bars.</p>
1780+
<p>At the time of this writing, NativeScript does not expose a way to make translucent status bars—aka status bars that you can see through. There is an <a href="https://github.com/NativeScript/NativeScript/issues/601">open issue requesting this feature</a>, but as with anything else when building with NativeScript, you don’t have to be limited by what NativeScript provides out of the box. Let’s look at how you can use that power to make your status bars look a little nicer.</p>
17811781
<h4 class="exercise-start">
1782-
<b>Exercise</b>: ???
1782+
<b>Exercise</b>: Making translucent status bars
17831783
</h4>
17841784

1785-
<p>Open <code>app/main.ts</code> and replace the contents of the file with the following code:</p>
1785+
<p>Sometimes accomplishing tasks with native code is simple, as it was with setting hint text on Android, and sometimes it takes a little more work. Because setting a status bar’s appearance is slightly more involved, the code has been prepopulated in <code>app/utils/status-bar-util.ts</code>. There are a few comments the link to detailed information on the techniques used, if you’re curious about how it all works.</p>
1786+
<p>Because this code changes the appearance of the status bar, we’ll want to call this method as soon as possible, so that the status bar doesn’t awkwardly update after the app has already loaded. Therefore to use this new utility, open <code>app/main.ts</code> and replace the contents of the file with the following code, which calls a new <code>setStatusBarColors()</code> before the app is bootstrapped.</p>
17861787
<pre><code class="lang-TypeScript">import {nativeScriptBootstrap} from &quot;nativescript-angular/application&quot;;
17871788
import {AppComponent} from &quot;./app.component&quot;;
17881789
import {setStatusBarColors} from &quot;./utils/status-bar-util&quot;;
17891790

17901791
setStatusBarColors();
17911792
nativeScriptBootstrap(AppComponent);
17921793
</code></pre>
1793-
<p>Next, open <code>app/platform.ios.css</code> and paste in the following code:</p>
1794+
<p>Finally, because a translucent status bar continues to take up space on iOS, open <code>app/platform.ios.css</code> and paste in the following code, which bumps the content of the page on top of the status bar.</p>
17941795
<pre><code class="lang-CSS">Page {
17951796
margin-top: -20;
17961797
}
17971798
</code></pre>
17981799
<div class="exercise-end"></div>
17991800

1800-
<p>Talk about what just happened.</p>
1801-
<p>TODO: Insert images</p>
1801+
<p>And with that, your status bar is not translucent on iOS and Android:</p>
1802+
<p><img src="images/chapter6/android/3.png" alt="Updated status bar on Android">
1803+
<img src="images/chapter6/ios/3.png" alt="Updated status bar on iOS"></p>
18021804
<p>And... that&#39;s it! You&#39;ve created a functional, cross-platform, backend-driven app to manage your grocery list. In the process you&#39;ve created a unique UI for Android and iOS, leveraged NativeScript plugins and npm modules, learned how to log in and register, managed backend services, created a list with add and delete functionality, and more. </p>
18031805
<p>Congratulations! Feel free to <a href="https://twitter.com/intent/tweet?text=I%20just%20built%20an%20iOS%20and%20Android%20app%20using%20@NativeScript%20%F0%9F%8E%89.%20You%20can%20too!%20http://docs.nativescript.org/start/getting-started%20%23opensource">share your accomplishment on Twitter</a> or <a href="https://www.facebook.com/sharer/sharer.php?u=http%3A%2F%2Fdocs.nativescript.org%2Fstart%2Fgetting-started&amp;p%5B">Facebook</a> to impress your friends 😀.</p>
1804-
<blockquote>
1805-
<p><strong>TIP</strong>:</p>
1806-
<ul>
1807-
<li>If you&#39;re curious about how NativeScript makes it possible to directly invoke iOS and Android APIs, you can read about <a href="http://developer.telerik.com/featured/nativescript-works/">“How NativeScript Works”</a> on our blog.</li>
1808-
<li>Remember that the <a href="https://github.com/NativeScript/sample-Groceries/tree/angular-end">Groceries app&#39;s “angular-end” branch</a> has the final state of this tutorial. Feel free to refer back to it at any time.</li>
1809-
</ul>
1810-
</blockquote>
18111806

18121807
</div>
18131808
<div class="chapter">

src/chapters/chapter6.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ Let’s move onto how to accomplish this same hint color task on Android.
104104

105105
### Accessing Android APIs
106106

107-
Talk about Googling the same sort of thing for Android APIs (setting the hint color).
107+
Much like with iOS, if you’ve not a native Android developer, figuring out how to accomplish a task on Android often starts with a web search. If you run a [search for “style Android text field hint text”](https://www.google.com/#q=style%20android%20text%20field%20hint%20text), you’ll end up on a [Stack Overflow answer](http://stackoverflow.com/questions/6438478/sethinttextcolor-in-edittext) that recommends using a [`android.widget.TextView`’s `setTextHintColor()` method](http://developer.android.com/reference/android/widget/TextView.html#attr_android:textColorHint). Let’s alter our code to do that.
108108

109109
<h4 class="exercise-start">
110-
<b>Exercise</b>: ???
110+
<b>Exercise</b>: Change hint colors on Android
111111
</h4>
112112

113113
Open `app/utils/hint-util.ts` and replace the existing contents of the file with the following code:
@@ -116,7 +116,6 @@ Open `app/utils/hint-util.ts` and replace the existing contents of the file with
116116
import {Color} from "color";
117117
import {TextField} from "ui/text-field";
118118

119-
declare var android: any;
120119
declare var NSAttributedString: any;
121120
declare var NSDictionary: any;
122121
declare var NSForegroundColorAttributeName: any;
@@ -138,21 +137,25 @@ export function setHintColor(args: { view: TextField, color: Color }) {
138137

139138
<div class="exercise-end"></div>
140139

141-
Talk about what just happened.
140+
Remember from the previous section that NativeScript makes native objects available via a `android` property. In this case `args.view.android` refers to a [`TextView`](http://developer.android.com/reference/android/widget/TextView.html), and therefore has the `setHintTextColor()` method that the Stack Overflow post said to call.
142141

143-
TODO: Insert gifs
142+
One other thing to notice is the if checks that you added around each of the native calls. Your TypeScript code runs across both platforms, and iOS APIs are not available on Android (and vice versa). Testing for the existence of the native object properties is a common way to fork your code in NativeScript to avoid errors. And with this change in place, your hint colors on Android are now far more legible.
144143

145-
Transition to status bar discussion
144+
![Better contrast on Android](images/chapter6/android/2.png)
145+
146+
Let’s look at one last way we can improve the look of this app with native code.
146147

147148
### Customizing the status bar
148149

149-
Talk about status bars.
150+
At the time of this writing, NativeScript does not expose a way to make translucent status bars—aka status bars that you can see through. There is an [open issue requesting this feature](https://github.com/NativeScript/NativeScript/issues/601), but as with anything else when building with NativeScript, you don’t have to be limited by what NativeScript provides out of the box. Let’s look at how you can use that power to make your status bars look a little nicer.
150151

151152
<h4 class="exercise-start">
152-
<b>Exercise</b>: ???
153+
<b>Exercise</b>: Making translucent status bars
153154
</h4>
154155

155-
Open `app/main.ts` and replace the contents of the file with the following code:
156+
Sometimes accomplishing tasks with native code is simple, as it was with setting hint text on Android, and sometimes it takes a little more work. Because setting a status bar’s appearance is slightly more involved, the code has been prepopulated in `app/utils/status-bar-util.ts`. There are a few comments the link to detailed information on the techniques used, if you’re curious about how it all works.
157+
158+
Because this code changes the appearance of the status bar, we’ll want to call this method as soon as possible, so that the status bar doesn’t awkwardly update after the app has already loaded. Therefore to use this new utility, open `app/main.ts` and replace the contents of the file with the following code, which calls a new `setStatusBarColors()` before the app is bootstrapped.
156159

157160
``` TypeScript
158161
import {nativeScriptBootstrap} from "nativescript-angular/application";
@@ -163,7 +166,7 @@ setStatusBarColors();
163166
nativeScriptBootstrap(AppComponent);
164167
```
165168

166-
Next, open `app/platform.ios.css` and paste in the following code:
169+
Finally, because a translucent status bar continues to take up space on iOS, open `app/platform.ios.css` and paste in the following code, which bumps the content of the page on top of the status bar.
167170

168171
``` CSS
169172
Page {
@@ -173,14 +176,11 @@ Page {
173176

174177
<div class="exercise-end"></div>
175178

176-
Talk about what just happened.
179+
And with that, your status bar is not translucent on iOS and Android:
177180

178-
TODO: Insert images
181+
![Updated status bar on Android](images/chapter6/android/3.png)
182+
![Updated status bar on iOS](images/chapter6/ios/3.png)
179183

180184
And... that's it! You've created a functional, cross-platform, backend-driven app to manage your grocery list. In the process you've created a unique UI for Android and iOS, leveraged NativeScript plugins and npm modules, learned how to log in and register, managed backend services, created a list with add and delete functionality, and more.
181185

182186
Congratulations! Feel free to [share your accomplishment on Twitter](https://twitter.com/intent/tweet?text=I%20just%20built%20an%20iOS%20and%20Android%20app%20using%20@NativeScript%20%F0%9F%8E%89.%20You%20can%20too!%20http://docs.nativescript.org/start/getting-started%20%23opensource) or [Facebook](https://www.facebook.com/sharer/sharer.php?u=http%3A%2F%2Fdocs.nativescript.org%2Fstart%2Fgetting-started&p%5B) to impress your friends 😀.
183-
184-
> **TIP**:
185-
> * If you're curious about how NativeScript makes it possible to directly invoke iOS and Android APIs, you can read about [“How NativeScript Works”](http://developer.telerik.com/featured/nativescript-works/) on our blog.
186-
> * Remember that the [Groceries app's “angular-end” branch](https://github.com/NativeScript/sample-Groceries/tree/angular-end) has the final state of this tutorial. Feel free to refer back to it at any time.

0 commit comments

Comments
 (0)