<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by SC on Medium]]></title>
        <description><![CDATA[Stories by SC on Medium]]></description>
        <link>https://medium.com/@sc71?source=rss-df6458a63ba8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*mtvvVnJHv1g4g2uulzr97g.png</url>
            <title>Stories by SC on Medium</title>
            <link>https://medium.com/@sc71?source=rss-df6458a63ba8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 12:51:20 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sc71/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Making an Android Widget]]></title>
            <link>https://medium.com/@sc71/making-an-android-widget-5ec59f015532?source=rss-df6458a63ba8------2</link>
            <guid isPermaLink="false">https://medium.com/p/5ec59f015532</guid>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[SC]]></dc:creator>
            <pubDate>Fri, 11 Jan 2019 17:43:06 GMT</pubDate>
            <atom:updated>2019-01-11T17:43:06.138Z</atom:updated>
            <content:encoded><![CDATA[<p>In this tutorial, you will learn how to create an Android Widget that is a homescreen memo and give it a custom typeface.</p><p><strong>What is a widget?</strong></p><p>Widgets are smaller views of an application that can be placed on the home screen for faster access.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/338/1*x-uSfrkv65bW57agssX5Sg.png" /><figcaption>A home screen memo widget</figcaption></figure><p><strong>Making a Widget</strong></p><p>In the Android Studio menu, select File -&gt; New -&gt; Widget -&gt; App Widget. This will open up a configuration screen.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*BHQUImHmatYIgTG9ppLXsA.png" /></figure><ol><li>On this screen, name your widget MemoWidget under the Class Name prompt.</li><li>Widgets can be placed on the home screen or the lock screen. However, it can only be placed on the lock screen (called keyguard) for Android versions lower than 5.0.</li><li>Make your widget Not Resizable for a static memo size.</li><li>Determine the size of your widget with the Minimum Width and Minimum Height prompts.</li><li>Check Configuration Screen if you want a configuration activity for your widget. You will need to check this if you want to change the appearance of the widget based on user input. To follow this tutorial, check Configuration Screen, since we are displaying user inputted text on a memo.</li></ol><p>When you select finish, five things will be created for you: a Java class for the widget, a Java class for the configuration activity, a xml layout for the widget, a xml layout for the configuration activity, and a xml file for the widget details.</p><p><strong>Making the Widget Layout</strong></p><p>Open the memo_widget.xml, which can be found under app -&gt; res -&gt; layout in the project menu.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/520/1*Se_JTh-f1HEbdWQ15l7-Ng.png" /></figure><p>Go to the text of the layout. We’re going to change the TextView to an ImageView and change some of the settings, as shown below:</p><pre>&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;<br>    android:layout_width=&quot;match_parent&quot;<br>    android:layout_height=&quot;match_parent&quot;<br>    android:background=&quot;@android:color/transparent&quot;<br>    android:padding=&quot;@dimen/widget_margin&quot;&gt;<br><br><br>        &lt;<strong>ImageView</strong><br>            android:id=&quot;@+id/appwidget_text&quot;<br>            android:layout_width=&quot;wrap_content&quot;<br>            android:layout_height=&quot;wrap_content&quot;<br>            <strong>android:layout_marginEnd=&quot;8dp&quot;<br>            android:layout_marginBottom=&quot;8dp&quot;<br>            android:background=&quot;@drawable/memo1_4&quot;</strong><br>            <strong>android:padding=&quot;8dp&quot;<br>            android:paddingEnd=&quot;8dp&quot;<br>            android:paddingStart=&quot;100dp&quot;<br></strong>            android:contentDescription=&quot;@string/appwidget_text&quot;/&gt;<br><br><br>&lt;/RelativeLayout&gt;</pre><p>The padding is necessary for the specific background image used because I wanted the text to begin not flush against the left side. If you want your text against the left side, remove the padding.</p><p>The background should be set to a drawable that you want to be the background of your memo.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/365/1*Xy-D96dP_pUeBtDlwCar6g.png" /><figcaption>Finished widget layout</figcaption></figure><p><strong>Custom Typeface on Memo</strong></p><p>To create a custom typeface, you must first create an assets folder under the app folder in the Project window. Next, in the assets folder, create a directory called fonts. Then copy paste the ttf file of your font into the fonts folder.</p><p>For widgets, the only way to use a custom typeface is to draw the text onto a canvas with a DynamicLayout.</p><p>Under the MemoWidget class, delete the view.setTextViewText(…) from the updateAppWidget method.</p><pre><strong>private static DynamicLayout <em>textLayout</em>;</strong></pre><pre>static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,<br>                            int appWidgetId) {<br><br>    CharSequence widgetText = MemoWidgetConfigureActivity.<em>loadTitlePref</em>(context, appWidgetId);<br>    <strong>String widgetTextString = widgetText.toString();</strong><br>    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.<em>memo_widget</em>);<br><br>    <strong>Bitmap bitmap = Bitmap.<em>createBitmap</em>(168, 84, Bitmap.Config.<em>ARGB_8888</em>);<br>    Canvas canvas = new Canvas(bitmap);<br>    TextPaint textPaint = new TextPaint();<br>    Typeface typeface = Typeface.<em>createFromAsset</em>(context.getAssets(), &quot;fonts/indieflower.ttf&quot;);<br><br>    textPaint.setTypeface(typeface);<br>    textPaint.setAntiAlias(true);<br>    textPaint.setTextSize(19);<br>    <em>textLayout </em>= new DynamicLayout(widgetTextString, textPaint, canvas.getWidth(), Layout.Alignment.<em>ALIGN_NORMAL</em>, 1.0f, 0.0f, false);<br><br>    int textX = 0;<br>    int textY = 0;<br><br>    canvas.translate(textX,textY);<br>    <em>textLayout</em>.draw(canvas);</strong><br><strong>    views.setImageViewBitmap(R.id.<em>appwidget_text</em>, bitmap);<br></strong>    appWidgetManager.updateAppWidget(appWidgetId, views);<br>}</pre><p>Create a private static DynamicLayout called textLayout. Create a string variable called widgetTextString, and set it to widgetText.toString. This will be used later as an argument for the creation of the DynamicLayout.</p><p>We will create a Bitmap for the canvas, the TextPaint, then a Typeface. Next we will set the TextPaint to our Typeface. AntiAlias must be true to smooth the text. We will also set the text size of the TextPaint. Initialize the textLayout using the user-inputted text and the textPaint we created.</p><p>We will create an x and y value for the canvas location. We will translate the canvas to those values, then draw the text onto the canvas. Finally we will set the ImageView to our bitmap.</p><p>When the code is run, you will have a working memo widget with a custom font!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5ec59f015532" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Android NumberPickers]]></title>
            <link>https://medium.com/@sc71/android-numberpickers-3ef535c45487?source=rss-df6458a63ba8------2</link>
            <guid isPermaLink="false">https://medium.com/p/3ef535c45487</guid>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[SC]]></dc:creator>
            <pubDate>Fri, 26 Oct 2018 15:32:43 GMT</pubDate>
            <atom:updated>2018-10-30T16:16:12.989Z</atom:updated>
            <content:encoded><![CDATA[<p>In this tutorial you will learn how to create NumberPicker widgets in Android Studio with custom values and how to retrieve NumberPicker values.</p><h4><strong>What are NumberPickers?</strong></h4><p>NumberPickers are a widget avalible in Android Studio. They are a set of values selectable by either a horizontal or vertical scrolling wheel.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/626/1*Zb-EtXoyD8yDbonmY-YYlA.gif" /></figure><h4>Creating a NumberPicker</h4><p>To create a NumberPicker, go to the XML file of your main activity. Switch from the Design tab to the Text tab and type &lt;NumberPicker. As you type this, Android Studio will suggest a NumberPicker. Press tab to create one, then set the width and height to wrap_content.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/626/1*j_hexr1fHtp7RhbsAum6BA.gif" /></figure><p>When you switch back to the Design tab, the NumberPicker should be visible. The NumberPicker in the XML will look different from the NumberPicker in the app. Under ID, name the NumberPicker with the what_where_why naming scheme. In this case it will be numberpicker_main_picker. Don’t forget to constrain your NumberPicker! For this example the picker is centered horizontally and vertically.</p><p>In your MainActivity, create a NumberPicker variable and wire it to the NumberPicker you created in your XML.</p><pre>private NumberPicker picker1;<br>@Override<br>protected void onCreate(Bundle savedInstanceState) {<br>    super.onCreate(savedInstanceState);<br>    setContentView(R.layout.<em>activity_main</em>);<br><strong>    picker1 = findViewById(R.id.<em>numberpicker_main_picker</em>);<br></strong>}</pre><h4>Setting Custom Values</h4><p>To set custom values on your NumberPicker, first set a minimum and maximum value. In this example we will have 5 values, so we will set the range to:</p><pre>picker1.setMaxValue(4);<br>picker1.setMinValue(0);</pre><p>A NumberPicker takes an array of Strings as its values. First create an instance variable of a string array, then initialize the array with your desired values.</p><pre>private String[] pickerVals;</pre><pre>.......</pre><pre>pickerVals = new String[] {&quot;dog&quot;, &quot;cat&quot;, &quot;lizard&quot;, &quot;turtle&quot;, &quot;axolotl&quot;};</pre><p>Next, set the NumberPicker values to these Strings with .setDisplayedValues()</p><pre>picker1.setDisplayedValues(pickerVals);</pre><p>When the program is run, it should look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/572/1*ZkCfNzu-pIrWgpBQeHYQIg.gif" /></figure><p><em>*note: the best way to format this code is to handle it under an initializePicker(); method, then run the method in onCreate. However, for the sake of simplicity in this tutorial, I will not be doing this.*</em></p><h4>Retrieving NumberPicker Values</h4><p>To get the value of your NumberPicker every time it is changed, create an onValueChangeListener in onCreate. In the overriden onValueChange method, create an int variable to store the picker value. Next, make a Log.d to log what the picker value is.</p><pre>picker1.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {<br>    @Override<br>    public void onValueChange(NumberPicker numberPicker, int i, int i1) {<br>        <strong>int valuePicker1 = picker1.getValue();<br>        Log.<em>d</em>(&quot;picker value&quot;, valuePicker1 + &quot;&quot;);</strong><br>    }<br>});</pre><p>With the code we currently have, the NumberPicker value will be an int. To retrieve the String value of the picker we must access the String array storing our picker values. Rather than just logging the value of picker1, we must log pickerVals[valuePicker1].</p><pre>Log.<em>d</em>(&quot;picker value&quot;, pickerVals[valuePicker1]);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/394/1*r44BBytGvtm5mG1Q3It2bQ.gif" /><figcaption>Looking at the log below you can see that the appearing animal names are corresponding to the picker value.</figcaption></figure><h4>Conclusion</h4><p>Here is the resulting code of the project:</p><pre>package com.example.numberpickerexample;<br><br>import android.support.v7.app.AppCompatActivity;<br>import android.os.Bundle;<br>import android.util.Log;<br>import android.widget.NumberPicker;<br><br>public class MainActivity extends AppCompatActivity {<br><br>    private NumberPicker picker1;<br>    private String[] pickerVals;<br>    @Override<br>    protected void onCreate(Bundle savedInstanceState) {<br>        super.onCreate(savedInstanceState);<br>        setContentView(R.layout.<em>activity_main</em>);<br>        <strong>picker1 = findViewById(R.id.<em>numberpicker_main_picker</em>);<br>        picker1.setMaxValue(4);<br>        picker1.setMinValue(0);<br>        pickerVals  = new String[] {&quot;dog&quot;, &quot;cat&quot;, &quot;lizard&quot;, &quot;turtle&quot;, &quot;axolotl&quot;};<br>        picker1.setDisplayedValues(pickerVals);<br><br>        picker1.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {<br>            @Override<br>            public void onValueChange(NumberPicker numberPicker, int i, int i1) {<br>                int valuePicker1 = picker1.getValue();<br>                Log.<em>d</em>(&quot;picker value&quot;, pickerVals[valuePicker1]);<br>            }<br>        });</strong><br><br>    }<br>}</pre><p>And the XML file:</p><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br>&lt;android.support.constraint.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;<br>    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;<br>    xmlns:tools=&quot;http://schemas.android.com/tools&quot;<br>    android:layout_width=&quot;match_parent&quot;<br>    android:layout_height=&quot;match_parent&quot;<br>    tools:context=&quot;.MainActivity&quot;&gt;<br><br>    <strong>&lt;NumberPicker<br>        android:id=&quot;@+id/numberpicker_main_picker&quot;<br>        android:layout_width=&quot;wrap_content&quot;<br>        android:layout_height=&quot;wrap_content&quot;<br>        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;<br>        app:layout_constraintEnd_toEndOf=&quot;parent&quot;<br>        app:layout_constraintHorizontal_bias=&quot;0.5&quot;<br>        app:layout_constraintStart_toStartOf=&quot;parent&quot;<br>        app:layout_constraintTop_toTopOf=&quot;parent&quot;&gt;&lt;/NumberPicker&gt;</strong><br>&lt;/android.support.constraint.ConstraintLayout&gt;</pre><p><strong>Good luck!</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3ef535c45487" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>