このエントリーをはてなブックマークに追加

2010年5月24日月曜日

Android TextView with Image



[In English]
I sometimes have the case to arrange the image next to the characters.
We can do it by putting TextView and ImageView into Layout.
But today I introduce the other way using only TextView.
The following sample code is how to show the image next to text.
(show four image(left, top, right, bottom of text))

final TextView textView = (TextView)findViewById(R.id.diet_log_label);
final Drawable iconDrawable = getResources().getDrawable(R.drawable.icon);
textView.setCompoundDrawablesWithIntrinsicBounds(iconDrawable, iconDrawable, iconDrawable, iconDrawable);
// or
textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon, R.drawable.icon, R.drawable.icon, R.drawable.icon);

To show only left image, write "setCompoundDrawablesWithIntrinsicBounds(iconDrawable, null, null, null)".



[In Japanese]
時々文字の隣に画像を配置したい場合があります。
そんな時は、TextViewとImageViewをLayout内に配置することで実現することもできますが、今日はTextViewだけを使った別の方法を紹介したいと思います。
以下は、テキストの上下左右にアイコン画像を表示させるサンプルコードになります。


final TextView textView = (TextView)findViewById(R.id.diet_log_label);
final Drawable iconDrawable = getResources().getDrawable(R.drawable.icon);
textView.setCompoundDrawablesWithIntrinsicBounds(iconDrawable, iconDrawable, iconDrawable, iconDrawable);
// or
textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon, R.drawable.icon, R.drawable.icon, R.drawable.icon);

テキストの左だけ画像を表示させる場合は、setCompoundDrawablesWithIntrinsicBounds(iconDrawable, null, null, null)とします。

2010年5月7日金曜日

How to PopupWindow

[In English]
Today, I'll show you how to use PopupWindow.
PopupWindow enables you to display view on top of the current activity. Actually Dialog class provides similar function, but those are slightly different.
The most distinctive difference is that PopupWindow has function that displays view at anywhere you want though Dialog can not.



@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.word_count_edit_text_activity);

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popupView = (View)inflater.inflate(R.layout.popupwindow_layout, null);
popupView.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));

final View view = findViewById(R.id.layout_view);
final PopupWindow popupWindow = new PopupWindow(view);

popupWindow.setContentView(popupView);
popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);

Handler mHandler = new Handler();
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
IBinder windowToken = view.getWindowToken();
if (windowToken != null && windowToken.isBinderAlive()) {
popupWindow.showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER, 10, 10);
}
}
}, 1000);

}





[In Japanese]

今回はPopupWindowの使用方法について記述します。
まずPopupWindowとは、現在表示されているActivityに対し、一番上のレイヤに表示させる事ができるWindowです。Dialogでも同じような動きをしますが、Dialogとの一番の違いは、表示する位置を指定する事ができる点です。Dialogでは基本、show()メソッドを呼び出すだけで、位置の指定まではできません。
では、PopupWindowの基本的な使い方を紹介します。



@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.word_count_edit_text_activity);

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popupView = (View)inflater.inflate(R.layout.popupwindow_layout, null);
popupView.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));

final View view = findViewById(R.id.layout_view);
final PopupWindow popupWindow = new PopupWindow(view);

popupWindow.setContentView(popupView);
popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);

Handler mHandler = new Handler();
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
IBinder windowToken = view.getWindowToken();
if (windowToken != null && windowToken.isBinderAlive()) {
popupWindow.showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER, 10, 10);
}
}
}, 1000);

}