Jan 27, 2015

Android EffectFactory example

It's a good example of using android.media.effect.EffectFactory. It can be found in your Android SDK folder, normally at:
.../Android/sdk/samples/android-21/legacy/HelloEffects

The EffectFactory class defines the list of available Effects, and provides functionality to inspect and instantiate them.


Jan 21, 2015

Display emoji from byte[], encoded using UTF-8

Example show how to display emoji on TextView, encoded from byte[].


To convert byte array to String using charset of UTF-8, we can call the constructor of String:
new String(bytes, "UTF-8")

Example:
package com.example.androidemoji;

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

public class MainActivity extends ActionBarActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  TextView emojiText = (TextView)findViewById(R.id.emojitext);
  
  //byte[] bytes = {(byte) 0xF0, (byte) 0x9F, (byte) 0x98, (byte) 0x81};
  //byte[] bytes = {(byte) 0xF0, (byte) 0x9F, (byte) 0x98, (byte) 0x84};
  byte[] bytes = {(byte) 0xF0, (byte) 0x9F, (byte) 0x98, (byte) 0x89};
  try {
   emojiText.setText(new String(bytes, "UTF-8"));
  } catch (UnsupportedEncodingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
  //Direct type in the emoji
  //emojiText.setText("😁");  //maybe cannot displayed on browser

 }

}

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

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

    <TextView
        android:id="@+id/emojitext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="100dp"
        android:textStyle="bold" />

</LinearLayout>

The page Emoji Unicode Tables show commonly-supported Emoji that map to standardized Unicode characters.

Jan 13, 2015

Android ListView, scroll to specified position

How to scroll Android ListView to top, bottom or specified position.

package com.example.androidlistviewscrolling;

import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.os.Bundle;

public class MainActivity extends ActionBarActivity {

 String[] months = { "January", "February", "March", "April", "May", "June",
   "July", "August", "September", "October", "November", "December" };

 ListView listView;
 int toPosition = months.length;
 
 Button btnScrollTop, btnScrollBottom, btnScrollTo;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  listView = (ListView) findViewById(R.id.listview);
  ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
    android.R.layout.simple_list_item_1, months);
  listView.setAdapter(adapter);
  
  listView.setOnItemClickListener(new OnItemClickListener(){

   @Override
   public void onItemClick(AdapterView<?> parent, View view,
     int position, long id) {
    toPosition = position;
    btnScrollTo.setText("Scroll to position " + toPosition);
   }});
  
  btnScrollTop = (Button)findViewById(R.id.scrolltop);
  btnScrollTop.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    listView.smoothScrollToPosition(0);
   }});
  
  btnScrollBottom = (Button)findViewById(R.id.scrollbottom);
  btnScrollBottom.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    listView.smoothScrollToPosition(listView.getCount()-1);
   }});
  
  btnScrollTo = (Button)findViewById(R.id.scrollto);
  btnScrollTo.setText("Scroll to position " + toPosition);
  btnScrollTo.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    listView.smoothScrollToPosition(toPosition);
   }});
 }

}

<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="horizontal"
    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.androidlistviewscrolling.MainActivity" >

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

        <ListView
            android:id="@+id/listview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <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:textStyle="bold"
            android:textSize="28dp"
            android:text="android-coding.blogspot.com" />
        
        <Button
            android:id="@+id/scrolltop"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Scroll to Top" />
        <Button
            android:id="@+id/scrollbottom"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Scroll to Bottom" />
        <Button
            android:id="@+id/scrollto"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

</LinearLayout>

Jan 10, 2015

List running service of Android device

The method getRunningServices(int maxNum) of ActivityManager return a list of the services that are currently running. (Note: this method is only intended for debugging or implementing service management type user interfaces)


package com.example.androidgetrunning;

import java.util.List;

import android.support.v7.app.ActionBarActivity;
import android.text.method.ScrollingMovementMethod;
import android.app.ActivityManager;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  //setContentView(R.layout.activity_main);
  
  ActivityManager activityManager = (ActivityManager)getSystemService(ACTIVITY_SERVICE);

  /*
   * maxNum: the maximum number of entries to return in the list. 
   * The actual number returned may be smaller, 
   * depending on how many services are running.
   */
  int maxNum = 100;
  List<ActivityManager.RunningServiceInfo> list = activityManager.getRunningServices(maxNum);
  
  StringBuilder info = new StringBuilder();
  info.append("android-coding.blogspot.com" + "\n\n");
  info.append("no. of running service: " + list.size() + "\n\n");
  for(int i=0; i<list.size(); i++){
   info.append(list.get(i).service + "\n\n");
  }
  
  TextView texView = new TextView(this);
  texView.setMovementMethod(new ScrollingMovementMethod());
  texView.setText(info);
  setContentView(texView);
 }

}

List running application processes of Android device

Call getSystemService(ACTIVITY_SERVICE) to get instance of the ActivityManager. With it, your app can interact with the overall activities running in the system. The method getRunningAppProcesses() return a list of application processes that are running on the device. (Note: this method is only intended for debugging or building a user-facing process management UI)

Example:
package com.example.androidgetrunning;

import java.util.List;

import android.support.v7.app.ActionBarActivity;
import android.text.method.ScrollingMovementMethod;
import android.app.ActivityManager;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  ActivityManager activityManager = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
  
  List<ActivityManager.RunningAppProcessInfo> list = activityManager.getRunningAppProcesses();
  
  StringBuilder info = new StringBuilder();
  info.append("android-coding.blogspot.com" + list.size() + "\n\n");
  info.append("no. of running application processes: " + list.size() + "\n");
  for(int i=0; i<list.size(); i++){
   info.append(list.get(i).processName + "\n");
  }
  
  TextView texView = new TextView(this);
  texView.setMovementMethod(new ScrollingMovementMethod());
  texView.setText(info);
  setContentView(texView);
 }

}


Android Vector Graphics

Tenghui Zhu presents the VectorDrawable and AnimatedVectorDrawable APIs in Android L, which can provide an easy and compact way to represent the drawables. These APIs also helps adding various animations to the drawables, including path morphing.

To learn more about vector drawable support in Android, check out http://goo.gl/fj7gv2


Infolinks In Text Ads