Wednesday, 22 June 2016

Samsung Galaxy Note 5 review

Samsung Galaxy Note 5 review

This is the best Galaxy Note to date — and a great overall phone — even with the loss of a couple of major features.

The quick take

Samsung has brought the once-oversized Galaxy Note closer in line with the more traditional Galaxy S line in 2015, and that's not necessarily a bad thing. Cleaner software, a new-and-improved hardware design and the industry-leading S Pen add up to a wonderful phone, even though some will bemoan the loss of a removable battery and SD card slot. The Note 5 has one of the best screens available in a phone, is easier to hold thanks to the new hardware design and the internals offer a significant performance boost over the Note 4 — and even so, battery life is a non-issue. This is a Note made for even more people to enjoy, and it's a really great choice if you want a large screen.

THE GOOD

  • Great new design
  • Easier to hold than previous Notes
  • Fantastic camera
  • Refined S Pen experience

THE BAD

  • Underwhelming speaker
  • Glass back still somewhat slippery
  • Seriously expensive
  • May cause Galaxy S buyer's remorse

Tuesday, 21 June 2016

Nexus 5X, three months on

Nexus 5X, three months on


The lesser of the 2015 Nexus phones is a bit of a mixed bag, even after three months of use.
After doing a roundup of our thoughts on the Nexus 6P after three monthsof use, we wanted to offer the same treatment to the Nexus 5X. Though it doesn't have the power or draw of the high-end 6P, the Nexus 5X has a considerable following — due in part to the nostalgia factor of its ties to the original Nexus 5, its smaller screen and more affordable price.
Plenty of folks bought the Nexus 5X, including several of us here atAndroid Central, and now after three months with the phone in our hands it's time to reflect a bit and see how we're getting on with the phone.

Here's when your Nexus phone or tablet will stop receiving Android updates

Here's when your Nexus phone or tablet will stop receiving Android updates

Last year, Google said that it will roll out monthly security updates to Nexus phones and tablets for three years from their respective launch dates, and platform updates for "at least two years." The search giant is now listing concrete timelines for when support will end for each Nexus phone and tablet, including the Nexus 6P and 5X:
DeviceNo guaranteed Android version updates after
Nexus 5XSeptember 2017
Nexus 6PSeptember 2017
Nexus 9October 2016
Nexus 6October 2016
Nexus 5October 2015
Nexus 7 (2013)July 2015
Nexus 10November 2014
The Nexus 6P and 5X will continue to get updated until September 2017, which is when we should we be starting to see Android O. Google has also talked about software updates for Android Onephones, stating:
All partners have committed to giving software updates for at least 18 months after the phone's launch. All Android One phones will get at least one major software update and several smaller security updates.

Zagg's Glass Curve screen protectors now available for Galaxy S7 and S7 edge

If you're looking for a good way to protect the screen of your Samsung Galaxy S7 or S7 edge, Zagg's Curved Glass screen protectors are now available for purchase.
Zagg's Glass Curve screen protectors now available for Galaxy S7 and S7 edge
The Curved Glass screen protectors from Zagg offer up protection for the full front of either device, with tempered glass running from the bezels to the screen itself. Zagg says that the protectors also feature easy application tabs, which should make fitting the glass to your screen an easy task.
Interested? If so, you can grab the Glass Curve for the Galaxy S7 for $44.99, or the Galaxy S7 edge for $54.99, at the link below.

Sony stops making smartphones in Brazil

Sony stops making smartphones in Brazil


Sony has announced that it will no longer manufacture smartphones in Brazil. Although the vendor invested R$250m ($83m) last year to expand its mobile unit in the country, the end of tax exemptions for locally manufactured smartphones that cost up to R$1,500 ($530) has motivated the brand to move away from local production.
Instead of continuing with Foxconn and Arima for local manufacturing, Sony will now import products — like the Xperia X and XA — from China and Thailand.
Sony's marketing director Ana Peretti confirmed the changes to local news outlet G1 (via ZDNet):
The law of the well was suspended and we only have products over R $ 1.8 billion, so we decided to import these models.
Peretti also mentioned that the changes would result in a more flexible distribution system. Sony isn't the only vendor reconsidering its Brazilian strategy, as Xiaomi stated earlier this month that it would not bring any new phones to the country. The Chinese vendor stated that it would not leave the market — its first outside of Asia, only that it wouldn't launch devices like the Mi 5 or the more recent Mi Max to the country:

Wednesday, 15 June 2016

Android Example of ViewPager with custom PagerAdapter - By Naman Tiwari

Example of ViewPager with custom PagerAdapter

This example show how to implement ViewPager with custom PagerAdapter.

Modify activity_main.xml to include android.support.v4.view.ViewPager in layout.
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.androidviewpagerapp.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />
    
    <android.support.v4.view.ViewPager
        android:id="@+id/myviewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
        
</LinearLayout>

MainActivity.java
package com.example.androidviewpagerapp;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;

public class MainActivity extends Activity {

 ViewPager viewPager;
 MyPagerAdapter myPagerAdapter;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  viewPager = (ViewPager)findViewById(R.id.myviewpager);
  myPagerAdapter = new MyPagerAdapter();
  viewPager.setAdapter(myPagerAdapter);

 }
 
 private class MyPagerAdapter extends PagerAdapter{
  
  int NumberOfPages = 5;
  
  int[] res = { 
   android.R.drawable.ic_dialog_alert,
   android.R.drawable.ic_menu_camera,
   android.R.drawable.ic_menu_compass,
   android.R.drawable.ic_menu_directions,
   android.R.drawable.ic_menu_gallery};
  int[] backgroundcolor = { 
   0xFF101010,
   0xFF202020,
   0xFF303030,
   0xFF404040,
   0xFF505050};

  @Override
  public int getCount() {
   return NumberOfPages;
  }

  @Override
  public boolean isViewFromObject(View view, Object object) {
   return view == object;
  }

  @Override
  public Object instantiateItem(ViewGroup container, int position) {
   
      
      TextView textView = new TextView(MainActivity.this);
      textView.setTextColor(Color.WHITE);
      textView.setTextSize(30);
      textView.setTypeface(Typeface.DEFAULT_BOLD);
      textView.setText(String.valueOf(position));
      
      ImageView imageView = new ImageView(MainActivity.this);
      imageView.setImageResource(res[position]);
      LayoutParams imageParams = new LayoutParams(
        LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
      imageView.setLayoutParams(imageParams);
      
      LinearLayout layout = new LinearLayout(MainActivity.this);
      layout.setOrientation(LinearLayout.VERTICAL);
      LayoutParams layoutParams = new LayoutParams(
        LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
      layout.setBackgroundColor(backgroundcolor[position]);
      layout.setLayoutParams(layoutParams);
      layout.addView(textView);
      layout.addView(imageView);
      
      final int page = position;
      layout.setOnClickListener(new OnClickListener(){

    @Override
    public void onClick(View v) {
     Toast.makeText(MainActivity.this, 
      "Page " + page + " clicked", 
      Toast.LENGTH_LONG).show();
    }});
      
      container.addView(layout);
      return layout;
  }

  @Override
  public void destroyItem(ViewGroup container, int position, Object object) {
   container.removeView((LinearLayout)object);
  }

 }

}

Thursday, 2 June 2016

Android Extract Prominent Colors from an Image, using Palette class

Extract Prominent Colors from an Image, using Palette class



The Android Support Library r21 and above includes the Palette class, which lets you extract prominent colors from an image. This class extracts the following prominent colors:
- Vibrant
- Vibrant dark
- Vibrant light
- Muted
- Muted dark
- Muted light

reference: http://developer.android.com/training/material/drawables.html#ColorExtract

This example show how to load photos, and get Prominent Colors using Palette class.
(Actually I don't know what the Prominent Colors means!)

To use the Palette class in your project, add the following Gradle dependency to your app's module:
dependencies {
    ...
    compile 'com.android.support:palette-v7:xx.x.x'
}


com.blogspot.android_er.androidpalette.MainActivity.java
package com.blogspot.android_er.androidpalette;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.graphics.Palette;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.FileNotFoundException;

public class MainActivity extends AppCompatActivity {

    Button buttonOpen;
    TextView textUri;
    ImageView imageView;
    TextView textVibrant, textVibrantDark, textVibrantLight;
    TextView textMuted, textMutedDark, textMutedLight;
    View viewVibrant, viewVibrantDark, viewVibrantLight;
    View viewMuted, viewMutedDark, viewMutedLight;

    private static final int RQS_OPEN_IMAGE = 1;

    Uri targetUri = null;

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

        textUri = (TextView) findViewById(R.id.texturi);
        imageView = (ImageView) findViewById(R.id.image);
        buttonOpen = (Button) findViewById(R.id.btnopen);
        buttonOpen.setOnClickListener(buttonOpenOnClickListener);

        textVibrant = (TextView)findViewById(R.id.textVibrant);
        textVibrantDark = (TextView)findViewById(R.id.textVibrantDark);
        textVibrantLight = (TextView)findViewById(R.id.textVibrantLight);
        textMuted = (TextView)findViewById(R.id.textMuted);
        textMutedDark = (TextView)findViewById(R.id.textMutedDark);
        textMutedLight = (TextView)findViewById(R.id.textMutedLight);

        viewVibrant = (View)findViewById(R.id.viewVibrant);
        viewVibrantDark = (View)findViewById(R.id.viewVibrantDark);
        viewVibrantLight = (View)findViewById(R.id.viewVibrantLight);
        viewMuted = (View)findViewById(R.id.viewMuted);
        viewMutedDark = (View)findViewById(R.id.viewMutedDark);
        viewMutedLight = (View)findViewById(R.id.viewMutedLight);
    }

    View.OnClickListener buttonOpenOnClickListener =
            new View.OnClickListener() {

                @TargetApi(Build.VERSION_CODES.KITKAT)
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();

                    if (Build.VERSION.SDK_INT >=
                            Build.VERSION_CODES.KITKAT) {
                        intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
                    } else {
                        intent.setAction(Intent.ACTION_GET_CONTENT);
                    }

                    intent.addCategory(Intent.CATEGORY_OPENABLE);

                    // set MIME type for image
                    intent.setType("image/*");

                    startActivityForResult(intent, RQS_OPEN_IMAGE);
                }

            };

    @TargetApi(Build.VERSION_CODES.KITKAT)
    @Override
    protected void onActivityResult(int requestCode,
                                    int resultCode, Intent data) {

        if (resultCode == Activity.RESULT_OK) {

            Uri dataUri = data.getData();

            if (requestCode == RQS_OPEN_IMAGE) {
                targetUri = dataUri;
                textUri.setText(dataUri.toString());
                updatImage(dataUri);
            }
        }

    }

    private void updatImage(Uri uri){

        if (uri != null){
            Bitmap bm;
            try {
                bm = BitmapFactory.decodeStream(
                        getContentResolver()
                                .openInputStream(uri));
                imageView.setImageBitmap(bm);

                extractProminentColors(bm);

            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    //extract prominent colors
    /*
    compile 'com.android.support:palette-v7:23.0.1'
    is needed in Gradle dependency
     */
    private void extractProminentColors(Bitmap bitmap){
        int defaultColor = 0x000000;

        Palette p = Palette.from(bitmap).generate();

        int VibrantColor = p.getVibrantColor(defaultColor);
        textVibrant.setText("VibrantColor: " + String.format("#%X", VibrantColor));
        viewVibrant.setBackgroundColor(VibrantColor);

        int VibrantColorDark = p.getDarkVibrantColor(defaultColor);
        textVibrantDark.setText("VibrantColorDark: " + String.format("#%X", VibrantColorDark));
        viewVibrantDark.setBackgroundColor(VibrantColorDark);

        int VibrantColorLight = p.getLightVibrantColor(defaultColor);
        textVibrantLight.setText("VibrantColorLight: " + String.format("#%X", VibrantColorLight));
        viewVibrantLight.setBackgroundColor(VibrantColorLight);

        int MutedColor = p.getMutedColor(defaultColor);
        textMuted.setText("MutedColor: " + String.format("#%X", MutedColor));
        viewMuted.setBackgroundColor(MutedColor);

        int MutedColorDark = p.getDarkMutedColor(defaultColor);
        textMutedDark.setText("MutedColorDark: " + String.format("#%X", MutedColorDark));
        viewMutedDark.setBackgroundColor(MutedColorDark);

        int MutedColorLight = p.getLightMutedColor(defaultColor);
        textMutedLight.setText("MutedColorLight: " + String.format("#%X", MutedColorLight));
        viewMutedLight.setBackgroundColor(MutedColorLight);

    }

}


layout/activity_main.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:padding="16dp"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold"/>
    <Button
        android:id="@+id/btnopen"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Load image"/>
    
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/texturi"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <ImageView
                android:id="@+id/image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"/>

            <TextView
                android:id="@+id/textVibrant"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Vibrant"/>
            <View
                android:id="@+id/viewVibrant"
                android:layout_width="match_parent"
                android:layout_height="25dp"/>

            <TextView
                android:id="@+id/textVibrantDark"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="VibrantDark"/>
            <View
                android:id="@+id/viewVibrantDark"
                android:layout_width="match_parent"
                android:layout_height="25dp"/>

            <TextView
                android:id="@+id/textVibrantLight"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="VibrantLight"/>
            <View
                android:id="@+id/viewVibrantLight"
                android:layout_width="match_parent"
                android:layout_height="25dp"/>

            <TextView
                android:id="@+id/textMuted"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Muted"/>
            <View
                android:id="@+id/viewMuted"
                android:layout_width="match_parent"
                android:layout_height="25dp"/>

            <TextView
                android:id="@+id/textMutedDark"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="MutedDark"/>
            <View
                android:id="@+id/viewMutedDark"
                android:layout_width="match_parent"
                android:layout_height="25dp"/>

            <TextView
                android:id="@+id/textMutedLight"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="MutedLight"/>
            <View
                android:id="@+id/viewMutedLight"
                android:layout_width="match_parent"
                android:layout_height="25dp"/>

        </LinearLayout>
    </ScrollView>
</LinearLayout>

BOOK - Android UI Design

Android UI Design

Android UI Design
Key Features
  • Take an initial idea for an Android app and develop it into a detailed plan, supported by sketches and wireframes
  • Provide a better experience for your users by following best practices and the new material design principles
  • Work more efficiently and save time by testing your ideas at an early stage by building a prototype
Book Description
Great design is one of the key drivers in the adoption of new applications, yet unfortunately design considerations are often neglected in the face of "will it work," "can we make it quicker," or "can we get more people using it"?

This book seeks to redress this balance by showing you how to get your PM to start treating the design phase of your project seriously. This book is focused entirely on the development of UI features, and you'll be able to practically implementing the design practices that we extol throughout the book.

Starting by briefly outlining some of the factors you need to keep in mind when building a UI, you'll learn the concepts of Android User Interface from scratch. We then move on to formulate a plan on how to implement these concepts in various applications. We will deep dive into how UI features are implemented in real-world applications where UIs are complex and dynamic.

This book offers near complete coverage of UI-specific content including, views, fragments, the wireframing process, and how to add in splash screens-everything you need to make professional standard UIs for modern applications. It will then cover material design and show you how to implement Google's design aesthetic in a practical manner. Finally, it ensures the best possible user experience by analyzing the UI using various tools, and then addressing any problems they uncover.

By the end of the book, you'll be able to leverage the concepts of Android User Interface in your applications in order to attract new customers.

What you will learn
  • Develop a user interface that adheres to all the core material design principles
  • Transform your initial app idea into a concrete and detailed plan
  • Add Views, ViewGroups, layouts, and common UI components to your own Android projects
  • Use fragments and various strategies to gather user input
  • Create a new Android Studio project and develop it into a prototype
  • Identify and solve problems with your app's UI to deliver a better user experience
About the Author
Jessica Thornsby studied poetry, prose, and script writing at Bolton University before discovering the world of open source and technical writing, and has never looked back since. Today, she is a technical writer and full-time Android enthusiast residing in sunny Sheffield, England.

She enjoys writing about rooting and flashing mobile devices, Java, Eclipse, and all things Android and Google. When not wordsmithing about technology and obsessing about the latest Android developments, she keeps a blog about her local food scene, and writes about looking after exotic pets. On the rare occasions that she's dragged away from her computer, she enjoys beer gardens, curry houses, the Great British seaside, scary movies, and spending lots of time with her house rabbits and chinchillas.

Android Generate random number and display in various radix

Generate random number and display in various radix

This example generate random number, in the range between 0 and user selected progress value of SeekBar (If progress of 0 is selected, the upper limit is open). Then display the value in variou radix, same as the method used in last exercise "Convert String to int to String base on various radix".


package com.example.androidstringformat;

import java.util.Locale;
import java.util.Random;

import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.os.Bundle;

public class MainActivity extends ActionBarActivity {
 
 SeekBar seekbarRange;
 TextView textOut;
 Button buttonGen;
 
 Spinner spAvailableLocale;
 Locale[] availableLocales;
 
 Random random;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        seekbarRange = (SeekBar)findViewById(R.id.seekbarrange);
     textOut = (TextView)findViewById(R.id.printout);
     buttonGen = (Button)findViewById(R.id.buttongen);
     
     buttonGen.setOnClickListener(onClickListener);

     random = new Random();
    }
    
    OnClickListener onClickListener =
     new OnClickListener(){

   @Override
   public void onClick(View arg0) {
    
    int range = seekbarRange.getProgress();
    int randomNum;
    
    if(range==0){
     randomNum = random.nextInt();
    }else{
     randomNum = random.nextInt(range+1);
    }
    
    String result 
    = "default: " + Integer.toString(randomNum) + "\n"
    + "Binary: " + Integer.toBinaryString(randomNum) + "\n"
    + "Binary: " + Integer.toOctalString(randomNum) + "\n"
    + "Radix 10: " + Integer.toString(randomNum, 10) + "\n"
    + "Hex: " + Integer.toHexString(randomNum) + "\n"
    + "Radix 32: " + Integer.toString(randomNum, 32);
   
   textOut.setText(result);

   }};

}


<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.androidstringformat.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />
    
    <SeekBar
        android:id="@+id/seekbarrange"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="0"/>

    <Button
        android:id="@+id/buttongen"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=" - Generate Random number - " />
    <TextView
        android:id="@+id/printout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold" />

</LinearLayout>

Android WebView: display SVG using HTML and Javascript - By NAMAN TIWARI

Android WebView: display SVG using HTML and Javascript

This example show how to display SVG in WebView, using HTML and Javascript.


Scalable Vector Graphics (SVG) is an XML-based vector image format for two-dimensional graphics with support for interactivity and animation. The SVG specification is an open standard developed by the World Wide Web Consortium (W3C) since 1999. ~ Wikipedia.

You can convert graph file from PNG to SVG using inkscape. Save your svg file in /assets, same level of our html, mypage.html

Modify /assets/mypage.html from the former exercise of WebView.
<!DOCTYPE HTML>
<HTML>
<HEAD>
<script>

var p1;
var p2;
var canvas;
var context;
var cx;
var cy;
var x;
var y;
var canvasOffsetX;
var canvasOffsetY;

var imgSVG = new Image();
imgSVG.src = "test.svg";

function init(){
 p1 = document.getElementById('p1');
 p2 = document.getElementById('p2');
 p1.innerHTML=navigator.userAgent;
 
 canvas = document.getElementById('myCanvas');
 context = canvas.getContext('2d');
 canvasOffsetX = canvas.getBoundingClientRect().left;
 canvasOffsetY = canvas.getBoundingClientRect().top;
 
 canvas.width = window.innerWidth - (2 * canvasOffsetX);
 canvas.height = window.innerHeight - canvasOffsetY - 100;
 
 context.drawImage(imgSVG, 0, 0);
 
}

</script>
</HEAD>
<BODY onload="init()" style="border:5px solid #000000;">

<p id='p1'>un-init</p>

<canvas id='myCanvas' style="border:1px solid #FF0000;">
Canvas not support!
</canvas>

<p id='p2'></p>

</BODY>
</HTML>

Get IP addresses associated with the given host - By NAMAN TIWARI

Get IP addresses associated with the given host

java.net.InetAddress is a class of an Internet Protocol (IP) address. This can be either an IPv4 address or an IPv6 address, and in practice you'll have an instance of either Inet4Address or Inet6Address (this class cannot be instantiated directly). Most code does not need to distinguish between the two families, and should use InetAddress.

An InetAddress may have a hostname (accessible via getHostName), but may not, depending on how the InetAddress was created.

In this exercise, we are going to get all IP addresses associated with a host, by calling the function getAllByName().

Get IP addresses associated with the given host

package com.exercise.AndroidIP;

import java.net.InetAddress;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidIP extends Activity {
 
 EditText inputHostName;
 Button btnCheck;
 TextView textInetAddress;
 
 final static String DEFAULT_HOST_NAME = "www.google.com";
 
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
    inputHostName = (EditText)findViewById(R.id.hostname);
    btnCheck = (Button)findViewById(R.id.check);
    textInetAddress = (TextView)findViewById(R.id.InetAddress);
    
    inputHostName.setText(DEFAULT_HOST_NAME);
    btnCheck.setOnClickListener(btnCheckOnClickListener);
   }
  
   Button.OnClickListener btnCheckOnClickListener
   = new Button.OnClickListener(){

  @Override
  public void onClick(View arg0) {
   // TODO Auto-generated method stub
   String hostName = inputHostName.getText().toString();
   try {

    InetAddress[] hostInetAddress
     = InetAddress.getAllByName(hostName);
    String all = "";
    for(int i = 0; i < hostInetAddress.length; i++){
     all = all + String.valueOf(i) + " : "
      + hostInetAddress[i].toString() + "\n";
    }
    textInetAddress.setText(all);
    
   } catch (UnknownHostException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    Toast.makeText(AndroidIP.this, e.toString(),
      Toast.LENGTH_LONG).show();
   }
  }
    
   };
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
<TextView 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="@string/hello"
   />
<TextView 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="Enter Host Name"
   />
<EditText
   android:id="@+id/hostname"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   />
<Button
   android:id="@+id/check"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text=" Check "
   />
<TextView 
   android:id="@+id/InetAddress"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   />
</LinearLayout>


Grant permission of "android.permission.INTERNET"
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.exercise.AndroidIP"
     android:versionCode="1"
     android:versionName="1.0">
   <uses-sdk android:minSdkVersion="4" />
   <uses-permission android:name="android.permission.INTERNET"></uses-permission>

   <application android:icon="@drawable/icon" android:label="@string/app_name">
       <activity android:name=".AndroidIP"
                 android:label="@string/app_name">
           <intent-filter>
               <action android:name="android.intent.action.MAIN" />
               <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
       </activity>

   </application>
</manifest>

FREE MSDN Magazine Windows 10 Special Issue - BY NAMAN TIWARI

FREE MSDN Magazine Windows 10 Special Issue


MSDN Magazine Windows 10 Special Issue, can be READ ONLINE or Download PDF.

Youtube Player Fragment API - By Naman Tiwari

Example to use YouTubePlayerFragment of YouTube Android Player API

Example to use YouTubePlayerFragment of YouTube Android Player API

Example to use YouTubePlayerFragment of YouTube Android Player API


Follow the step in "YouTube Android Player API step-by-step" to Download and import YouTube Android Player API, and Register your app using YouTube Android Player API and obtain API Key. To use YouTubePlayerFragment, you have to define android:minSdkVersion="11" or higher in your AndroidManifest.xml.

Modify /res/layout/activity_main.xml to define layout, with <fragment> of "com.google.android.youtube.player.YouTubePlayerFragment".
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <fragment
        android:name="com.google.android.youtube.player.YouTubePlayerFragment"
        android:id="@+id/youtubeplayerfragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold"
        android:layout_gravity="center_horizontal"
        android:autoLink="web" />
    
    <ScrollView 
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <Button
                android:id="@+id/btnviewfullscreen"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="View in Full Screen" />
            <TextView 
                android:id="@+id/videolog"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
            
        </LinearLayout>
    </ScrollView>
    
</LinearLayout>


Create /res/layout-land/activity_main.xml to define layout in Landscape orientation.
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="horizontal"
    tools:context=".MainActivity" >

    <fragment
        android:name="com.google.android.youtube.player.YouTubePlayerFragment"
        android:id="@+id/youtubeplayerfragment"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3"/>
    <LinearLayout 
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight="1">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="http://android-er.blogspot.com/"
            android:textStyle="bold"
            android:layout_gravity="center_horizontal"
            android:autoLink="web" />
        
        <ScrollView 
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <LinearLayout 
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
                <Button
                    android:id="@+id/btnviewfullscreen"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="View in Full Screen" />
                <TextView 
                    android:id="@+id/videolog"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
            </LinearLayout>
  </ScrollView>
    </LinearLayout>
</LinearLayout>


MainActivity.java
package com.example.androidyoutubeapiplayer;

import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayer.PlaybackEventListener;
import com.google.android.youtube.player.YouTubePlayerFragment;
import com.google.android.youtube.player.YouTubePlayer.PlayerStateChangeListener;
import com.google.android.youtube.player.YouTubePlayer.Provider;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends YouTubeBaseActivity implements 
 YouTubePlayer.OnInitializedListener{
 
 public static final String API_KEY = "AIzaSyCe6tORd9Ch4lx-9Ku5SQ476uS9OtZYsWA";
 public static final String VIDEO_ID = "o7VVHhK9zf0";
 
 private YouTubePlayer youTubePlayer;
 private YouTubePlayerFragment youTubePlayerFragment;
 private TextView textVideoLog;
 private Button btnViewFullScreen;
 
 private static final int RQS_ErrorDialog = 1;
 
 private MyPlayerStateChangeListener myPlayerStateChangeListener;
 private MyPlaybackEventListener myPlaybackEventListener;
 
 String log = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        youTubePlayerFragment = (YouTubePlayerFragment)getFragmentManager()
             .findFragmentById(R.id.youtubeplayerfragment);
        youTubePlayerFragment.initialize(API_KEY, this);

        textVideoLog = (TextView)findViewById(R.id.videolog);
        
        myPlayerStateChangeListener = new MyPlayerStateChangeListener();
        myPlaybackEventListener = new MyPlaybackEventListener();
        
        btnViewFullScreen = (Button)findViewById(R.id.btnviewfullscreen);
        btnViewFullScreen.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View arg0) {
    youTubePlayer.setFullscreen(true);
   }});
    }

 @Override
 public void onInitializationFailure(Provider provider,
   YouTubeInitializationResult result) {
  
  if (result.isUserRecoverableError()) {
   result.getErrorDialog(this, RQS_ErrorDialog).show(); 
  } else {
   Toast.makeText(this, 
     "YouTubePlayer.onInitializationFailure(): " + result.toString(), 
     Toast.LENGTH_LONG).show(); 
  }
 }

 @Override
 public void onInitializationSuccess(Provider provider, YouTubePlayer player,
   boolean wasRestored) {
  
  youTubePlayer = player;
  
  Toast.makeText(getApplicationContext(), 
    "YouTubePlayer.onInitializationSuccess()", 
    Toast.LENGTH_LONG).show();
  
  youTubePlayer.setPlayerStateChangeListener(myPlayerStateChangeListener);
  youTubePlayer.setPlaybackEventListener(myPlaybackEventListener);
  
  if (!wasRestored) {
        player.cueVideo(VIDEO_ID);
      }

 }
 
 private final class MyPlayerStateChangeListener implements PlayerStateChangeListener {
  
  private void updateLog(String prompt){
   log +=  "MyPlayerStateChangeListener" + "\n" + 
     prompt + "\n\n=====";
   textVideoLog.setText(log);
  };

  @Override
  public void onAdStarted() {
   updateLog("onAdStarted()");
  }

  @Override
  public void onError(
    com.google.android.youtube.player.YouTubePlayer.ErrorReason arg0) {
   updateLog("onError(): " + arg0.toString());
  }

  @Override
  public void onLoaded(String arg0) {
   updateLog("onLoaded(): " + arg0);
  }

  @Override
  public void onLoading() {
   updateLog("onLoading()");
  }

  @Override
  public void onVideoEnded() {
   updateLog("onVideoEnded()");
  }

  @Override
  public void onVideoStarted() {
   updateLog("onVideoStarted()");
  }
  
 }
 
 private final class MyPlaybackEventListener implements PlaybackEventListener {
  
  private void updateLog(String prompt){
   log +=  "MyPlaybackEventListener" + "\n-" + 
     prompt + "\n\n=====";
   textVideoLog.setText(log);
  };

  @Override
  public void onBuffering(boolean arg0) {
   updateLog("onBuffering(): " + String.valueOf(arg0));
  }

  @Override
  public void onPaused() {
   updateLog("onPaused()");
  }

  @Override
  public void onPlaying() {
   updateLog("onPlaying()");
  }

  @Override
  public void onSeekTo(int arg0) {
   updateLog("onSeekTo(): " + String.valueOf(arg0));
  }

  @Override
  public void onStopped() {
   updateLog("onStopped()");
  }
  
 }

}