Posts Tagged ‘Android’

[Android] Simple repeat timer count

Tuesday, November 15th, 2011

I practiced to make timer base app.

When start button is pushed, it starts to count up and shows counted value in the label.
When stop button is pushed, it stops to count up.

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/currentTimeLabel"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

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

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

        <Button
            android:id="@+id/stopButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Stop" />

    </LinearLayout>

</LinearLayout>

TimerSampleActivity.java

package com.mytimer;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class TimerSampleActivity extends Activity implements View.OnClickListener{
    private final String TAG = "TimerSample";  
	
	private Button mStartButton;
	private Button mStopButton;
	public int currentTime;
	private Handler _myHandler;
	private TextView mCurrentTimeLabel;
	private Runnable _myTask;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        currentTime = 0;
        mStartButton = (Button)findViewById(R.id.startButton);
        mStopButton = (Button)findViewById(R.id.stopButton);
        mCurrentTimeLabel = (TextView)findViewById(R.id.currentTimeLabel);
        mStartButton.setOnClickListener(this);
        mStopButton.setOnClickListener(this);
    }

	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.startButton){
			Log.d(TAG, "start");
			cancelTimer();
			_myTask = new MyTimerTask();
			_myHandler = new Handler();
			_myHandler.postDelayed(_myTask, 100);
		}
		else if(v.getId() == R.id.stopButton){
			Log.d(TAG, "stop");
			mCurrentTimeLabel.setText("stop");
			cancelTimer();
		}
		
	}
	
	private void cancelTimer()
	{
		if(_myHandler != null){
			_myHandler.removeCallbacks(_myTask);
		}
	}
	
	private class MyTimerTask implements Runnable
	{
		@Override
		public void run() {
			currentTime++;
			mCurrentTimeLabel.setText(Integer.toString(currentTime));
			
			_myHandler.postDelayed(this, 100);
		}
	}
}

Below page is original source code page.
Updating the UI from a Timer

[Android] How to access media data in assets and res/raw folder

Monday, November 14th, 2011

To make custom media player, I tried to use MediaPlayer.
But when I set datasource, it could not work well.

I placed video files in assets/ or res/raw.
I didn’t know how to access.

I googled.
Then I found the solution.

If you placed in assets/video_file.mp4

_player = new MediaPlayer();
AssetFileDescriptor afd = getAssets().openFd("video_file.mp4");
_player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
    		
_player.setDisplay(_holder); //_holder is SurfaceHolder of SurfaceView
_player.prepare();

Or if you placed in res/raw/video_file.mp4

_player = new MediaPlayer();
 String fileName = "android.resource://" + getPackageName() + "/" + R.raw.video_file;
_player.setDataSource(this, Uri.parse(fileName));
    		
_player.setDisplay(_holder); //_holder is SurfaceHolder of SurfaceView
_player.prepare();

These pages have some very useful information.
Thanks.

Discovering Android – Embedding Video in an Android Application
Play audio file from the assets directory

[Android] how to make display to fullscreen

Monday, November 14th, 2011

There are several way to make display to fullscreen.

method 1)
this method’s point is placing requestWindowFeature between onCreate and setContentView.

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE); // to fullscreen
        setContentView(R.layout.main);
    }

method 2)
add theme property application or activity xml tag in AndroidManifest.xml.


or


see this page
http://developer.android.com/guide/topics/ui/themes.html#ApplyATheme

[Android] Honeycomb Android 3 system bar height is 48px

Monday, November 14th, 2011

Honeycomb Android 3 system bar height is 48px.

If your device display size is WXGA (1280 x 800), your content height is 752px.

[Android] How to make Sony Tablet S been able to develop on mac.

Monday, November 14th, 2011

I can’t connect to Sony Tablet S from my mac.
I searched, and found how to solve.

This page gives me very useful information.
[Q] Sony Tablet S ADB support

It says

you need to cd to your .android folder (on a mac, this is ~/.android) and then type

echo 0x054c > adb_usb.ini

Where 0x054c is the vendor id for Sony (I got that by looking at the attached evices in the 'About this mac' preferences)

I found the file named adb_usb.ini in below location.

/Users/myacountname/.android/adb_usb.ini

Then, I opened the file with text editor.
and added the sentence.

0x054c

and restart the adb server. In terminal app

./adb kill-server
./adb start-server

Then adb could recognize my tablet.

You can confirm usb vender id from this.
About This Mac > More Info > System Report > Hardware | USB > USB Device Tree > Vendor ID:

[Android] Failed to install app, timeout

Tuesday, October 11th, 2011

I got an error on debugging.
When I debugging, I can’t install my app.

The article on this link is the same error.
Android error: Failed to install *.apk on device *: timeout

And I could see how to fix it.

First, change the time of time out longer.

Preference > Android > DDMS
ADB connection time out (ms):
I changed default 5000 to 10000.

Second, reboot adb.

On the terminal app, I typed this code.

adb kill-server
adb start-server

I could fix it.