Nov 28, 2012

Display custom TrueType Font (TTF)

To display text using custom TTF; create /assets/fonts folder and save your ttf files in it. Create custom Typeface using Typeface.createFromAsset() method and apply it on views by calling setTypeface() method.

Display custom TrueType Font (TTF)


package com.example.androidttf;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Typeface;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  TextView testText = (TextView)findViewById(R.id.testtext);
  Button testButton = (Button)findViewById(R.id.testbutton);
  EditText testEditText = (EditText)findViewById(R.id.testedittext);
  
  Typeface typeface_Abbeyline = Typeface.createFromAsset(getAssets(), "fonts/Abbeyline.ttf");
  Typeface typeface_36daysag = Typeface.createFromAsset(getAssets(), "fonts/36daysag.ttf");
  Typeface typeface_AtomicClockRadio = Typeface.createFromAsset(getAssets(), "fonts/AtomicClockRadio.ttf");
  testText.setTypeface(typeface_Abbeyline);
  testButton.setTypeface(typeface_36daysag);
  testEditText.setTypeface(typeface_AtomicClockRadio);
 }

}


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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    <TextView
        android:id="@+id/testtext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="I'm a TextView" />
    <Button
        android:id="@+id/testbutton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="I'm a Button" />
    <EditText
        android:id="@+id/testedittext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>


Nov 22, 2012

Monitor battery level

This code monitor battery level by registering our BroadcastReceiver with IntentFilter for Intent.ACTION_BATTERY_CHANGED.

Monitor battery level of Android device


package com.example.androidbattery;

import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.widget.TextView;

public class MainActivity extends Activity {
 
 TextView batteryStatus;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  batteryStatus = (TextView)findViewById(R.id.batterystatus);
  
  IntentFilter batteryFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
  registerReceiver(batteryMonitor, batteryFilter);
 }

 private BroadcastReceiver batteryMonitor
 = new BroadcastReceiver(){

  @Override
  public void onReceive(Context arg0, Intent arg1) {
   int batteryLevel = arg1.getIntExtra("level", 0);
   batteryStatus.setText("Battery level = " + String.valueOf(batteryLevel) + "%");
  }};

}


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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    <TextView
        android:id="@+id/batterystatus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
        
</LinearLayout>


Nov 1, 2012

Remove View dynamically using Java code

Last post "Insert ImageView dynamically using Java code", by clicking on buttons. Removing function is added here to remove clicked ImageView from parent ViewGroup.

Remove View dynamically using Java code


Modify the Java code from last post, adding OnClickListener to the ImageViews to remove itself from its parent.
package com.example.androidinsertimages;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MainActivity extends Activity {
 
 Button addinHorizontalScrollView, addinScrollView;
 LinearLayout inHorizontalScrollView, inScrollView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        inHorizontalScrollView = (LinearLayout)findViewById(R.id.inhorizontalscrollview);
        inScrollView = (LinearLayout)findViewById(R.id.inscrollview);
        
        addinHorizontalScrollView = (Button)findViewById(R.id.addinhorizontalscrollview);
        addinHorizontalScrollView.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View arg0) {
    addImageView(inHorizontalScrollView);
   }});
        
        addinScrollView = (Button)findViewById(R.id.addinscrollview);
        addinScrollView.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View arg0) {
    addImageView(inScrollView);
   }});
        
    }
    
    private void addImageView(LinearLayout layout){
     ImageView imageView = new ImageView(this);
     imageView.setImageResource(R.drawable.ic_launcher);
     layout.addView(imageView);
     
     imageView.setOnClickListener(viewOnClickListener);
    }

    OnClickListener viewOnClickListener
    = new OnClickListener(){

  @Override
  public void onClick(View v) {
   ViewGroup parent = (ViewGroup)v.getParent();
   parent.removeView(v);
  }
    };
}



The layout file refer to last post.


Oct 29, 2012

Insert ImageView dynamically using Java code

Here demonstrate how to create and add ImageView in LinearLayout (inside HorizontalScrollView/ScrollView) dynamically using Java code.

Insert ImageView dynamically using Java code


package com.example.androidinsertimages;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MainActivity extends Activity {
 
 Button addinHorizontalScrollView, addinScrollView;
 LinearLayout inHorizontalScrollView, inScrollView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        inHorizontalScrollView = (LinearLayout)findViewById(R.id.inhorizontalscrollview);
        inScrollView = (LinearLayout)findViewById(R.id.inscrollview);
        
        addinHorizontalScrollView = (Button)findViewById(R.id.addinhorizontalscrollview);
        addinHorizontalScrollView.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View arg0) {
    addImageView(inHorizontalScrollView);
   }});
        
        addinScrollView = (Button)findViewById(R.id.addinscrollview);
        addinScrollView.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View arg0) {
    addImageView(inScrollView);
   }});
        
    }
    
    private void addImageView(LinearLayout layout){
     ImageView imageView = new ImageView(this);
     imageView.setImageResource(R.drawable.ic_launcher);
     layout.addView(imageView);
    }

}


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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        tools:context=".MainActivity" />
    
    <Button
        android:id="@+id/addinhorizontalscrollview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Add in HorizontalScrollView"/>
    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:id="@+id/inhorizontalscrollview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
        </LinearLayout>
    </HorizontalScrollView>
    
    <Button
        android:id="@+id/addinscrollview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Add in ScrollView"/>
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:id="@+id/inscrollview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">
        </LinearLayout>
    </ScrollView>

</LinearLayout>


We can also remove the ImageViews using Java code, refer to next post, "Remove View dynamically using Java code".


Oct 16, 2012

Get video width and height of MediaPlayer and update SurfaceView LayoutParams accordingly

Last article demonstrate how to Play Youtube 3gp video with MediaPlayer in SurfaceView. It can be noticed that the video is fill in parent view, because "fill_parent" are defined in layout.

To adjust the video fit the Youtube video width and height, we can implement our custom OnVideoSizeChangedListener(), and call myMediaPlayer.getVideoWidth() and myMediaPlayer.getVideoHeight() to get the video width and height. After then, update SurfaceView LayoutParams with correct size accordingly.

MediaPlay with true size

Modify the Java code:
package com.example.androidyoutubeplayer;

import java.io.IOException;

import android.graphics.PixelFormat;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnVideoSizeChangedListener;
import android.os.Bundle;
import android.app.Activity;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity implements SurfaceHolder.Callback{
 
 MediaPlayer myMediaPlayer;
 SurfaceView mySurfaceView;
 SurfaceHolder mySurfaceHolder;
 
 Button btnPlay, btnStop;
 TextView textVideoSize;
 String path3gp = "rtsp://v8.cache6.c.youtube.com/CjYLENy73wIaLQkP0kiLmutogRMYDSANFEIJbXYtZ29vZ2xlSARSBXdhdGNoYKjR78WV1ZH5Tgw=/0/0/0/video.3gp";
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getWindow().setFormat(PixelFormat.UNKNOWN);
        mySurfaceView = (SurfaceView)findViewById(R.id.mediasurface);        
        mySurfaceHolder = mySurfaceView.getHolder();
        mySurfaceHolder.addCallback(this);
        myMediaPlayer = new MediaPlayer();
        
        btnPlay = (Button)findViewById(R.id.play);
        btnStop = (Button)findViewById(R.id.stop);
        
        btnPlay.setOnClickListener(playOnClickListener);
        btnStop.setOnClickListener(stopOnClickListener);
        
        textVideoSize = (TextView)findViewById(R.id.size);

    }
    
    OnClickListener playOnClickListener
    = new OnClickListener(){

  @Override
  public void onClick(View v) {
   myMediaPlayer.reset();
   
   myMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
   myMediaPlayer.setDisplay(mySurfaceHolder);
   
   try {
    
    myMediaPlayer.setOnVideoSizeChangedListener(videoSizeChangedListener);
    
    myMediaPlayer.setDataSource(path3gp);
    myMediaPlayer.prepare();
    
    /*
     * if you get Video Width and Height after prepare() here, it always return 0, 0!
     */
    int videoWidth = myMediaPlayer.getVideoWidth();
    int videoHeight = myMediaPlayer.getVideoHeight();
    textVideoSize.setText(String.valueOf(videoWidth) + " x " + String.valueOf(videoHeight));
    
    myMediaPlayer.start();
   } catch (IllegalArgumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (SecurityException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IllegalStateException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   
  }};
    
    OnClickListener stopOnClickListener
    = new OnClickListener(){

  @Override
  public void onClick(View v) {
   if(myMediaPlayer.isPlaying()){
    myMediaPlayer.stop(); 
   }
   
  }};

 @Override
 public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
  // TODO Auto-generated method stub
  
 }

 @Override
 public void surfaceCreated(SurfaceHolder holder) {
  // TODO Auto-generated method stub
  
 }

 @Override
 public void surfaceDestroyed(SurfaceHolder holder) {
  // TODO Auto-generated method stub
  
 }
 
 /*
  * Get Video Width and Height in OnVideoSizeChangedListener,
  * and update SurfaceView LayoutParams accordingly.
  */
 OnVideoSizeChangedListener videoSizeChangedListener
 = new OnVideoSizeChangedListener(){

  @Override
  public void onVideoSizeChanged(MediaPlayer arg0, int arg1, int arg2) {
   int videoWidth = myMediaPlayer.getVideoWidth();
   int videoHeight = myMediaPlayer.getVideoHeight();
   
   textVideoSize.setText(String.valueOf(videoWidth) + " x " + String.valueOf(videoHeight));
   
   mySurfaceView.setLayoutParams(
     new LinearLayout.LayoutParams(videoWidth, videoHeight));
  }};
}


Modify the layout file to add TextView to show the detected size.
<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">
    
    <Button
        android:id="@+id/play"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="PLAY"/>
    <Button
        android:id="@+id/stop"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="STOP"/>
    <TextView
        android:id="@+id/size"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    <SurfaceView
        android:id="@+id/mediasurface"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

</LinearLayout>


Modify AndroidManifest.xml to add permission of "android.permission.INTERNET".

Infolinks In Text Ads