Pages

Showing posts with label example. Show all posts
Showing posts with label example. Show all posts

Thursday, May 12, 2016

Android NFC readBlock for MifareClassic to dump data in RFID tag



Last example "Android NFC read MifareClassic RFID tag, with android.nfc.action.TECH_DISCOVERED" read some general info of the RFID tag; such as type, size... This example dump the data inside the tag by calling readBlock() of MifareClassic.

Test on bland new MifareClassic RFID Card and Key.
(Android Studio project and signed APK are available on bottom of this post, you can test on your Android devices)


MifareClassic.readBlock() is an I/O operation and will block until complete. It must not be called from the main application thread. So we have to implement our AsyncTask to perform in background thread.

Modify MainActivity.java in last example:
package com.blogspot.android_er.androidnfctechdiscovered;

import android.content.Intent;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.MifareClassic;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {

private NfcAdapter nfcAdapter;
TextView textViewInfo, textViewTagInfo, textViewBlock;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewInfo = (TextView)findViewById(R.id.info);
textViewTagInfo = (TextView)findViewById(R.id.taginfo);
textViewBlock = (TextView)findViewById(R.id.block);

nfcAdapter = NfcAdapter.getDefaultAdapter(this);
if(nfcAdapter == null){
Toast.makeText(this,
"NFC NOT supported on this devices!",
Toast.LENGTH_LONG).show();
finish();
}else if(!nfcAdapter.isEnabled()){
Toast.makeText(this,
"NFC NOT Enabled!",
Toast.LENGTH_LONG).show();
finish();
}
}

@Override
protected void onResume() {
super.onResume();

Intent intent = getIntent();
String action = intent.getAction();

if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
Toast.makeText(this,
"onResume() - ACTION_TECH_DISCOVERED",
Toast.LENGTH_SHORT).show();

Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
if(tag == null){
textViewInfo.setText("tag == null");
}else{
String tagInfo = tag.toString() + " ";

tagInfo += " Tag Id: ";
byte[] tagId = tag.getId();
tagInfo += "length = " + tagId.length +" ";
for(int i=0; i<tagId.length; i++){
tagInfo += String.format("%02X", tagId[i] & 0xff) + " ";
}
tagInfo += " ";

String[] techList = tag.getTechList();
tagInfo += " Tech List ";
tagInfo += "length = " + techList.length +" ";
for(int i=0; i<techList.length; i++){
tagInfo += techList[i] + " ";
}

textViewInfo.setText(tagInfo);

//Only android.nfc.tech.MifareClassic specified in nfc_tech_filter.xml,
//so must be MifareClassic
readMifareClassic(tag);
}
}else{
Toast.makeText(this,
"onResume() : " + action,
Toast.LENGTH_SHORT).show();
}
}

public void readMifareClassic(Tag tag){
MifareClassic mifareClassicTag = MifareClassic.get(tag);

String typeInfoString = "--- MifareClassic tag --- ";
int type = mifareClassicTag.getType();
switch(type){
case MifareClassic.TYPE_PLUS:
typeInfoString += "MifareClassic.TYPE_PLUS ";
break;
case MifareClassic.TYPE_PRO:
typeInfoString += "MifareClassic.TYPE_PRO ";
break;
case MifareClassic.TYPE_CLASSIC:
typeInfoString += "MifareClassic.TYPE_CLASSIC ";
break;
case MifareClassic.TYPE_UNKNOWN:
typeInfoString += "MifareClassic.TYPE_UNKNOWN ";
break;
default:
typeInfoString += "unknown...! ";
}

int size = mifareClassicTag.getSize();
switch(size){
case MifareClassic.SIZE_1K:
typeInfoString += "MifareClassic.SIZE_1K ";
break;
case MifareClassic.SIZE_2K:
typeInfoString += "MifareClassic.SIZE_2K ";
break;
case MifareClassic.SIZE_4K:
typeInfoString += "MifareClassic.SIZE_4K ";
break;
case MifareClassic.SIZE_MINI:
typeInfoString += "MifareClassic.SIZE_MINI ";
break;
default:
typeInfoString += "unknown size...! ";
}

int blockCount = mifareClassicTag.getBlockCount();
typeInfoString += "BlockCount = " + blockCount + " ";
int sectorCount = mifareClassicTag.getSectorCount();
typeInfoString += "SectorCount = " + sectorCount + " ";

textViewTagInfo.setText(typeInfoString);

new ReadMifareClassicTask(mifareClassicTag).execute();

}

private class ReadMifareClassicTask extends AsyncTask<Void, Void, Void> {

/*
MIFARE Classic tags are divided into sectors, and each sector is sub-divided into blocks.
Block size is always 16 bytes (BLOCK_SIZE). Sector size varies.
MIFARE Classic 1k are 1024 bytes (SIZE_1K), with 16 sectors each of 4 blocks.
*/

MifareClassic taskTag;
int numOfBlock;
final int FIX_SECTOR_COUNT = 16;
boolean success;
final int numOfSector = 16;
final int numOfBlockInSector = 4;
byte[][][] buffer = new byte[numOfSector][numOfBlockInSector][MifareClassic.BLOCK_SIZE];

ReadMifareClassicTask(MifareClassic tag){
taskTag = tag;
success = false;
}

@Override
protected void onPreExecute() {
textViewBlock.setText("Reading Tag, dont remove it!");
}

@Override
protected Void doInBackground(Void... params) {

try {
taskTag.connect();

for(int s=0; s<numOfSector; s++){
if(taskTag.authenticateSectorWithKeyA(s, MifareClassic.KEY_DEFAULT)) {
for(int b=0; b<numOfBlockInSector; b++){
int blockIndex = (s * numOfBlockInSector) + b;
buffer[s][b] = taskTag.readBlock(blockIndex);
}
}
}

success = true;
} catch (IOException e) {
e.printStackTrace();
} finally{
if(taskTag!=null){
try {
taskTag.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

return null;
}

@Override
protected void onPostExecute(Void aVoid) {
//display block
if(success){
String stringBlock = "";
for(int i=0; i<numOfSector; i++){
stringBlock += i + " : ";
for(int j=0; j<numOfBlockInSector; j++){
for(int k=0; k<MifareClassic.BLOCK_SIZE; k++){
stringBlock += String.format("%02X", buffer[i][j][k] & 0xff) + " ";
}
stringBlock += " ";
}
stringBlock += " ";
}
textViewBlock.setText(stringBlock);
}else{
textViewBlock.setText("Fail to read Blocks!!!");
}
}
}
}


layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout


android_layout_width="match_parent"
android_layout_height="match_parent"
android_padding="16dp"
android_orientation="horizontal"
tools_context=".MainActivity">

<LinearLayout
android_layout_width="0dp"
android_layout_height="match_parent"
android_layout_weight="1"
android_orientation="vertical">

<TextView
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_margin="10dp"
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="match_parent"
android_layout_height="wrap_content"
android_textStyle="italic"/>

<TextView
android_id="@+id/taginfo"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_textStyle="bold"/>

</LinearLayout>

<ScrollView
android_layout_width="0dp"
android_layout_height="match_parent"
android_layout_weight="1">
<TextView
android_id="@+id/block"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_typeface="monospace"/>
</ScrollView>
</LinearLayout>

Other files, AndroidManifest.xml and nfc_tech_filter.xml, refer to last example "Android NFC read MifareClassic RFID tag, with android.nfc.action.TECH_DISCOVERED"

download filesDownload the files (Android Studio Format) .

download filesDownload APK .


- Similarly example run on Arduino: Arduino Uno + RFID-RC522, MFRC522 library example DumpInfo
- Step-by-step to make MFRC522-python work on Raspberry Pi 2/raspbian Jessie, read RFID tags using RFID Reader, RFID-RC522.
- Raspberry Pi 2 + MFRC522-python - Dump RFID Tag data using mxgxw/MFRC522-python

Read More..

Thursday, May 5, 2016

Android example to create grouped sub OptionsMenu


Create menu/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu
>
<item
android_id="@+id/option1"
android_orderInCategory="100"
app_showAsAction="ifRoom"
android_title="Option 1"/>
<item
android_id="@+id/option2"
android_orderInCategory="100"
app_showAsAction="never"
android_title="Option 2"/>
<item
android_id="@+id/option3"
android_orderInCategory="100"
app_showAsAction="never"
android_title="Option 3...">
<menu >
<group android_id="@+id/group3"
android_checkableBehavior="single">
<item android_id="@+id/submenu3a"
android_title="SubMenu 3A" />
<item android_id="@+id/submenu3b"
android_title="SubMenu 3B" />
</group>
</menu>

</item>
</menu>

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

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

public class MainActivity extends AppCompatActivity {

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.option1:
Toast.makeText(MainActivity.this, "Option 1", Toast.LENGTH_LONG).show();
return true;
case R.id.option2:
Toast.makeText(MainActivity.this, "Option 2", Toast.LENGTH_LONG).show();
return true;
case R.id.submenu3a:
Toast.makeText(MainActivity.this, "Option 3 A", Toast.LENGTH_LONG).show();
return true;
case R.id.submenu3b:
Toast.makeText(MainActivity.this, "Option 3 B", Toast.LENGTH_LONG).show();
return true;
}
return super.onOptionsItemSelected(item);
}
}



Read More..

Android Chat example with server sending individual message to specify client


Refer to my old post of Simple Android Chat Application, server side, and client side. Function to send sending individual message to specify client is added in server side.


uses-permission of "android.permission.INTERNET" is needed in AndroidManifest.xml, for both server and client.


Server side:

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

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

public class MainActivity extends AppCompatActivity {

static final int SocketServerPORT = 8080;

TextView infoIp, infoPort, chatMsg;
Spinner spUsers;
ArrayAdapter<ChatClient> spUsersAdapter;
Button btnSentTo;

String msgLog = "";

List<ChatClient> userList;

ServerSocket serverSocket;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
infoIp = (TextView) findViewById(R.id.infoip);
infoPort = (TextView) findViewById(R.id.infoport);
chatMsg = (TextView) findViewById(R.id.chatmsg);

spUsers = (Spinner) findViewById(R.id.spusers);
userList = new ArrayList<ChatClient>();
spUsersAdapter = new ArrayAdapter<ChatClient>(
MainActivity.this, android.R.layout.simple_spinner_item, userList);
spUsersAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spUsers.setAdapter(spUsersAdapter);

btnSentTo = (Button)findViewById(R.id.sentto);
btnSentTo.setOnClickListener(btnSentToOnClickListener);

infoIp.setText(getIpAddress());

ChatServerThread chatServerThread = new ChatServerThread();
chatServerThread.start();
}

View.OnClickListener btnSentToOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
ChatClient client = (ChatClient)spUsers.getSelectedItem();
if(client != null){
String dummyMsg = "Dummy message from server. ";
client.chatThread.sendMsg(dummyMsg);
msgLog += "- Dummy message to " + client.name + " ";
chatMsg.setText(msgLog);

}else{
Toast.makeText(MainActivity.this, "No user connected", Toast.LENGTH_LONG).show();
}
}
};

@Override
protected void onDestroy() {
super.onDestroy();

if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

private class ChatServerThread extends Thread {

@Override
public void run() {
Socket socket = null;

try {
serverSocket = new ServerSocket(SocketServerPORT);
MainActivity.this.runOnUiThread(new Runnable() {

@Override
public void run() {
infoPort.setText("Im waiting here: "
+ serverSocket.getLocalPort());
}
});

while (true) {
socket = serverSocket.accept();
ChatClient client = new ChatClient();
userList.add(client);
ConnectThread connectThread = new ConnectThread(client, socket);
connectThread.start();

runOnUiThread(new Runnable() {
@Override
public void run() {
spUsersAdapter.notifyDataSetChanged();
}
});
}

} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}

}

private class ConnectThread extends Thread {

Socket socket;
ChatClient connectClient;
String msgToSend = "";

ConnectThread(ChatClient client, Socket socket){
connectClient = client;
this.socket= socket;
client.socket = socket;
client.chatThread = this;
}

@Override
public void run() {
DataInputStream dataInputStream = null;
DataOutputStream dataOutputStream = null;

try {
dataInputStream = new DataInputStream(socket.getInputStream());
dataOutputStream = new DataOutputStream(socket.getOutputStream());

String n = dataInputStream.readUTF();

connectClient.name = n;

msgLog += connectClient.name + " connected@" +
connectClient.socket.getInetAddress() +
":" + connectClient.socket.getPort() + " ";
MainActivity.this.runOnUiThread(new Runnable() {

@Override
public void run() {
chatMsg.setText(msgLog);
}
});

dataOutputStream.writeUTF("Welcome " + n + " ");
dataOutputStream.flush();

broadcastMsg(n + " join our chat. ");

while (true) {
if (dataInputStream.available() > 0) {
String newMsg = dataInputStream.readUTF();


msgLog += n + ": " + newMsg;
MainActivity.this.runOnUiThread(new Runnable() {

@Override
public void run() {
chatMsg.setText(msgLog);
}
});

broadcastMsg(n + ": " + newMsg);
}

if(!msgToSend.equals("")){
dataOutputStream.writeUTF(msgToSend);
dataOutputStream.flush();
msgToSend = "";
}

}

} catch (IOException e) {
e.printStackTrace();
} finally {
if (dataInputStream != null) {
try {
dataInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

if (dataOutputStream != null) {
try {
dataOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

userList.remove(connectClient);

MainActivity.this.runOnUiThread(new Runnable() {

@Override
public void run() {
spUsersAdapter.notifyDataSetChanged();
Toast.makeText(MainActivity.this,
connectClient.name + " removed.", Toast.LENGTH_LONG).show();

msgLog += "-- " + connectClient.name + " leaved ";
MainActivity.this.runOnUiThread(new Runnable() {

@Override
public void run() {
chatMsg.setText(msgLog);
}
});

broadcastMsg("-- " + connectClient.name + " leaved ");
}
});
}

}

private void sendMsg(String msg){
msgToSend = msg;
}

}

private void broadcastMsg(String msg){
for(int i=0; i<userList.size(); i++){
userList.get(i).chatThread.sendMsg(msg);
msgLog += "- send to " + userList.get(i).name + " ";
}

MainActivity.this.runOnUiThread(new Runnable() {

@Override
public void run() {
chatMsg.setText(msgLog);
}
});
}

private String getIpAddress() {
String ip = "";
try {
Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface
.getNetworkInterfaces();
while (enumNetworkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = enumNetworkInterfaces
.nextElement();
Enumeration<InetAddress> enumInetAddress = networkInterface
.getInetAddresses();
while (enumInetAddress.hasMoreElements()) {
InetAddress inetAddress = enumInetAddress.nextElement();

if (inetAddress.isSiteLocalAddress()) {
ip += "SiteLocalAddress: "
+ inetAddress.getHostAddress() + " ";
}

}

}

} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ip += "Something Wrong! " + e.toString() + " ";
}

return ip;
}

class ChatClient {
String name;
Socket socket;
ConnectThread chatThread;

@Override
public String toString() {
return name + ": " + socket.getInetAddress().getHostAddress();
}
}
}


activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout

android_layout_width="match_parent"
android_layout_height="match_parent"
android_padding="16dp"
android_orientation="vertical"
tools_context="com.blogspot.android_er.androidchatserver.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_layout_width="match_parent"
android_layout_height="wrap_content"
android_text="Char Server"
android_textStyle="bold" />

<TextView
android_id="@+id/infoport"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_textStyle="italic" />

<TextView
android_id="@+id/infoip"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_textStyle="italic" />

<Spinner
android_id="@+id/spusers"
android_layout_width="match_parent"
android_layout_height="wrap_content"/>

<Button
android_id="@+id/sentto"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_text="Sent msg to individual user"/>

<ScrollView
android_layout_width="match_parent"
android_layout_height="match_parent" >

<TextView
android_id="@+id/chatmsg"
android_layout_width="wrap_content"
android_layout_height="wrap_content" />
</ScrollView>
</LinearLayout>



download filesDownload the server side files (Android Studio Format) .

Client side same as in the post "Simple Android Chat Application, client side" in Android Studio form.

download filesDownload the client side files (Android Studio Format) .

Read More..

Saturday, April 30, 2016

Animated GIF Androidify for 3D Hologram viewer


Last post I tried to generate interactive animation using ObjectAnimator, to simulate 3D Hologram effect. This post I create Custom View to display animated GIF of Androidify, to simulate the effect.


The animated GIFs of Androidify are create using Googles Androidify App.  Then I have to edit the animated GIFs to resize it to 200x200, and change the background to black using GIMP. Then copy the files to drawable folder in Android Studio project.

Create a custom view, GifView, to handle the animated GIF. If you are looking for display animated GIF, refer "Play animated GIF using android.graphics.Movie, with Movie.decodeStream(InputStream)". In this version, I have to handle src attribute from xml, such that I can display four GifView of different source.

GifView .java
package com.blogspot.android_er.androidmirror;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Movie;
import android.util.AttributeSet;
import android.view.View;

import java.io.InputStream;

public class GifView extends View {

private InputStream gifInputStream;
private Movie gifMovie;
private int movieWidth, movieHeight;
private long movieDuration;
private long mMovieStart;

public GifView(Context context) {
super(context);
init(context, null);
}

public GifView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}

public GifView(Context context, AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}

private void init(Context context, AttributeSet attrs){
setFocusable(true);

//Handle src attribute from xml
if(attrs == null){
gifInputStream = context.getResources().openRawResource(R.drawable.androidify1b);
}else{
int src_id = attrs.getAttributeResourceValue(
"http://schemas.android.com/apk/res/android",
"src",
R.drawable.androidify1b);
gifInputStream = context.getResources().openRawResource(src_id);
}

gifMovie = Movie.decodeStream(gifInputStream);
movieWidth = gifMovie.width();
movieHeight = gifMovie.height();
movieDuration = gifMovie.duration();
}

@Override
protected void onMeasure(int widthMeasureSpec,
int heightMeasureSpec) {
setMeasuredDimension(movieWidth, movieHeight);
}

public int getMovieWidth(){
return movieWidth;
}

public int getMovieHeight(){
return movieHeight;
}

public long getMovieDuration(){
return movieDuration;
}

@Override
protected void onDraw(Canvas canvas) {

long now = android.os.SystemClock.uptimeMillis();
if (mMovieStart == 0) { // first time
mMovieStart = now;
}

if (gifMovie != null) {

int dur = gifMovie.duration();
if (dur == 0) {
dur = 1000;
}

int relTime = (int)((now - mMovieStart) % dur);

gifMovie.setTime(relTime);

gifMovie.draw(canvas, 0, 0);
invalidate();

}

}
}


layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout


android_layout_width="match_parent"
android_layout_height="match_parent"
android_padding="16dp"
android_background="@android:color/black"
tools_context=".MainActivity">

<TextView
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_autoLink="web"
android_text="http://android-er.blogspot.com/"
android_textStyle="bold"
android_layout_alignParentTop="true"/>

<GridLayout
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_columnCount="3"
android_layout_centerInParent="true">
<ImageView
android_layout_width="wrap_content"
android_layout_height="wrap_content"/>
<com.blogspot.android_er.androidmirror.GifView
android_id="@+id/image2"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_src="@drawable/androidify1b"
android_layout_gravity="center"/>
<ImageView
android_layout_width="wrap_content"
android_layout_height="wrap_content"/>
<com.blogspot.android_er.androidmirror.GifView
android_id="@+id/image4"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_src="@drawable/androidify2b"
android_layout_gravity="center"
android_rotation="270"/>
<ImageView
android_layout_width="20mm"
android_layout_height="20mm"/>
<com.blogspot.android_er.androidmirror.GifView
android_id="@+id/image6"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_src="@drawable/androidify3b"
android_layout_gravity="center"
android_rotation="90"/>
<ImageView
android_layout_width="wrap_content"
android_layout_height="wrap_content"/>
<com.blogspot.android_er.androidmirror.GifView
android_id="@+id/image8"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_src="@drawable/androidify4b"
android_layout_gravity="center"
android_rotation="180"/>
<ImageView
android_layout_width="wrap_content"
android_layout_height="wrap_content"/>

</GridLayout>
</RelativeLayout>


Edit AndroidManifest.xml to disable hardwareAccelerated by setting it false.
<?xml version="1.0" encoding="utf-8"?>
<manifest
package="com.blogspot.android_er.androidmirror" >

<application
android_allowBackup="true"
android_icon="@mipmap/ic_launcher"
android_label="@string/app_name"
android_supportsRtl="true"
android_theme="@style/AppTheme" >
<activity android_name=".MainActivity"
android_hardwareAccelerated="false">
<intent-filter>
<action android_name="android.intent.action.MAIN" />

<category android_name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>


Nothing do on MainActivity.java
package com.blogspot.android_er.androidmirror;

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

public class MainActivity extends AppCompatActivity {

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

}



download filesDownload the files (Android Studio Format) .

download filesDownload APK .


If you have a DIY 3D Hologram Projector, you can view the test view at YouTube.

Read More..

Sunday, April 17, 2016

SQLite DB Example Android Developer Tutorial Part 12


There are 4 ways of storing data on the android platform:

  • 1.    Preferences
  • 2.    SQLite Database
  • 3.    Files
  • 4.    Network

A word about each of them here and then I will move on to an example that shows how to work with SQLite DB that comes along with the android platform.


Preferences
Basically used for storing user preferences for a single application or across applications for a mobile. This is typically name-value pairs accessible to the context.


Databases –
Android supports creating of databases based on SQLite db. Each database is private to the applications that creates it

Files –
Files can be directly stored on the mobile or on to an extended storage medium. By default other applications cannot access it.


Network –
Data can be stored and retrieved from the network too depending on the availability.


If an application wants to store and retrieve data for its own use, without having to share the data across applications, it can access the SQLite DB directly. There is no need of a content provider. We have seen in an earlier post how to use content providers.


In this example, we will do the following:
1.    Create a database (typically a one time activity)
2.    Create a table (typically a one time activity)
3.    Insert values into the table
4.    Retrieve the values from the table
5.    Display the retrieved values as a List view
6.    Delete all the records from the table before closing the connection to the database


Step 1: Create a database:


   sampleDB =  this.openOrCreateDatabase(SAMPLE_DB_NAME, MODE_PRIVATE, null);

This opens a database defined in the constant SAMPLE_DB_NAME, if it already exists. Else it creates a database and opens it. The second parameter is operating mode : MODE_PRIVATE meaning it is accessible to only this context. The other modes are and MODE_WORLD_WRITABLE. MODE_WORLD_READABLE


Step 2: Create a Table:


sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " +
                        SAMPLE_TABLE_NAME +
                        " (LastName VARCHAR, FirstName VARCHAR," +
                        " Country VARCHAR, Age INT(3));");


Step 3: Insert values into the table:


sampleDB.execSQL("INSERT INTO " +
                        SAMPLE_TABLE_NAME +
                        " Values (Makam,Sai Geetha,India,25);");


Step 4: Retrieve values


Cursor c = sampleDB.rawQuery("SELECT FirstName, Age FROM " +
                        SAMPLE_TABLE_NAME +
                        " where Age > 10 LIMIT 5", null);
           
      if (c != null ) {
            if  (c.moveToFirst()) {
                  do {
String firstName = c.getString(c.getColumnIndex("FirstName"));
                  int age = c.getInt(c.getColumnIndex("Age"));
                  results.add("" + firstName + ",Age: " + age);
                  }while (c.moveToNext());
            }
       }


Step 5: Display the values as a list
           
       this.setListAdapter(newArrayAdapter<String>(this, android.R.layout.simple_list_item_1,results));


The statement displays it as a list as the class extends a ListActivity.


Step 6: Delete the values from the table in the finally part of the try block


finally {
            if (sampleDB != null)
                  sampleDB.execSQL("DELETE FROM " + SAMPLE_TABLE_NAME);
                  sampleDB.close();
        }


It is as simple as this to work with the SQLite DB even in android. No different from a desktop application. However, there are various overloaded methods of query() provided by the SQLIteDatabase class which can be more optimally used instead of execSQL.


The complete code for this example is downloadable here.

Read More..

Thursday, April 14, 2016

Android Programming for Beginners

Learn all the Java and Android skills you need to start making powerful mobile applications

Android Programming for Beginners

About This Book
  • Kick-start your Android programming career, or just have fun publishing apps to the Google Play marketplace
  • A first-principles introduction to Java, via Android, which means youll be able to start building your own applications from scratch
  • Learn by example and build three real-world apps and over 40 mini apps throughout the book
Who This Book Is For
Are you trying to start a career in programming, but havent found the right way in? Do you have a great idea for an app, but dont know how to make it a reality? Or maybe youre just frustrated that "to learn Android, you must know Android." If so, Android Programming for Beginners is for you. You dont need any programming experience to follow along with this book, just a computer and a sense of adventure.

What You Will Learn
  • Master the fundamentals of coding Java for Android
  • Install and set up your Android development environment
  • Build functional user interfaces with the Android Studio visual designer
  • Add user interaction, data captures, sound, and animation to your apps
  • Manage your apps data using the built-in Android SQLite database
  • Find out about the design patterns used by professionals to make top-grade applications
  • Build, deploy, and publish real Android applications to the Google Play marketplace
In Detail
Android is the most popular OS in the world. There are millions of devices accessing tens of thousands of applications. It is many peoples entry point into the world of technology; it is an operating system for everyone. Despite this, the entry-fee to actually make Android applications is usually a computer science degree, or five years worth of Java experience.

Android Programming for Beginners will be your companion to create Android applications from scratch-whether youre looking to start your programming career, make an application for work, be reintroduced to mobile development, or are just looking to program for fun. We will introduce you to all the fundamental concepts of programming in an Android context, from the Java basics to working with the Android API. All examples are created from within Android Studio, the official Android development environment that helps supercharge your application development process.

After this crash-course, well dive deeper into Android programming and youll learn how to create applications with a professional-standard UI through fragments, make location-aware apps with Google Maps integration, and store your users data with SQLite. In addition, youll see how to make your apps multilingual, capture images from a devices camera, and work with graphics, sound, and animations too.

By the end of this book, youll be ready to start building your own custom applications in Android and Java.

Read More..

Wednesday, April 6, 2016

Android communicate with Arduino HC 06 Bluetooth Module part II

Its my old post "Android example to communicate with Bluetooth device, HC-06 Bluetooth Module", to make a Android phone communicate with Arduino + HC-06 Bluetooth Module. In the example, the first byte always lost.

After more test, I found that the first byte not lost. Because the byte stream sent from Arduino not in sync with Android side, and the bytes may be separated in different slot received in Android ThreadConnected, and the old slot covered by the new slot.

To show it, I modify it not to clear the TextView.


MainActivity.java
/*
Android Example to connect to and communicate with Bluetooth
In this exercise, the target is a Arduino Due + HC-06 (Bluetooth Module)

Ref:
- Make BlueTooth connection between Android devices
http://android-er.blogspot.com/2014/12/make-bluetooth-connection-between.html
- Bluetooth communication between Android devices
http://android-er.blogspot.com/2014/12/bluetooth-communication-between-android.html
*/
package com.example.androidbtcontrol;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Set;
import java.util.UUID;

public class MainActivity extends ActionBarActivity {

private static final int REQUEST_ENABLE_BT = 1;

BluetoothAdapter bluetoothAdapter;

ArrayList<BluetoothDevice> pairedDeviceArrayList;

TextView textInfo, textStatus, textByteCnt;
ListView listViewPairedDevice;
LinearLayout inputPane;
EditText inputField;
Button btnSend, btnClear;

ArrayAdapter<BluetoothDevice> pairedDeviceAdapter;
private UUID myUUID;
private final String UUID_STRING_WELL_KNOWN_SPP =
"00001101-0000-1000-8000-00805F9B34FB";

ThreadConnectBTdevice myThreadConnectBTdevice;
ThreadConnected myThreadConnected;

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

textInfo = (TextView)findViewById(R.id.info);
textStatus = (TextView)findViewById(R.id.status);
textByteCnt = (TextView)findViewById(R.id.textbyteCnt);
listViewPairedDevice = (ListView)findViewById(R.id.pairedlist);

inputPane = (LinearLayout)findViewById(R.id.inputpane);
inputField = (EditText)findViewById(R.id.input);
btnSend = (Button)findViewById(R.id.send);
btnSend.setOnClickListener(new View.OnClickListener(){

@Override
public void onClick(View v) {
if(myThreadConnected!=null){
byte[] bytesToSend = inputField.getText().toString().getBytes();
myThreadConnected.write(bytesToSend);
byte[] NewLine = " ".getBytes();
myThreadConnected.write(NewLine);
}
}});

btnClear = (Button)findViewById(R.id.clear);
btnClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textStatus.setText("");
textByteCnt.setText("");
}
});

if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)){
Toast.makeText(this,
"FEATURE_BLUETOOTH NOT support",
Toast.LENGTH_LONG).show();
finish();
return;
}

//using the well-known SPP UUID
myUUID = UUID.fromString(UUID_STRING_WELL_KNOWN_SPP);

bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
Toast.makeText(this,
"Bluetooth is not supported on this hardware platform",
Toast.LENGTH_LONG).show();
finish();
return;
}

String stInfo = bluetoothAdapter.getName() + " " +
bluetoothAdapter.getAddress();
textInfo.setText(stInfo);
}

@Override
protected void onStart() {
super.onStart();

//Turn ON BlueTooth if it is OFF
if (!bluetoothAdapter.isEnabled()) {
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
}

setup();
}

private void setup() {
Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
pairedDeviceArrayList = new ArrayList<BluetoothDevice>();

for (BluetoothDevice device : pairedDevices) {
pairedDeviceArrayList.add(device);
}

pairedDeviceAdapter = new ArrayAdapter<BluetoothDevice>(this,
android.R.layout.simple_list_item_1, pairedDeviceArrayList);
listViewPairedDevice.setAdapter(pairedDeviceAdapter);

listViewPairedDevice.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
BluetoothDevice device =
(BluetoothDevice) parent.getItemAtPosition(position);
Toast.makeText(MainActivity.this,
"Name: " + device.getName() + " "
+ "Address: " + device.getAddress() + " "
+ "BondState: " + device.getBondState() + " "
+ "BluetoothClass: " + device.getBluetoothClass() + " "
+ "Class: " + device.getClass(),
Toast.LENGTH_LONG).show();

textStatus.setText("start ThreadConnectBTdevice");
myThreadConnectBTdevice = new ThreadConnectBTdevice(device);
myThreadConnectBTdevice.start();
}
});
}
}

@Override
protected void onDestroy() {
super.onDestroy();

if(myThreadConnectBTdevice!=null){
myThreadConnectBTdevice.cancel();
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==REQUEST_ENABLE_BT){
if(resultCode == Activity.RESULT_OK){
setup();
}else{
Toast.makeText(this,
"BlueTooth NOT enabled",
Toast.LENGTH_SHORT).show();
finish();
}
}
}

//Called in ThreadConnectBTdevice once connect successed
//to start ThreadConnected
private void startThreadConnected(BluetoothSocket socket){

myThreadConnected = new ThreadConnected(socket);
myThreadConnected.start();
}

/*
ThreadConnectBTdevice:
Background Thread to handle BlueTooth connecting
*/
private class ThreadConnectBTdevice extends Thread {

private BluetoothSocket bluetoothSocket = null;
private final BluetoothDevice bluetoothDevice;


private ThreadConnectBTdevice(BluetoothDevice device) {
bluetoothDevice = device;

try {
bluetoothSocket = device.createRfcommSocketToServiceRecord(myUUID);
textStatus.setText("bluetoothSocket: " + bluetoothSocket);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

@Override
public void run() {
boolean success = false;
try {
bluetoothSocket.connect();
success = true;
} catch (IOException e) {
e.printStackTrace();

final String eMessage = e.getMessage();
runOnUiThread(new Runnable() {

@Override
public void run() {
textStatus.setText("something wrong bluetoothSocket.connect(): " + eMessage);
}
});

try {
bluetoothSocket.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}

if(success){
//connect successful
final String msgconnected = "connect successful: "
+ "BluetoothSocket: " + bluetoothSocket + " "
+ "BluetoothDevice: " + bluetoothDevice;

runOnUiThread(new Runnable() {

@Override
public void run() {
textStatus.setText("");
textByteCnt.setText("");
Toast.makeText(MainActivity.this, msgconnected, Toast.LENGTH_LONG).show();

listViewPairedDevice.setVisibility(View.GONE);
inputPane.setVisibility(View.VISIBLE);
}
});

startThreadConnected(bluetoothSocket);

}else{
//fail
}
}

public void cancel() {

Toast.makeText(getApplicationContext(),
"close bluetoothSocket",
Toast.LENGTH_LONG).show();

try {
bluetoothSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

/*
ThreadConnected:
Background Thread to handle Bluetooth data communication
after connected
*/
private class ThreadConnected extends Thread {
private final BluetoothSocket connectedBluetoothSocket;
private final InputStream connectedInputStream;
private final OutputStream connectedOutputStream;

public ThreadConnected(BluetoothSocket socket) {
connectedBluetoothSocket = socket;
InputStream in = null;
OutputStream out = null;

try {
in = socket.getInputStream();
out = socket.getOutputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

connectedInputStream = in;
connectedOutputStream = out;
}

@Override
public void run() {
byte[] buffer = new byte[1024];
int bytes;

String strRx = "";

while (true) {
try {
bytes = connectedInputStream.read(buffer);
final String strReceived = new String(buffer, 0, bytes);
final String strByteCnt = String.valueOf(bytes) + " bytes received. ";

runOnUiThread(new Runnable(){

@Override
public void run() {
textStatus.append(strReceived);
textByteCnt.append(strByteCnt);
}});

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

final String msgConnectionLost = "Connection lost: "
+ e.getMessage();
runOnUiThread(new Runnable(){

@Override
public void run() {
textStatus.setText(msgConnectionLost);
}});
}
}
}

public void write(byte[] buffer) {
try {
connectedOutputStream.write(buffer);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void cancel() {
try {
connectedBluetoothSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}


layout/activity_main.xml
<LinearLayout 

android_layout_width="match_parent"
android_layout_height="match_parent"
android_paddingLeft="@dimen/activity_horizontal_margin"
android_paddingRight="@dimen/activity_horizontal_margin"
android_paddingTop="@dimen/activity_vertical_margin"
android_paddingBottom="@dimen/activity_vertical_margin"
android_orientation="horizontal"
tools_context=".MainActivity">

<LinearLayout
android_layout_width="0dp"
android_layout_height="match_parent"
android_layout_weight="1"
android_orientation="vertical">
<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/textbyteCnt"
android_layout_width="wrap_content"
android_layout_height="0dp"
android_layout_weight="1"/>
<TextView
android_id="@+id/status"
android_layout_width="wrap_content"
android_layout_height="0dp"
android_layout_weight="1"/>
</LinearLayout>

<LinearLayout
android_layout_width="0dp"
android_layout_height="match_parent"
android_layout_weight="1"
android_orientation="vertical">

<TextView
android_id="@+id/info"
android_textStyle="bold|italic"
android_layout_width="wrap_content"
android_layout_height="wrap_content"/>
<ListView
android_id="@+id/pairedlist"
android_layout_width="match_parent"
android_layout_height="match_parent"/>

<LinearLayout
android_id="@+id/inputpane"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_orientation="vertical"
android_visibility="gone">

<EditText
android_id="@+id/input"
android_layout_width="match_parent"
android_layout_height="wrap_content"/>
<Button
android_id="@+id/send"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_text="Sent"/>
<Button
android_id="@+id/clear"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_text="Clear"/>

</LinearLayout>

</LinearLayout>

</LinearLayout>


Have to modify AndroidManifest.xml to uses-permission of "android.permission.BLUETOOTH". Refer the exmple "Android example to communicate with Bluetooth device, HC-06 Bluetooth Module".

download filesDownload the files (Android Studio Format) .

download filesDownload and try APK.


For the Arduino side, refer to : Arduino-er - Connect Arduino Due with HC-06 (Bluetooth Module).


This Android App can be used to talk with PC, with FTDI USB-to-Serial adapter connected to HC-06. Refer to the next post "Android Bluetooth connect PC (running Windows10) via HC-06 BT Module and FTDI US-to-Serial adapter".

more:
- Android Bluetooth Terminal to login Raspberry Pi + HC-06

Read More..

Wednesday, March 30, 2016

DialogFragment example something wrong on Android 6 Marshmallow emulator

Its a example to implement DialogFragment. Please check the screenshots of running on Android Emulators of Android 4.1 Jelly Bean with API 16, Android 5.1 Lollipop with API 22 and Android 6.0 Marshmallow with API 23. If the emulator of Android 6.0 Marshmallow work properly, DialogFragment display wrong on Marshmallow.



Check this video:


layout/fragment_dialog.xml, DialogFragment layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout

android_padding="10dp"
android_orientation="vertical"
android_layout_width="match_parent"
android_layout_height="match_parent">

<ImageView
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_src="@mipmap/ic_launcher"/>

<TextView
android_id="@+id/dialogtext"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_textSize="20dp"
android_textStyle="italic|bold"/>

</LinearLayout>

MainActivity.java with DialogFragment.
package com.blogspot.android_er.androiddialogfragment;

import android.app.DialogFragment;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

EditText inputTextField;

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

inputTextField = (EditText)findViewById(R.id.inputtext);
Button btnOpen = (Button)findViewById(R.id.opendialog);
btnOpen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog();
}
});
}

void showDialog() {
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment prev = getFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);

String inputText = inputTextField.getText().toString();

DialogFragment newFragment = MyDialogFragment.newInstance(inputText);
newFragment.show(ft, "dialog");

}

public static class MyDialogFragment extends DialogFragment {

String mText;

static MyDialogFragment newInstance(String text) {
MyDialogFragment f = new MyDialogFragment();

Bundle args = new Bundle();
args.putString("text", text);
f.setArguments(args);

return f;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mText = getArguments().getString("text");

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View dialogView = inflater.inflate(R.layout.fragment_dialog, container, false);
TextView dialogText = (TextView)dialogView.findViewById(R.id.dialogtext);
dialogText.setText(mText);

return dialogView;
}
}
}


layout/activity_main.xml, main layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout


android_layout_width="match_parent"
android_layout_height="match_parent"
android_padding="16dp"
android_orientation="vertical"
tools_context=".MainActivity"
android_background="#808080">

<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" />

<EditText
android_id="@+id/inputtext"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_hint="Type something"/>
<Button
android_id="@+id/opendialog"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_text="Open DialogFragment"
android_textAllCaps="false"/>
</LinearLayout>


reference: http://developer.android.com/reference/android/app/DialogFragment.html


To fixed it, edit layout/fragment_dialog.xml, modify android:layout_width of <ImageView> from "wrap_content" to "match_parent".

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout

android_padding="10dp"
android_orientation="vertical"
android_layout_width="match_parent"
android_layout_height="match_parent">

<ImageView
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_src="@mipmap/ic_launcher"/>

<TextView
android_id="@+id/dialogtext"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_textSize="20dp"
android_textStyle="italic|bold"/>

</LinearLayout>

android:layout_width="wrap_content"

android:layout_width="match_parent"

Read More..

Friday, March 4, 2016

Android Studio Development Essentials Android 6 Edition

Android Studio Development Essentials - Android 6 Edition

Fully updated for Android 6, the goal of this book is to teach the skills necessary to develop Android based applications using the Android Studio Integrated Development Environment (IDE) and the Android 6 Software Development Kit (SDK).

Beginning with the basics, this book provides an outline of the steps necessary to set up an Android development and testing environment. An overview of Android Studio is included covering areas such as tool windows, the code editor and the Designer tool. An introduction to the architecture of Android is followed by an in-depth look at the design of Android applications and user interfaces using the Android Studio environment. More advanced topics such as database management, content providers and intents are also covered, as are touch screen handling, gesture recognition, camera access and the playback and recording of both video and audio. This edition of the book also covers printing, transitions and cloud-based file storage.

The concepts of material design are also covered in detail, including the use of floating action buttons, Snackbars, tabbed interfaces, card views, navigation drawers and collapsing toolbars.

In addition to covering general Android development techniques, the book also includes Google Play specific topics such as implementing maps using the Google Maps Android API, in-app billing and submitting apps to the Google Play Developer Console.

Chapters also cover advanced features of Android Studio such as Gradle build configuration and the implementation of build variants to target multiple Android device types from a single project code base.

Assuming you already have some Java programming experience, are ready to download Android Studio and the Android SDK, have access to a Windows, Mac or Linux system and ideas for some apps to develop, you are ready to get started.

Read More..

Monday, February 29, 2016

Android example code using ColorFilter



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

import android.graphics.ColorFilter;
import android.graphics.ColorMatrixColorFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.SeekBar;

public class MainActivity extends AppCompatActivity {

ImageView imageView;
SeekBar redBar, greenBar, blueBar;

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

imageView = (ImageView)findViewById(R.id.iv);
redBar = (SeekBar)findViewById(R.id.redbar);
greenBar = (SeekBar)findViewById(R.id.greenbar);
blueBar = (SeekBar)findViewById(R.id.bluebar);

redBar.setOnSeekBarChangeListener(colorBarChangeListener);
greenBar.setOnSeekBarChangeListener(colorBarChangeListener);
blueBar.setOnSeekBarChangeListener(colorBarChangeListener);

setColorFilter(imageView);
}

SeekBar.OnSeekBarChangeListener colorBarChangeListener
= new SeekBar.OnSeekBarChangeListener(){

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
setColorFilter(imageView);
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {

}
};

private void setColorFilter(ImageView iv){

/*
* 5x4 matrix for transforming the color+alpha components of a Bitmap.
* The matrix is stored in a single array, and its treated as follows:
* [ a, b, c, d, e,
* f, g, h, i, j,
* k, l, m, n, o,
* p, q, r, s, t ]
*
* When applied to a color [r, g, b, a], the resulting color is computed
* as (after clamping)
* R = a*R + b*G + c*B + d*A + e;
* G = f*R + g*G + h*B + i*A + j;
* B = k*R + l*G + m*B + n*A + o;
* A = p*R + q*G + r*B + s*A + t;
*/

float redValue = ((float)redBar.getProgress())/255;
float greenValue = ((float)greenBar.getProgress())/255;
float blueValue = ((float)blueBar.getProgress())/255;

float[] colorMatrix = {
redValue, 0, 0, 0, 0, //red
0, greenValue, 0, 0, 0, //green
0, 0, blueValue, 0, 0, //blue
0, 0, 0, 1, 0 //alpha
};

ColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix);
iv.setColorFilter(colorFilter);
}

}


layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout


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_gravity="center_horizontal"
android_autoLink="web"
android_text="http://android-er.blogspot.com/"
android_textStyle="bold" />

<ImageView
android_id="@+id/iv"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_src="@mipmap/ic_launcher"/>

<SeekBar
android_id="@+id/redbar"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_max="255"
android_progress="255"/>
<SeekBar
android_id="@+id/greenbar"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_max="255"
android_progress="255"/>
<SeekBar
android_id="@+id/bluebar"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_max="255"
android_progress="255"/>
</LinearLayout>


Related:
- Convert ImageView to black and white, and set brightness, using ColorFilter

Read More..