Jul 20, 2016

Load Spinner with string-array from resources xml


To define string-array in resources xml, edit values/strings.xml to add items of string-array.
<resources>
    <string name="app_name">AndroidSpinner</string>
    <string-array name="weekday">
        <item>Sunday</item>
        <item>Monday</item>
        <item>Tuesday</item>
        <item>Wednesday</item>
        <item>Thursday</item>
        <item>Friday</item>
        <item>Saturday</item>
    </string-array>
</resources>


Edit layout/activity_main.xml to add Spinner with entries to load from string-array.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.androidspinner.MainActivity">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android-coding.blogspot.com"
        android:textSize="28dp"
        android:textStyle="bold" />
    <Spinner
        android:id="@+id/myspinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/weekday"/>
</LinearLayout>


MainActivity.java
package com.example.androidspinner;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Spinner mySpinner = (Spinner)findViewById(R.id.myspinner);
        mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                String item = (String)adapterView.getItemAtPosition(i);
                Toast.makeText(MainActivity.this, item, Toast.LENGTH_LONG).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }
}

Mar 4, 2016

Get the number of processor cores available to the VM

The method Runtime.availableProcessors() returns the number of processor cores available to the VM, at least 1. Traditionally this returned the number currently online, but many mobile devices are able to take unused cores offline to save power, so releases newer than Android 4.2 (Jelly Bean) return the maximum number of cores that could be made available if there were no power or heat constraints.


package example.com.androidprocessors;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        int availableProcessors = Runtime.getRuntime().availableProcessors();
        Toast.makeText(MainActivity.this,
                "Available Processors: " + availableProcessors,
                Toast.LENGTH_LONG).show();
    }
}

Jan 16, 2016

Android Textual Layout (Android Dev Summit 2015)



Recent versions of Android have significant advances in typographic sophistication, including automatic hyphenation, balanced and optimized paragraph layout, OpenType features, support for dozens of scripts around the world, and more. Raph Levien, software engineer and tech lead of Android Text on the Android UI Toolkit team, covers these capabilities and how apps can make best use of them.

View presentation slides here: https://speakerdeck.com/raphlinus/android-textual-layout


Nov 23, 2015

What’s New in Android Studio 1.5

Android Studio 1.5 is focused on delivering more stability, with most of the enhancements being made under the hood. It adds new lint checks, the ability to use short names when code-completing custom views, and the memory profiler can now help you detect some of the most commonly known causes of leaked activities.

Android Studio 1.5 is now available to download from the stable release channel.

Find out more from Android Developers Blog: http://goo.gl/oIbJHO

Nov 9, 2015

What is Cardboard

What is Cardboard and Virtual Reality. This video introduces Cardboard for developers and how to use a phone and a simple box of Cardboard to tap into a new type of immersion with virtual reality.

Watch more episodes of Cardboard here:
https://goo.gl/IAoGGs



Cardboard: How Cardboard Works

Nov 5, 2015

Google Play services 8.3

Google Play services 8.3 is now out enabling you to build better apps with new functionality for: Sign In, Fused Location Provider, App Invites, and the Wearable Data Layer APIs.

Android and Android Studio: Getting Started


Learn how to get started with Android and Android Studio in this short tutorial. It demontrates how to install Android Studio (Google’s official Android IDE) and create your first Android app. You’ll learn how to download the Java SDK, download and install Android Studio, create a new “Hello World” project, and run your app on an emulator and real Android device.

You’ll also learn a series of Protips from an Android app startup as they go through the process of developing their app in a highly stressful environment. With over 1 billion Android devices already activated, Android represents an incredible opportunity for developers. Installing Android Studio is your first step!

Download the Java Development Kit: http://goo.gl/zXjC
Download Android Studio: http://goo.gl/2qpr
Android USB Drivers for Windows: http://goo.gl/91Y8C

Once you’ve installed Android Studio, learn more about developing Android Apps using these resources:
Android Developer Documentation: http://goo.gl/km7ab
Developing Android Apps Udacity Online Training: https://goo.gl/u1pxZv
Android Design for Developers Udacity Online Training: https://goo.gl/7W2S28

Check out more music from the composer: www.terramonk.com

Oct 15, 2015

Interactive flip ImageView using ObjectAnimator

User touch on buttons to flip the ImageView forward/backward alternatively, around X-axis and Y-axis.


package com.example.androidflipview;

import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    Button buttonFlipX, buttonFlipY;
    ImageView imageView;
    boolean dirX = true;
    boolean dirY = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = (ImageView)findViewById(R.id.image);
        buttonFlipX = (Button)findViewById(R.id.buttonflipX);
        buttonFlipY = (Button)findViewById(R.id.buttonflipY);

        buttonFlipX.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                if(dirX){
                    dirX = false;
                    buttonFlipX.setText("Flip X Backward");
                    ObjectAnimator flip = ObjectAnimator.ofFloat(imageView, "rotationX", 0f, 180f);
                    flip.setDuration(500);
                    flip.start();
                }else{
                    dirX = true;
                    buttonFlipX.setText("Flip X Forward");
                    ObjectAnimator flip = ObjectAnimator.ofFloat(imageView, "rotationX", 180f, 0f);
                    flip.setDuration(1000);
                    flip.start();
                }
            }
        });

        buttonFlipY.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                if(dirY){
                    dirY = false;
                    buttonFlipY.setText("Flip Y Backward");
                    ObjectAnimator flip = ObjectAnimator.ofFloat(imageView, "rotationY", 0f, 180f);
                    flip.setDuration(2000);
                    flip.start();
                }else{
                    dirY = true;
                    buttonFlipY.setText("Flip Y Forward");
                    ObjectAnimator flip = ObjectAnimator.ofFloat(imageView, "rotationY", 180f, 0f);
                    flip.setDuration(3000);
                    flip.start();
                }
            }
        });
    }
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
a
    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android-coding.blogspot.com"
        android:textSize="28dp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/buttonflipX"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Flip X Forward" />
    <Button
        android:id="@+id/buttonflipY"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Flip Y Forward" />

    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@mipmap/ic_launcher" />
</LinearLayout>


Sep 14, 2015

Get Started AdMob for Android in Android Studio

AdMob uses the Google Mobile Ads SDK. This guide will show you how to integrate the Google Mobile Ads SDK into a brand new app and use it to display a simple banner ad. It should take about thirty minutes to complete and will give you a good sense of how the SDK functions within an app. If you're new to Google Mobile Ads, this is a great place to start before moving on to more advanced examples.

https://developers.google.com/admob/android/quick-start


Sep 9, 2015

Android Asset Studio

Android Asset Studio is a set of web-based tools for generating graphics and other assets that would eventually be in an Android application's res/ directory.



See the source on GitHub.

Sep 4, 2015

Google Android Developers Channel in YouTube

The home for videos, demos, tutorials, interviews, and anything else related to Android development. Android Developers channel at Youtube: https://www.youtube.com/user/androiddevelopers

Aug 8, 2015

Re-using Bitmaps

In modern mobile applications, Bitmaps can account for a large amount of memory churn. Constantly loading thumbnails, user icons, and Emoji sets can provide your users with a steady stream of media, but it can also contribute to some HUGE pauses for garbage collection. 

In this video Colt McAnlis, and for temporally allocated bitmaps, there’s a handy trick that you absolutely should be using to escape these performance problems: Re-using bitmaps.

Every time you allocate a bitmap, you have to incur some overhead to allocate the objects from the heap, which is less than ideal if you’ve got a lot of bitmaps. Rather than banging on the heap for new objects each time, you can instead, reuse the memory that an existing bitmap has created, and load your image there.

The end result? Less memory churn from bitmaps.


Pre-scaling Bitmaps

For media rich applications, BITMAPS are everywhere. But these high-resolution images can cause a horde of performance problems if the size of the image in memory is larger than the size you’re displaying it on screen. As such one of the most important things you can do to alleviate memory pressure in your app, is resizing your bitmaps.

Rather than writing all your own image resizing code, Android has a set of APIs which can do all this work for you. But the trick is, knowing which one to use?

For example, inSampleSize is the fastest way to down-scale your image; But you can only make it smaller by some factor of your image. createScaledBitmap is a great API, but requires an extra memory allocation to get it done.

Thankfully, Colt McAnlis covers all these topics (and more) in this video, helping you reduce your memory footprint, and get some smaller images.

Jul 5, 2015

Create custom text style

Example to create custom text style:


Modify /res/values/styles.xml to create our custom text style "LargeRedText", "InverseMediumBlueText", "GreenText", "ItalicGrayText" and "Bold50BlackText".
<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>
    
    <style name="LargeRedText" parent="@android:style/TextAppearance.Large">
        <item name="android:textColor">#FF0000</item>
    </style>
    <style name="InverseMediumBlueText" parent="@android:style/TextAppearance.Medium.Inverse">
        <item name="android:background">#0000FF</item>
    </style>
    <style name="GreenText" parent="@android:style/TextAppearance">
        <item name="android:textColor">#00FF00</item>
    </style>
    <style name="ItalicGrayText" parent="@android:style/TextAppearance">
        <item name="android:textColor">#A0A0A0</item>
        <item name="android:textStyle">italic</item>
    </style>
    <style name="Bold50BlackText">
        <item name="android:textColor">#000000</item>
        <item name="android:textStyle">bold</item>
        <item name="android:textSize">50dp</item>
    </style>

</resources>


Example to use our custom text style in layout xml.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.androidtextappearance.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android-coding.blogspot.com"
        android:textSize="24dp"
        android:textStyle="bold" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="textAppearance"
        android:textAppearance="?android:textAppearance" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="textAppearanceLarge"
        android:textAppearance="?android:textAppearanceLarge" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="textAppearanceMedium"
        android:textAppearance="?android:textAppearanceMedium" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="textAppearanceSmall"
        android:textAppearance="?android:textAppearanceSmall" />
    
 <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="custom style LargeRedText"
        style="@style/LargeRedText" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="custom style InverseMediumBlueText"
        style="@style/InverseMediumBlueText" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="custom style GreenText"
        style="@style/GreenText" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="custom style ItalicGrayText"
        style="@style/ItalicGrayText" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="custom style Bold50BlackText"
        style="@style/Bold50BlackText" />
    
</LinearLayout>


Example to set textAppearance in XML

Example to set textAppearance in XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.androidtextappearance.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android-coding.blogspot.com"
        android:textSize="24dp"
        android:textStyle="bold" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="textAppearance"
        android:textAppearance="?android:textAppearance" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="textAppearanceLarge"
        android:textAppearance="?android:textAppearanceLarge" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="textAppearanceMedium"
        android:textAppearance="?android:textAppearanceMedium" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="textAppearanceSmall"
        android:textAppearance="?android:textAppearanceSmall" />
    
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="textAppearanceInverse"
        android:textAppearance="?android:textAppearanceInverse" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="textAppearanceLargeInverse"
        android:textAppearance="?android:textAppearanceLargeInverse" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="textAppearanceMediumInverse"
        android:textAppearance="?android:textAppearanceMediumInverse" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="textAppearanceSmallInverse"
        android:textAppearance="?android:textAppearanceSmallInverse" />

 <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/darker_gray"
        android:text="textAppearanceInverse"
        android:textAppearance="?android:textAppearanceInverse" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/darker_gray"
        android:text="textAppearanceLargeInverse"
        android:textAppearance="?android:textAppearanceLargeInverse" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/darker_gray"
        android:text="textAppearanceMediumInverse"
        android:textAppearance="?android:textAppearanceMediumInverse" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/darker_gray"
        android:text="textAppearanceSmallInverse"
        android:textAppearance="?android:textAppearanceSmallInverse" />
    
</LinearLayout>



Jun 1, 2015

100 Days of Google Dev


Starting now, Google are releasing 100 developer videos over 100 days, covering from Chrome to Android. Subscribe now: http://goo.gl/mQyv5L


May 17, 2015

How Android OTA Updates work

Presentation at Embedded Linux Conference 2015 by Andrew Boie from Intel Corporation.

AOSP is distributed with the source code and tools for full (whole image) and incremental (binary patch) secure over-the-air (OTA) software updates, specifically an alternate boot target Recovery Console, the updater logic itself, and tools to create software updates. There is no publicly available documentation for how this mechanism is supposed to be integrated. This presentation gives a detailed end-to-end description of how software updates are created, digitally signed, and applied to the device. It includes a discussion on the plug-in architecture and Edify language which allows builders to customize the OTA updates with platform-specific features. This is an updated version of a talk presented at ABS in 2012, with details on new OTA features including block-level OTA updates in Lollipop.

Speakers
Andrew Boie
Intel Corporation
Andrew Boie is a software engineer and scrum master for the Intel Android-IA project hosted on 01.org, which aims to support Android on Intel Core and Atom platforms. Prior to working at Intel Andrew worked for Garmin International as an engineering team lead on Android Eclair-based Nuvifone projects. He spoke at ABS 2011 on the topic of Android OTA Software Updates.


May 12, 2015

Google Play Services 7.3

Google Play Services 7.3 brings a ton of great new features to help you BUILD BETTER APPS! This update brings the ability to connect multiple wearables simultaneously to a single phone.

There are also some great new updates to Google Fit, including nutrition types, and to Location.


May 6, 2015

Older YouTube apps is no longer be supported



If you see this video in your YouTube app’s video feeds, your device is affected. (Note: If the video appears unexpectedly in a web browser, it means that website is using an outdated method to access YouTube videos.)

As Google upgrade the YouTube Data API to bring more features, the old version will be begin shutting down on April 20, 2015. This will result in the current YouTube app not working on certain device models from 2012 and older.

https://youtube.com/devicesupport


May 4, 2015

Using VideoView to play mp4, with MediaController enable/disable

Example to play mp4 on SDCard/ExternalStorage using android.widget.VideoView. You can also enable/disable the MediaController, contains the buttons like "Play/Pause", "Rewind", "Fast Forward" and a progress slider, by calling VideoView.setMediaController().

With the MediaController set, tap on the video (or the VideoView) to display the controller.


Example code:
package com.example.androidvideoview;

import java.io.File;

import android.support.v7.app.ActionBarActivity;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.widget.VideoView;
import android.os.Bundle;
import android.os.Environment;

public class MainActivity extends ActionBarActivity {
 
 ToggleButton enableMediaController;
 VideoView myVideoView;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  enableMediaController = (ToggleButton)findViewById(R.id.enableMediaController);
  myVideoView = (VideoView)findViewById(R.id.myvideoview);
  myVideoView.setVideoPath(getViewSrc());
  myVideoView.requestFocus();
  myVideoView.start();
  
  setMediaController();
  
  enableMediaController.setOnCheckedChangeListener(new OnCheckedChangeListener(){

   @Override
   public void onCheckedChanged(CompoundButton buttonView,
     boolean isChecked) {
    setMediaController();
   }});
 }
 
 private void setMediaController(){
  if(enableMediaController.isChecked()){
   myVideoView.setMediaController(new MediaController(this));
  }else{
   myVideoView.setMediaController(null);
  }
 }

 private String getViewSrc(){
  File extStorageDirectory = Environment.getExternalStorageDirectory();
  String s = extStorageDirectory.getAbsolutePath() + "/test.mp4";
  Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
  return s;
 }
}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.androidvideoview.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android-coding.blogspot.com"
        android:textSize="24dp"
        android:textStyle="bold" />

    <ToggleButton
        android:id="@+id/enableMediaController"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textOn="Disable MediaController"
        android:textOff="Enable MediaController"
        android:checked="true"/>
    <VideoView
        android:id="@+id/myvideoview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

To play the file in SD Card, <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> is needed in AndroidManifest.xml, otherwise "Can't play this video".


Infolinks In Text Ads