Thursday, April 9, 2015

Android coding example to Set fontFamily

Android 4.1 adds several more variants of the Roboto font style for a total of 10 variants, and they're all usable by apps. Your apps now have access to the full set of both light and condensed variants. - http://developer.android.com/about/versions/android-4.1.html

Example:


MainActivity.java
package com.example.androidfront;

import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.Spinner;
import android.graphics.Typeface;
import android.os.Bundle;

public class MainActivity extends ActionBarActivity {

String typeFaceName[] = {
"sans-serif",
"sans-serif-light",
"sans-serif-condensed",
"sans-serif-thin",
"sans-serif-medium"};

CheckBox checkBold, checkItalic;
Spinner selTypeFace;
EditText editArea;

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

editArea = (EditText)findViewById(R.id.editarea);

checkBold = (CheckBox)findViewById(R.id.boldsel);
checkBold.setOnCheckedChangeListener(new OnCheckedChangeListener(){

@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
updateFonts();
}});

checkItalic = (CheckBox)findViewById(R.id.italicsel);
checkItalic.setOnCheckedChangeListener(new OnCheckedChangeListener(){

@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
updateFonts();
}});

selTypeFace = (Spinner)findViewById(R.id.typefacesel);
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, typeFaceName);
adapter.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
selTypeFace.setAdapter(adapter);
selTypeFace.setOnItemSelectedListener(new OnItemSelectedListener(){

@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
updateFonts();
}

@Override
public void onNothingSelected(AdapterView<?> parent) {}});

}

private void updateFonts(){

int tfSel = selTypeFace.getSelectedItemPosition();
String selTypeFaceName = typeFaceName[tfSel];

int style;

if(!checkBold.isChecked() && !checkItalic.isChecked()){
style = Typeface.NORMAL;
}else if(checkBold.isChecked() && !checkItalic.isChecked()){
style = Typeface.BOLD;
}else if(!checkBold.isChecked() && checkItalic.isChecked()){
style = Typeface.ITALIC;
}else{
style = Typeface.BOLD_ITALIC;
}

Typeface tf = Typeface.create(selTypeFaceName, style);
editArea.setTypeface(tf);

}

}

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"
tools:context="com.example.androidfront.MainActivity"
android:orientation="vertical" >

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

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >

<TextView
android:fontFamily="sans-serif"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/test"/>
<TextView
android:fontFamily="sans-serif-light"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/test"/>
<TextView
android:fontFamily="sans-serif-condensed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/test"/>
<TextView
android:fontFamily="sans-serif-thin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/test"/>
<TextView
android:fontFamily="sans-serif-medium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/test"/>

</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >

<Spinner
android:id="@+id/typefacesel"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<CheckBox
android:id="@+id/boldsel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="BOLD"/>
<CheckBox
android:id="@+id/italicsel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ITALIC"/>
<EditText
android:id="@+id/editarea"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:singleLine="false"
android:gravity="top"
android:background="#E0E0E0"/>
</LinearLayout>


</LinearLayout>
</LinearLayout>

string resources of "test" is needed, read HERE.

Tuesday, April 7, 2015

Example to set fonts for Android

How to set fonts (Typeface and style) on Android using xml and programmatically.



package com.example.androidfront;

import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.Spinner;
import android.graphics.Typeface;
import android.os.Bundle;

public class MainActivity extends ActionBarActivity {

String typeFaceName[] = {
"DEFAULT",
"DEFAULT_BOLD",
"MONOSPACE",
"SANS_SERIF",
"SERIF"};

Typeface typeFace[] = {
Typeface.DEFAULT,
Typeface.DEFAULT_BOLD,
Typeface.MONOSPACE,
Typeface.SANS_SERIF,
Typeface.SERIF};

CheckBox checkBold, checkItalic;
Spinner selTypeFace;
EditText editArea;

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

editArea = (EditText)findViewById(R.id.editarea);

checkBold = (CheckBox)findViewById(R.id.boldsel);
checkBold.setOnCheckedChangeListener(new OnCheckedChangeListener(){

@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
updateFonts();
}});

checkItalic = (CheckBox)findViewById(R.id.italicsel);
checkItalic.setOnCheckedChangeListener(new OnCheckedChangeListener(){

@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
updateFonts();
}});

selTypeFace = (Spinner)findViewById(R.id.typefacesel);
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, typeFaceName);
adapter.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
selTypeFace.setAdapter(adapter);
selTypeFace.setOnItemSelectedListener(new OnItemSelectedListener(){

@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
updateFonts();
}

@Override
public void onNothingSelected(AdapterView<?> parent) {}});

}

private void updateFonts(){
int tfSel = selTypeFace.getSelectedItemPosition();
Typeface tf = typeFace[tfSel];
editArea.setTypeface(tf);

int style;

if(!checkBold.isChecked() && !checkItalic.isChecked()){
style = Typeface.NORMAL;
}else if(checkBold.isChecked() && !checkItalic.isChecked()){
style = Typeface.BOLD;
}else if(!checkBold.isChecked() && checkItalic.isChecked()){
style = Typeface.ITALIC;
}else{
style = Typeface.BOLD_ITALIC;
}

editArea.setTypeface(tf, style);
}

}

<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"
tools:context="com.example.androidfront.MainActivity"
android:orientation="vertical" >

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

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >

<TextView
android:typeface="normal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/test"/>
<TextView
android:typeface="monospace"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/test"/>
<TextView
android:typeface="sans"
android:textStyle="italic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/test"/>
<TextView
android:typeface="serif"
android:textStyle="bold|italic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/test"/>

</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >

<Spinner
android:id="@+id/typefacesel"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<CheckBox
android:id="@+id/boldsel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="BOLD"/>
<CheckBox
android:id="@+id/italicsel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ITALIC"/>
<EditText
android:id="@+id/editarea"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:singleLine="false"
android:gravity="top"
android:background="#E0E0E0"/>
</LinearLayout>


</LinearLayout>
</LinearLayout>

Add <string name="test"> in /res/values/strings.xml.
<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">AndroidFront</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="test">ABCDEfghijKLMNOpqrstUVWXYz1234567890</string>
</resources>


Tuesday, March 17, 2015

Get number of available cores and cpu info

The method Runtime.availableProcessors() returns the number of processor cores available to the VM.

And the /proc/cpuinfo hold infoprmation about the CPU, you can read it as text file.



Example:
package com.example.androidcpuinfo;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

TextView cores, cpuinfo;

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

Runtime runtime = Runtime.getRuntime();
int availableProcessors = runtime.availableProcessors();
cores.setText("You have " + availableProcessors + " availableProcessors");

//Read text file "/proc/cpuinfo"
String file_cpuinfo = "/proc/cpuinfo";
String info = "";
try {
FileReader fileReader = new FileReader(file_cpuinfo);
BufferedReader bufferReader = new BufferedReader(fileReader);

String line;
try {
while((line = bufferReader.readLine()) != null)
{
info += line + "\n";
}
cpuinfo.setText(info);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

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

}

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

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

<TextView
android:id="@+id/cores"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textStyle="italic" />

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >

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

</LinearLayout>

Saturday, March 7, 2015

Create mirror bitmap using Matrix



package com.example.androidmirrorimage;

import android.support.v7.app.ActionBarActivity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.widget.ImageView;

public class MainActivity extends ActionBarActivity {

ImageView image1, image2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image1 = (ImageView) findViewById(R.id.image1);
image2 = (ImageView) findViewById(R.id.image2);

Bitmap bm = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);

image1.setImageBitmap(bm);
image2.setImageBitmap(getMirrorBitmap(bm));

}

private Bitmap getMirrorBitmap(Bitmap src) {

Matrix matrix = new Matrix();
matrix.preScale(1, -1);
Bitmap result = Bitmap.createBitmap(
src, 0, 0, src.getWidth(), src.getHeight(), matrix, false);
return 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:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.androidmirrorimage.MainActivity" >

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android-coding.blogspot.com"
android:textSize="28dp"
android:textStyle="bold" />

<ImageView
android:id="@+id/image1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />

<ImageView
android:id="@+id/image2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />

</LinearLayout>

Tuesday, February 24, 2015

Introduction to Android Studio

A high level introduction to Android Studio, the new IDE for Android application development. Learn why you should migrate your projects to Android Studio now and how it can help you be more productive as a developer. Rich layout editor, handy suggestions and fixes, new Android project view - these are just some of the things you can expect from the IDE, which is built on the successful IntelliJ IDEA.

Monday, February 23, 2015

Custom ImageView to show touched portion only

Here is a custom ImageView (TouchImageView), override onTouchEvent(MotionEvent event) and onDraw(Canvas canvas) methods, to make it display the touched portion only.


package com.example.androidtouchimageview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;

public class TouchImageView extends ImageView {

private float radius = 0;
private Paint paint = null;

private float x;
private float y;
private boolean touched = false;

public TouchImageView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}

public TouchImageView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}

public TouchImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// TODO Auto-generated constructor stub
}

@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
touched = (action == MotionEvent.ACTION_DOWN
|| action == MotionEvent.ACTION_MOVE);
x = event.getX();
y = event.getY();

int w = getWidth();
int h = getHeight();
if(w>=h){
radius = h * event.getSize();
}else{
radius = w * event.getSize();
}

invalidate();
return true;
}

@Override
protected void onDraw(Canvas canvas) {
if (paint == null) {
Bitmap bm = Bitmap.createBitmap(
getWidth(),
getHeight(),
Bitmap.Config.ARGB_8888);
Canvas originalCanvas = new Canvas(bm);
super.onDraw(originalCanvas);

Shader shader = new BitmapShader(bm,
Shader.TileMode.CLAMP,
Shader.TileMode.CLAMP);

paint = new Paint();
paint.setShader(shader);
}

canvas.drawColor(getSolidColor());
if (touched) {
canvas.drawCircle(x, y, radius, paint);
}
}

}


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.androidtouchimageview.MainActivity" >

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android-coding.blogspot.com"
android:textSize="28dp"
android:textStyle="bold" />

<com.example.androidtouchimageview.TouchImageView
android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />

<com.example.androidtouchimageview.TouchImageView
android:id="@+id/image2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher" />

</LinearLayout>

Sunday, February 22, 2015

Implement color animation with ValueAnimator

To implement color animation with ValueAnimator:


package com.example.valueanimatorofcolor;

import com.example.objectanimatorofargb.R;

import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {

TextView title;
Button btnStart;
ImageView image;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
title = (TextView) findViewById(R.id.title);
image = (ImageView) findViewById(R.id.image);
btnStart = (Button) findViewById(R.id.start);

btnStart.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
startColorAnimation(image);
}});
}

private void startColorAnimation(View view){
//int colorStart = 0xFFffffff;
int colorStart = view.getSolidColor();
int colorEnd = 0xFF000000;

ValueAnimator colorAnim = ObjectAnimator.ofInt(
view, "backgroundColor", colorStart, colorEnd);
colorAnim.setDuration(2000);
colorAnim.setEvaluator(new ArgbEvaluator());
colorAnim.setRepeatCount(1);
colorAnim.setRepeatMode(ValueAnimator.REVERSE);
colorAnim.start();
}

}

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

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android-coding.blogspot.com"
android:textSize="28dp"
android:textStyle="bold" />

<Button
android:id="@+id/start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start Animation" />

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

</LinearLayout>