Thursday, April 24, 2014

Example of ViewPager with custom PagerAdapter

This example show how to implement ViewPager with custom PagerAdapter. (Another example(s) implement ViewPager with FragmentPagerAdapter, read HERE)


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);
  }

 }

}


download filesDownload the files.

Wednesday, April 23, 2014

Java Programming for Android Developers For Dummies

Get started creating Android apps with Java in no time!

Java Programming for Android Developers For Dummies

The demand for Android apps is not slowing down but many mobile developers who want to create Android apps lack the necessary Java background. This beginner guide gets you up and running with using Java to create Android apps with no prior knowledge or experienced necessary!
  • Shows you the basic Java development concepts and techniques that are necessary to develop Android apps
  • Explores what goes into creating an Android app to give you a better understanding of the various elements
  • Addresses how to deal with standard programming challenges and debugging
Beginning Android Programming with Java For Dummies puts you well on your way toward creating Android apps quickly with Java.

Android Hacker's Handbook

The first comprehensive guide to discovering and preventing attacks on the Android OS

Android Hacker's Handbook

As the Android operating system continues to increase its share of the smartphone market, smartphone hacking remains a growing threat. Written by experts who rank among the world's foremost Android security researchers, this book presents vulnerability discovery, analysis, and exploitation tools for the good guys. Following a detailed explanation of how the Android OS works and its overall security architecture, the authors examine how vulnerabilities can be discovered and exploits developed for various system components, preparing you to defend against them.

If you are a mobile device administrator, security researcher, Android app developer, or consultant responsible for evaluating Android security, you will find this guide is essential to your toolbox.
  • A crack team of leading Android security researchers explain Android security risks, security design and architecture, rooting, fuzz testing, and vulnerability analysis
  • Covers Android application building blocks and security as well as debugging and auditing Android apps
  • Prepares mobile device administrators, security researchers, Android app developers, and security consultants to defend Android systems against attack
Android Hacker's Handbook is the first comprehensive resource for IT professionals charged with smartphone security.

Sunday, April 20, 2014

Example of programming Android NFC

This example send Uri between Android devices using NFC.


To using NFC on your Android app, modify AndroidManifest.xml.
  • This example target minSdkVersion="16"
  • Add permission of "android.permission.NFC"
  • Add intent-filter of "android.nfc.action.NDEF_DISCOVERED"
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.androidnfc"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="19" />
    <uses-permission android:name="android.permission.NFC"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.androidnfc.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            
            <intent-filter>
                <action android:name="android.nfc.action.NDEF_DISCOVERED" />
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:scheme="http"
                    android:host="android-er.blogspot.com"
                    android:pathPrefix="/" />
            </intent-filter>
            
        </activity>
    </application>

</manifest>


MainActivity.java
package com.example.androidnfc;

import android.app.Activity;
import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.NfcAdapter.CreateNdefMessageCallback;
import android.nfc.NfcAdapter.OnNdefPushCompleteCallback;
import android.nfc.NfcEvent;
import android.os.Bundle;
import android.os.Parcelable;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements 
 CreateNdefMessageCallback, OnNdefPushCompleteCallback{
 
 TextView textInfo;
 
 NfcAdapter nfcAdapter;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  textInfo = (TextView)findViewById(R.id.info);

  nfcAdapter = NfcAdapter.getDefaultAdapter(this);
  if(nfcAdapter==null){
   Toast.makeText(MainActivity.this, 
    "nfcAdapter==null, no NFC adapter exists", 
    Toast.LENGTH_LONG).show();
  }else{
   Toast.makeText(MainActivity.this, 
     "Set Callback(s)", 
     Toast.LENGTH_LONG).show();
   nfcAdapter.setNdefPushMessageCallback(this, this);
   nfcAdapter.setOnNdefPushCompleteCallback(this, this);
  }
 }

 @Override
 protected void onResume() {
  super.onResume();
  Intent intent = getIntent();
  String action = intent.getAction();
  if(action.equals(NfcAdapter.ACTION_NDEF_DISCOVERED)){
   Parcelable[] parcelables = 
    intent.getParcelableArrayExtra(
      NfcAdapter.EXTRA_NDEF_MESSAGES);
   NdefMessage inNdefMessage = (NdefMessage)parcelables[0];
   NdefRecord[] inNdefRecords = inNdefMessage.getRecords();
   NdefRecord NdefRecord_0 = inNdefRecords[0];
   String inMsg = new String(NdefRecord_0.getPayload());
   textInfo.setText(inMsg);
  }
 }

 @Override
 protected void onNewIntent(Intent intent) {
  setIntent(intent);
 }

 @Override
 public void onNdefPushComplete(NfcEvent event) {
  
  final String eventString = "onNdefPushComplete\n" + event.toString();
  runOnUiThread(new Runnable() {
   
   @Override
   public void run() {
    Toast.makeText(getApplicationContext(), 
      eventString, 
      Toast.LENGTH_LONG).show();
   }
  });

 }

 @Override
 public NdefMessage createNdefMessage(NfcEvent event) {
  NdefRecord rtdUriRecord = NdefRecord.createUri("http://android-er.blogspot.com/");

  NdefMessage ndefMessageout = new NdefMessage(rtdUriRecord);
  return ndefMessageout;
 }

}


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: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.androidnfc.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" />

    <TextView
        android:id="@+id/info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>


download filesDownload the files.

Saturday, April 19, 2014

Strategies for Developing Cross-Device Applications with Visual Studio 2013

This session will cover the strategic decisions developers have to make when targeting multiples devices in application. The video will explore the tools and technologies that available in Visual Studio 2013 for both web and native applications that target Windows, iOS and Android devices, as well as best practices to reuse code and skills across them.

source: http://channel9.msdn.com/Events/Build/2014/2-586