プロトタイプ作成でよく使うのでまとめておく。
xml:@android:drawable/ic_menu_***
Javaコード:android.R.drawable.ic_menu_***
で使える41種類。
(SDK内にはこの他にもメニューアイコンリソースがあります)
アイコン画像はSDKのdrawable-hdpi配下のもの。
2.xは72x72px、4.xは48x48pxとなっている。
型番 | ペットネーム | サイズ | 解像度 | パネル |
---|---|---|---|---|
HTL21 | HTC J butterfly | 5" | FHD | super LCD 3 |
SOL21 | Xperia VL | 4.3" | HD | TFT |
SCL21 | GALAXY S III Progre | 4.8" | HD | SUPER AMOLED |
SHL21 | AQUOS PHONE SERIE | 4.7" | HD | S-CGSilicon |
FJL21 | ARROWS ef | 4.3" | HD | TFT |
LGL21 | Optimus G | 4.7" | HD | IPS |
KYL21 | DIGNO S | 4.7" | HD | TFT |
PTL21 | VEGA | 4.3" | HD | TFT |
CAL21 | G'zOne TYPE-L | 4" | WVGA | IPS |
<resources> | |
<style name="AppTheme" parent="android:Theme.Light.NoTitleBar"> | |
<item name="android:windowIsTranslucent">true</item> | |
<item name="android:windowBackground">@android:color/transparent</item> | |
</style> | |
</resources> |
<resources> | |
<style name="AppTheme" parent="android:Theme.Holo.Light.NoActionBar"> | |
<item name="android:windowIsTranslucent">true</item> | |
<item name="android:windowBackground">@android:color/transparent</item> | |
</style> | |
</resources> |
import android.util.Log; | |
public class DebugLog { | |
private static final String TAG = "MyApp"; | |
private static final int MAX_TAB_COUNT = 8; | |
private static final int TAB_SIZE = 4; | |
private static final int INDEX = 3; | |
/* | |
* DEBUG | |
*/ | |
public static void d(String message) { | |
if (BuildConfig.DEBUG) { | |
Log.d(TAG, message + getPadding(message) + getAdditionalLog()); | |
} | |
} | |
public static void d(String tag, String message) { | |
if (BuildConfig.DEBUG) { | |
Log.d(tag, message + getPadding(message) + getAdditionalLog()); | |
} | |
} | |
public static void d(String message, Throwable throwable) { | |
if (BuildConfig.DEBUG) { | |
Log.d(TAG, message + getPadding(message) + getAdditionalLog(), throwable); | |
} | |
} | |
public static void d(String tag, String message, Throwable throwable) { | |
if (BuildConfig.DEBUG) { | |
Log.d(tag, message + getPadding(message) + getAdditionalLog(), throwable); | |
} | |
} | |
private static String getPadding(String message) { | |
String padding = ""; | |
int tabCount = MAX_TAB_COUNT - (message.length() / TAB_SIZE); | |
if (tabCount <= 0) { | |
return "\t"; | |
} | |
for (int i = 0; i < tabCount; i++) { | |
padding += "\t"; | |
} | |
return padding; | |
} | |
private static String getAdditionalLog() { | |
String additionalLog = " at " + getClassName() + "#" + getMethodName() | |
+ "(" + getFileName() + ":" + getLineNumber() + ")"; | |
return additionalLog; | |
} | |
private static String getClassName() { | |
String className = new Throwable().getStackTrace()[INDEX].getClassName(); | |
className = className.substring(className.lastIndexOf(".") + 1); | |
return className; | |
} | |
private static String getMethodName() { | |
return new Throwable().getStackTrace()[INDEX].getMethodName(); | |
} | |
private static String getFileName() { | |
return new Throwable().getStackTrace()[INDEX].getFileName(); | |
} | |
private static String getLineNumber() { | |
return String.valueOf(new Throwable().getStackTrace()[INDEX].getLineNumber()); | |
} | |
} |
import android.app.Activity; | |
import android.app.KeyguardManager; | |
import android.content.BroadcastReceiver; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.IntentFilter; | |
import android.os.Bundle; | |
import android.util.Log; | |
public class MainActivity extends Activity { | |
private final String TAG = getClass().getSimpleName(); | |
private MyBroadcastReceiver mReceiver = new MyBroadcastReceiver(); | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
} | |
@Override | |
protected void onResume() { | |
Log.d(TAG, "onResume()"); | |
super.onResume(); | |
IntentFilter intentFilter = new IntentFilter(); | |
intentFilter.addAction(Intent.ACTION_USER_PRESENT); | |
registerReceiver(mReceiver, intentFilter); | |
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE); | |
Log.d(TAG, "inKeyguardRestrictedInputMode() : " + keyguardManager.inKeyguardRestrictedInputMode()); | |
if (!keyguardManager.inKeyguardRestrictedInputMode()) { | |
Log.d(TAG, "再開"); | |
/* | |
* 再開処理 | |
*/ | |
} | |
} | |
@Override | |
protected void onPause() { | |
Log.d(TAG, "onPause()"); | |
super.onPause(); | |
unregisterReceiver(mReceiver); | |
Log.d(TAG, "中断"); | |
/* | |
* 中断処理 | |
*/ | |
} | |
private class MyBroadcastReceiver extends BroadcastReceiver { | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
Log.d(TAG, "MyBroadcastReceiver#onReceive()"); | |
Log.d(TAG, "再開"); | |
/* | |
* 再開処理 | |
*/ | |
} | |
} | |
} |
2.3 から GC の方式が変わったので、SoftReference や WeakReference で Bitmap をキャッシュするのはあんまり意味ないからオススメしないそうだ
— Yuki Anzaiさん (@yanzm) 8月 14, 2012
これどこ情報なんだろう?ListViewに表示するTwitterアイコンのキャッシュにSoftReference使ってるけど、確かに2.2、2.3、4.xで動き違って困ってた。2.3と4.xはすぐキャッシュが解放されてて再取得が走る... > RT
— koji okabeさん (@kojiokb) 8月 14, 2012
@yanzm 知りませんでした(・o・) そのうち自作アプリもLruCache使うようにしてみます。developer.android.com/training/displ…
— HaRuさん (@h6a_h4i) 8月 14, 2012
これか。明日じっくり読んでみる。 Android Training - Advanced Training - Caching Bitmaps developer.android.com/training/displ…
— koji okabeさん (@kojiokb) 8月 14, 2012
import android.graphics.Bitmap; | |
import android.support.v4.util.LruCache; | |
public final class ImageCache { | |
private static final int MEM_CACHE_SIZE = 1 * 1024 * 1024; // 1MB | |
private static LruCache<String, Bitmap> sLruCache; | |
static { | |
sLruCache = new LruCache<String, Bitmap>(MEM_CACHE_SIZE) { | |
@Override | |
protected int sizeOf(String key, Bitmap bitmap) { | |
return bitmap.getRowBytes() * bitmap.getHeight(); | |
} | |
}; | |
} | |
private ImageCache() { | |
} | |
public static void setImage(String key, Bitmap bitmap) { | |
if (getImage(key) == null) { | |
sLruCache.put(key, bitmap); | |
} | |
} | |
public static Bitmap getImage(String key) { | |
return sLruCache.get(key); | |
} | |
} |
package com.example.alertdialogfragment; import android.content.DialogInterface; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Toast; public class MainActivity extends FragmentActivity implements OnClickListener { private AlertDialogFragment mDialogFragment1; private AlertDialogFragment mDialogFragment2; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.button1).setOnClickListener(this); findViewById(R.id.button2).setOnClickListener(this); } public void onClick(View view) { DialogButtonClickListener listener = new DialogButtonClickListener(); AlertDialogFragment.Builder builder = new AlertDialogFragment.Builder(this); Bundle params = new Bundle(); switch (view.getId()) { case R.id.button1: builder.setTitle("Title1"); builder.setMessage("Message1"); builder.setPositiveButton("POSITIVE", listener); builder.setNeutralButton("NEUTRAL", listener); builder.setNegativeButton("NEGATIVE", listener); params.putSerializable("builder", builder); mDialogFragment1 = new AlertDialogFragment(); mDialogFragment1.setArguments(params); mDialogFragment1.setCancelable(false); mDialogFragment1.show(getSupportFragmentManager(), "dialog1"); break; case R.id.button2: builder.setTitle("Title2"); builder.setMessage("Message2"); builder.setPositiveButton("POSITIVE", listener); builder.setNeutralButton("NEUTRAL", listener); builder.setNegativeButton("NEGATIVE", listener); params.putSerializable("builder", builder); mDialogFragment2 = new AlertDialogFragment(); mDialogFragment2.setArguments(params); mDialogFragment2.setCancelable(false); mDialogFragment2.show(getSupportFragmentManager(), "dialog2"); break; } } private class DialogButtonClickListener implements DialogInterface.OnClickListener { public void onClick(DialogInterface dialog, int which) { String dialogName = ""; if (dialog.equals(mDialogFragment1.getDialog())) { dialogName = "ダイアログ1"; } else if (dialog.equals(mDialogFragment2.getDialog())) { dialogName = "ダイアログ2"; } switch (which) { case DialogInterface.BUTTON_POSITIVE: Toast.makeText(getApplicationContext(), dialogName + ":POSITIVE", Toast.LENGTH_SHORT).show(); break; case DialogInterface.BUTTON_NEUTRAL: Toast.makeText(getApplicationContext(), dialogName + ":NEUTRAL", Toast.LENGTH_SHORT).show(); break; case DialogInterface.BUTTON_NEGATIVE: Toast.makeText(getApplicationContext(), dialogName + ":NEGATIVE", Toast.LENGTH_SHORT).show(); break; } } } }AlertDialogFragment.java
package com.example.alertdialogfragment; import java.io.Serializable; import android.app.AlertDialog; import android.app.Dialog; import android.content.Context; import android.os.Bundle; import android.support.v4.app.DialogFragment; public class AlertDialogFragment extends DialogFragment { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { Bundle params = getArguments(); AlertDialog.Builder builder = (AlertDialog.Builder) params.getSerializable("builder"); return builder.create(); } public static class Builder extends AlertDialog.Builder implements Serializable { private static final long serialVersionUID = 1L; public Builder(Context context) { super(context); } public Builder(Context context, int theme) { super(context, theme); } } }
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <android.support.v4.view.PagerTabStrip android:id="@+id/pager_tab_strip" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="top" android:background="#33B5E5" android:textColor="#FFFFFF" android:paddingTop="10dp" android:paddingBottom="10dp" /> </android.support.v4.view.ViewPager>MainActivity.java
package com.example.pagertabstripsample; import android.graphics.Color; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.PagerTabStrip; import android.support.v4.view.ViewPager; public class MainActivity extends FragmentActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ViewPager viewPager = (ViewPager) findViewById(R.id.pager); PagerTabStrip pagerTabStrip = (PagerTabStrip) findViewById(R.id.pager_tab_strip); FragmentPagerAdapter fragmentPagerAdapter = new MyFragmentPagerAdapter( getSupportFragmentManager()); viewPager.setAdapter(fragmentPagerAdapter); pagerTabStrip.setDrawFullUnderline(true); pagerTabStrip.setTabIndicatorColor(Color.DKGRAY); } }MyFragmentPagerAdapter.java
package com.example.pagertabstripsample; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; public class MyFragmentPagerAdapter extends FragmentPagerAdapter { private String[] pageTitle = { "Page1", "Page2", "Page3" }; public MyFragmentPagerAdapter(FragmentManager fragmentManager) { super(fragmentManager); } @Override public Fragment getItem(int position) { Fragment fragment = new PageFragment(); Bundle arguments = new Bundle(); arguments.putString("pageIndex", Integer.toString(position + 1)); fragment.setArguments(arguments); return fragment; } @Override public int getCount() { return pageTitle.length; } @Override public CharSequence getPageTitle(int position) { return pageTitle[position]; } }PagerFragment.java
package com.example.pagertabstripsample; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class PageFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_page, null); TextView textView = (TextView) view.findViewById(R.id.textview); textView.setText(getArguments().getString("pageIndex")); return view; } }
public class SplashActivity extends Activity { | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.splash); | |
Handler handler = new Handler() { | |
@Override | |
public void handleMessage(Message msg) { | |
Intent intent = new Intent(getApplicationContext(), MainActivity.class); | |
startActivity(intent); | |
finish(); | |
} | |
}; | |
handler.sendEmptyMessageDelayed(0, 3000); | |
} | |
} |
/** Automatically generated file. DO NOT MODIFY */ package com.example.builconfigtest; public final class BuildConfig { public final static boolean DEBUG = true; }
public class BuildConfigTestActivity extends Activity { private final String TAG = getClass().getSimpleName(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); if (BuildConfig.DEBUG) { Log.e(TAG, "BuildConfig.DEBUG = true"); } else { Log.e(TAG, "BuildConfig.DEBUG = false"); } } }
<?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>コードから指定する方法
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);
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; } }
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);
@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を*****...に置き換える これにより、開発者向けテスト用のバナーが表示されるようになる