2012年4月21日土曜日

ボタンにアイコンを表示する

レイアウトファイルで指定する方法
drawableLeft
drawableRight
drawableTop
drawableBottom
でアイコンの位置と画像を指定。

drawablePadding
でアイコンと文字の間隔を指定。
<?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:gravity="center"
    android:orientation="vertical" >
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableLeft="@drawable/ic_launcher"
        android:drawablePadding="10dp"
        android:text="drawableLeft" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableRight="@drawable/ic_launcher"
        android:drawablePadding="10dp"
        android:text="drawableRight" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableTop="@drawable/ic_launcher"
        android:drawablePadding="10dp"
        android:text="drawableTop" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableBottom="@drawable/ic_launcher"
        android:drawablePadding="10dp"
        android:text="drawableBottom" />
</LinearLayout>
コードから指定する方法
setCompoundDrawablesWithIntrinsicBounds()
でアイコンの位置と画像を指定。
引数はリソースID または Drawableで指定。

setCompoundDrawablePadding()
でアイコンと文字の間隔を指定。
単位はpx。たぶん。。
        Button button = (Button) findViewById(R.id.Button);

        int left = R.drawable.ic_launcher;
        int top = 0;
        int right = 0;
        int bottom = 0;

        // Drawable left = getResources().getDrawable(R.drawable.ic_launcher);
        // Drawable top = null;
        // Drawable right = null;
        // Drawable bottom = null;

        button.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);
        button.setCompoundDrawablePadding(10);

2012年3月24日土曜日

Android開発環境(再)構築メモ for Windows 7(64bit)

Android SDK r17とADT 17が公開されたので導入したら
下記のExceptionが出るようになる
「Exception while initializing java tooling」
↓
Eclipse再インストール ※ついでなので3.6(32bit)->3.7(64bit)にバージョンアップ
↓
3.7(64bit)起動せず ※Eclipse 64bit動かすにはJDK 64bitが必要?
「Failed to load the JNI shared library "C:\Program Files(x86)\Java\jdk1.6.0_24\jre\bin\client\jvm.dll"」
↓
諦めて3.7(32bit)に
↓
起動するも症状変わらず...
「Exception while initializing java tooling」
↓
調べてみたところworkspaceが壊れてる?模様
http://www.myeclipseide.com/PNphpBB2-viewtopic-t-21638.html
既存のworkspaceをリネームしてバックアップした後
新しくworkspaceを作り直したところ復旧
↓
バックアップしたworkspaceからプロジェクトをインポート
↓
@Overrideアノテーションでエラー出る
↓
調べる
↓
解決
Window->Preferences->Java->Compiler->JDK Compliance->Compiler compliance level
1.5->1.6に変更
※参考にしたページ

Java関連の日記 - Firespeed
「@Overrideのエラーを解決してみた」


次回の再構築時にもハマりそうなので、忘れないようにメモる。


因みにEclipseの設定は下記の通り。
・Android開発向けEclipse設定メモ
http://neta-abc.blogspot.jp/2011/09/eclipse.html
・Eclipse Java ; { の入力
http://neta-abc.blogspot.jp/2011/10/eclipse-java.html

2012年2月25日土曜日

ADT 17 Preview メモ

ADT 17 Previewをインストールしてみた。
ダウンロードと変更点の詳細は↓から。

以下、気になった変更点。
Network Statistics

使い方
DDMSパースペクティブを開く
[Window]-[Show View]-[Other]を開き[Android]-[Network Statistics]を選択
Devicesから測定したいパッケージ名を選択してNetwork Statisticsの[Start]ボタンを押下

Totalは灰色、タグを設定した処理の通信量は色分けして表示される。
タグの設定方法は下記。
TrafficStats.setThreadStatsTag(0xF00D);
try {
// make network request using HttpClient.execute()
} finally {
TrafficStats.clearThreadStatsTag();
}

Proguard
設定ファイル名が変わった?
proguard.cfg → proguard-project.txt
設定ファイルが分割された?
<android-sdk-path>\tools\proguard\proguard-android.txt
Proguardの設定ファイルを読みこなせないので詳しいことはわかりません...

Lint
いろいろ変わったらしい...

Export Screenshot from the Layout Editor
Layout Editorから右クリック-[Export Screenshot...]でLayout Editor上画面レイアウトのスクリーンショットが保存できる。

Androidアプリのデフォルトアイコン
ic_launcher.pngが↓に変わった。

/res/drawable-xhdpi
新規Androidプロジェクトの作成時に生成されるようになった。

以上。

2012年2月8日水曜日

デバッグビルドとリリースビルドで処理を変える方法

2012/05/15追記
↓その2書きました。こちらの方がシンプルです。
デバッグビルドとリリースビルドで処理を変える方法(2)
http://neta-abc.blogspot.jp/2012/05/2.html

コード
MainActivity.java
public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        if (DebugUtil.isDebuggable(getApplicationContext())) {
            Log.e(getClass().getSimpleName(), "Debug Build");
        } else {
            Log.e(getClass().getSimpleName(), "Release Build");
        }
    }
}
DebugUtil.java
public class DebugUtil {
    public static boolean isDebuggable(Context context) {
        PackageManager manager = context.getPackageManager();
        ApplicationInfo info = null;
        try {
            info = manager.getApplicationInfo(context.getPackageName(), 0);
        } catch (NameNotFoundException e) {
            return false;
        }
        if ((info.flags & ApplicationInfo.FLAG_DEBUGGABLE) == ApplicationInfo.FLAG_DEBUGGABLE) {
            return true;
        }
        return false;
    }
}

AndroidManifest.xmlのapplicationタグ内でandroid:debuggableをtrue/falseしてデバッグビルドとリリースビルドを切り替えると思っていましたが、Eclipseから実行した場合は自動でandroid:debuggable="true"となるようです。

上記コードで以下を確認しました。
・Eclipseから実行した場合
  Debug Buildのログが表示される。
・Export Signed Application Packageで作成したAPKファイルをインストールして実行した場合
  Release Buildのログが表示される。

参考にしたサイト
Tech Racho
「Androidでリリース時にデバッグログをオフにする方法」
http://techracho.bpsinc.jp/bps/2010_06_28/1946
Y.A.Mの雑記帳
「Android SDK Tools, Reviison 8」
General notes: "true debug build をサポート。..."の部分
http://y-anz-m.blogspot.com/2010/12/androidsdk-tools-reviison-8.html

2012年1月22日日曜日

Intent経由でGoogleカレンダーに予定を追加する方法

コード
Intent intent = new Intent(Intent.ACTION_EDIT);  
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("title", "予定のタイトル");
intent.putExtra("description", "予定の内容");
intent.putExtra("beginTime", eventStartInMillis); //開始日時
intent.putExtra("endTime", eventEndInMillis); //終了日時
startActivity(intent);

メモ
今回の方法では
1. IntentでGoogleカレンダーの新規予定作成画面を起動
2. Intentに設定したタイトル等の情報が新規作成画面に反映される
3. ユーザー操作で登録先カレンダー変更・通知設定等を行い、予定の追加完了
という流れになります。

Intentを使わない方法と比較すると若干ユーザー操作が増えるというデメリットがありますが、下記のメリットもあるかと思います。
・予定追加の際、更にユーザーが内容を追記できる
・自分のアプリにカレンダー読み書きのパーミッションを付加しなくてもよい

開始日時/終了日時はエポックタイムで指定します。
私はCalendarクラスのgetTimeInMillis()メソッドを使用して指定しました。

因みに、「ジョルテ」がインストールされていると、Intentに「ジョルテ」も反応しますが設定した予定の情報を渡すことは出来ませんでした。
データの渡し方が分かれば「ジョルテ」にも対応できるんじゃないかと思います。

上記コードは2.2(Froyo)、4.0(ICS)で動作確認を行っています。

参考にしたサイト
自転車で通勤しましょ♪ブログ
「ANDROID:カレンダーにINTENTを発行する」
http://319ring.net/blog/archives/1596
Stack Overflow
「How to launch Android Calendar application using Intent(Froyo)」
http://stackoverflow.com/questions/4373074/how-to-launch-android-calendar-application-using-intent-froyo

2012年1月15日日曜日

AdMob 4.3.1 メモ

ライブラリ追加
プロジェクトフォルダ直下に/libsディレクトリを作成
GoogleAdMobAdsSdk-4.3.1.jarを/libsディレクトリへコピー
Java Build PathのAdd JARs...から上記ライブラリを追加

AndroidManifest.xml
パーミッションの追加
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Activityの追加
<activity
android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
↑Project Build Targetを3.x以上?にしないと使えないパラメータが入っているので必要に応じてTarget SDK Versionを上げる

layoutファイル
    <com.google.ads.AdView
        android:id="@+id/adview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        ads:adUnitId="パブリッシャーID" />

コード
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mAdView = (AdView) findViewById(R.id.adview);
        AdRequest adRequest = new AdRequest();
        adRequest.addTestDevice(AdRequest.TEST_EMULATOR); //テスト用バナーを表示する
        mAdView.loadAd(adRequest);
    }

    @Override
    protected void onDestroy() {
        mAdView.destroy();
        super.onDestroy();
    }
上記コードを実行するとLogCatに I/Ads(32576): To get test ads on this device, call adRequest.addTestDevice("*****..."); なログが出るので#addTestDeviceのAdRequest.TEST_EMULATORを*****...に置き換える これにより、開発者向けテスト用のバナーが表示されるようになる