【AndroidのViewを制する】 PopupWindowの設定を変更するメソッド群2
今回、紹介するPopupWindowは画面の任意の位置に好きなViewをポップアップすることができます。
メソッドがたくさんあるため数回に分割して説明します。
今回は設定を更新できるメソッドの使い方を説明します。
PopupWindow#setWindowLayoutModeを使ってPopupWindowのWindowLayoutModeを制御する
PopupWindow#setWindowLayoutModeはPopupWindowのWindowLayoutModeを設定するメソッドです。引数には WRAP_CONTENT, MATCH_PARENT, 0を使用します。
0を引数とした場合は、PopupWindow#setWidthやPopupWindow#setHeightで指定したサイズのPopupWindowが表示されます。
デフォルトで0となっているためPopupWindow#setWidthやPopupWindow#setHeightを設定しないとPopupWindowは表示されません。 PopupWindowが表示されている状態で設定を変更するメソッドを使用してもPopupWindowには影響がありません。
表示中の場合はPopupWindowを再表示する必要があります。
final FrameLayout frameLayout = new FrameLayout(this);
setContentView(frameLayout);
// PopupWindow内に表示するFrameLayoutを作成する。
LinearLayout contentView = new LinearLayout(this);
contentView.setBackgroundColor(Color.LTGRAY);
contentView.setOrientation(LinearLayout.VERTICAL);
EditText editText = new EditText(this);
editText.setHint("入力してください");
contentView.addView(editText);
Button button = new Button(this);
button.setText("トーストを表示する");
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(PopupWindowActivity5.this, "ボタンがクリックされました", Toast.LENGTH_SHORT).show();
}
});
contentView.addView(button);
// PopupWindowのインスタンスを作成する
final PopupWindow popupWindow = new PopupWindow(contentView , 800 ,800);
// setWindowLayoutMode
Button setWindowLayoutMode = new Button(this);
setWindowLayoutMode.setText("WindowLayoutModeを変更する");
setWindowLayoutMode.setOnClickListener(new View.OnClickListener() {
private boolean isWrap = false;
@Override
public void onClick(View v) {
popupWindow.dismiss();
if (isWrap) {
popupWindow.setWindowLayoutMode(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
Toast.makeText(PopupWindowActivity6.this, "WindowLayoutModeをMATCH_PARENTに変更しました", Toast.LENGTH_SHORT).show();
} else {
popupWindow.setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
Toast.makeText(PopupWindowActivity6.this, "WindowLayoutModeをWRAP_CONTENTに変更しました", Toast.LENGTH_SHORT).show();
}
isWrap = !isWrap;
popupWindow.showAsDropDown(v);
}
});
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.addView(setWindowLayoutMode);
frameLayout.addView(linearLayout);
PopupWindow#setTouchInterceptorを使ってPopupWindow上のタッチイベントをインターセプトする
PopupWindow#setTouchInterceptorはPopupWindow上で発生したタッチイベントをインターセプトするメソッドです。
final FrameLayout frameLayout = new FrameLayout(this);
setContentView(frameLayout);
// PopupWindow内に表示するFrameLayoutを作成する。
LinearLayout contentView = new LinearLayout(this);
contentView.setBackgroundColor(Color.LTGRAY);
contentView.setOrientation(LinearLayout.VERTICAL);
EditText editText = new EditText(this);
editText.setHint("入力してください");
contentView.addView(editText);
Button button = new Button(this);
button.setText("トーストを表示する");
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(PopupWindowActivity5.this, "ボタンがクリックされました", Toast.LENGTH_SHORT).show();
}
});
contentView.addView(button);
// PopupWindowのインスタンスを作成する
final PopupWindow popupWindow = new PopupWindow(contentView , 800 ,800);
// setTouchInterceptor
Button setTouchInterceptor = new Button(this);
setTouchInterceptor.setText("TouchInterceptorを変更する");
setTouchInterceptor.setOnClickListener(new View.OnClickListener() {
private boolean isSet = false;
@Override
public void onClick(View v) {
popupWindow.dismiss();
if (isSet) {
popupWindow.setTouchInterceptor(null);
Toast.makeText(PopupWindowActivity6.this, "setTouchInterceptorを解除しました", Toast.LENGTH_SHORT).show();
} else {
popupWindow.setTouchInterceptor(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
Toast.makeText(PopupWindowActivity6.this, "setTouchInterceptorがタッチイベント(MotionEvent.ACTION_DOWN)を検出しました。", Toast.LENGTH_SHORT).show();
} else if (event.getAction() == MotionEvent.ACTION_UP) {
Toast.makeText(PopupWindowActivity6.this, "setTouchInterceptorがタッチイベント(MotionEvent.ACTION_UP)を検出しました。", Toast.LENGTH_SHORT).show();
}
return false;
}
});
Toast.makeText(PopupWindowActivity6.this, "setTouchInterceptorを設定しました", Toast.LENGTH_SHORT).show();
}
isSet = !isSet;
popupWindow.setHeight(popupWindow.getMaxAvailableHeight(v));
popupWindow.showAsDropDown(v);
}
});
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.addView(setTouchInterceptor);
frameLayout.addView(linearLayout);
PopupWindow#setOnDismissListenerを使ってPopupWindowが消えたことを検知する
PopupWindow#setOnDismissListenerはPopupWindowが消えた時にコールバックされるインターフェースを設定するメソッドです。
final FrameLayout frameLayout = new FrameLayout(this);
setContentView(frameLayout);
// PopupWindow内に表示するFrameLayoutを作成する。
LinearLayout contentView = new LinearLayout(this);
contentView.setBackgroundColor(Color.LTGRAY);
contentView.setOrientation(LinearLayout.VERTICAL);
EditText editText = new EditText(this);
editText.setHint("入力してください");
contentView.addView(editText);
Button button = new Button(this);
button.setText("トーストを表示する");
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(PopupWindowActivity5.this, "ボタンがクリックされました", Toast.LENGTH_SHORT).show();
}
});
contentView.addView(button);
// PopupWindowのインスタンスを作成する
final PopupWindow popupWindow = new PopupWindow(contentView , 800 ,800);
// setOnDismissListener
Button setOnDismissListener = new Button(this);
setOnDismissListener.setText("OnDismissListenerを変更する");
setOnDismissListener.setOnClickListener(new View.OnClickListener() {
private boolean isSet = false;
@Override
public void onClick(View v) {
popupWindow.dismiss();
if (isSet) {
popupWindow.setOnDismissListener(null);
Toast.makeText(PopupWindowActivity6.this, "setOnDismissListenerを解除しました", Toast.LENGTH_SHORT).show();
} else {
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
Toast.makeText(PopupWindowActivity6.this, "PopupWindowが消えました", Toast.LENGTH_SHORT).show();
}
});
Toast.makeText(PopupWindowActivity6.this, "setOnDismissListenerを設定しました", Toast.LENGTH_SHORT).show();
}
isSet = !isSet;
popupWindow.showAsDropDown(v);
}
});
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.addView(setOnDismissListener);
frameLayout.addView(linearLayout);
PopupWindow#setEnterTransitionを使ってPopupWindowの表示される時アニメーションを変更する
PopupWindow#setEnterTransitionはPopupWindowの表示アニメーションを設定するメソッドです。引数にはTransitionを継承したクラスを使用します。 PopupWindow#getEnterTransitionはPopupWindowの表示アニメーションを取得するメソッドです。
final FrameLayout frameLayout = new FrameLayout(this);
setContentView(frameLayout);
// PopupWindow内に表示するFrameLayoutを作成する。
LinearLayout contentView = new LinearLayout(this);
contentView.setBackgroundColor(Color.LTGRAY);
contentView.setOrientation(LinearLayout.VERTICAL);
EditText editText = new EditText(this);
editText.setHint("入力してください");
contentView.addView(editText);
Button button = new Button(this);
button.setText("トーストを表示する");
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(PopupWindowActivity5.this, "ボタンがクリックされました", Toast.LENGTH_SHORT).show();
}
});
contentView.addView(button);
// PopupWindowのインスタンスを作成する
final PopupWindow popupWindow = new PopupWindow(contentView , 800 ,800);
// setEnterTransition
Button setEnterTransition = new Button(this);
setEnterTransition.setText("EnterTransitionを変更する");
setEnterTransition.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
if (popupWindow.getEnterTransition() == null) {
popupWindow.setEnterTransition(new Slide(Gravity.LEFT));
Toast.makeText(PopupWindowActivity6.this, "EnterTransitionをSlideに変更しました", Toast.LENGTH_SHORT).show();
} else {
popupWindow.setEnterTransition(null);
Toast.makeText(PopupWindowActivity6.this, "EnterTransitionをnullに変更しました", Toast.LENGTH_SHORT).show();
}
popupWindow.showAsDropDown(v);
}
});
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.addView(setEnterTransition);
frameLayout.addView(linearLayout);
PopupWindow#setExitTransitionを使ってPopupWindowの消える時のアニメーションを変更する
PopupWindow#setExitTransitionはPopupWindowが消える時のアニメーションを設定するメソッドです。引数にはTransitionを継承したクラスを使用します。 PopupWindow#getExitTransitionはPopupWindowが消える時のアニメーションを取得するメソッドです。
final FrameLayout frameLayout = new FrameLayout(this);
setContentView(frameLayout);
// PopupWindow内に表示するFrameLayoutを作成する。
LinearLayout contentView = new LinearLayout(this);
contentView.setBackgroundColor(Color.LTGRAY);
contentView.setOrientation(LinearLayout.VERTICAL);
EditText editText = new EditText(this);
editText.setHint("入力してください");
contentView.addView(editText);
Button button = new Button(this);
button.setText("トーストを表示する");
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(PopupWindowActivity5.this, "ボタンがクリックされました", Toast.LENGTH_SHORT).show();
}
});
contentView.addView(button);
// PopupWindowのインスタンスを作成する
final PopupWindow popupWindow = new PopupWindow(contentView , 800 ,800);
// setExitTransition
Button setExitTransition = new Button(this);
setExitTransition.setText("ExitTransitionを変更する");
setExitTransition.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (popupWindow.getExitTransition() == null) {
popupWindow.setExitTransition(new Slide(Gravity.LEFT));
Toast.makeText(PopupWindowActivity6.this, "ExitTransitionをSlideに変更しました", Toast.LENGTH_SHORT).show();
} else {
popupWindow.setExitTransition(null);
Toast.makeText(PopupWindowActivity6.this, "ExitTransitionをnullに変更しました", Toast.LENGTH_SHORT).show();
}
popupWindow.dismiss();
popupWindow.showAsDropDown(v);
v.postDelayed(new Runnable() {
@Override
public void run() {
popupWindow.dismiss();
}
}, 1000);
Toast.makeText(PopupWindowActivity6.this, "MaxAvailableHeightは" + popupWindow.getMaxAvailableHeight(v) + "です。", Toast.LENGTH_SHORT).show();
}
});
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.addView(setExitTransition);
frameLayout.addView(linearLayout);
PopupWindow#getMaxAvailableHeightを使ってPopupWindowが表示できる最大の高さを取得する
PopupWindow#getMaxAvailableHeightはPopupWindowが表示できる画面上の最大の高さを取得するメソッドです。このメソッドには3つのオーバーロードが存在します。
| int getMaxAvailableHeight (View anchor) | PopupWindowをアンカービュー(View anchor)を指定して表示した場合にPopupWindowに割り当てられることができる最大の高さを取得することができます。 |
| int getMaxAvailableHeight (View anchor, int yOffset) | PopupWindowをアンカービュー(View anchor)とオフセット(int yOffset)を指定して表示した場合にPopupWindowに割り当てられることができる最大の高さを取得することができます。 |
| int getMaxAvailableHeight (View anchor, int yOffset, boolean ignoreBottomDecorations) | PopupWindowをアンカービュー(View anchor)とオフセット(int yOffset)を指定して表示した場合にPopupWindowに割り当てられることができる最大の高さを取得することができます。 この時、boolean ignoreBottomDecorationsをtrueにするとナビゲーションバーを含む高さを取得します、falseの場合はコンテンツ領域内の最大値を取得します。 |
final FrameLayout frameLayout = new FrameLayout(this);
setContentView(frameLayout);
// PopupWindow内に表示するFrameLayoutを作成する。
LinearLayout contentView = new LinearLayout(this);
contentView.setBackgroundColor(Color.LTGRAY);
contentView.setOrientation(LinearLayout.VERTICAL);
EditText editText = new EditText(this);
editText.setHint("入力してください");
contentView.addView(editText);
Button button = new Button(this);
button.setText("トーストを表示する");
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(PopupWindowActivity5.this, "ボタンがクリックされました", Toast.LENGTH_SHORT).show();
}
});
contentView.addView(button);
// PopupWindowのインスタンスを作成する
final PopupWindow popupWindow = new PopupWindow(contentView , 800 ,800);
// getMaxAvailableHeight
Button getMaxAvailableHeight = new Button(this);
getMaxAvailableHeight.setText("MaxAvailableHeightを取得する");
getMaxAvailableHeight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(PopupWindowActivity6.this, "getMaxAvailableHeight (View anchor)は" + popupWindow.getMaxAvailableHeight(v) + "です。", Toast.LENGTH_SHORT).show();
Toast.makeText(PopupWindowActivity6.this, "getMaxAvailableHeight (View anchor,int yOffset)は" + popupWindow.getMaxAvailableHeight(v, 100) + "です。", Toast.LENGTH_SHORT).show();
Toast.makeText(PopupWindowActivity6.this, "getMaxAvailableHeight (View anchor,int yOffset,boolean ignoreBottomDecorations)は" + popupWindow.getMaxAvailableHeight(v, 100, false) + "です。", Toast.LENGTH_SHORT).show();
popupWindow.dismiss();
popupWindow.setHeight(popupWindow.getMaxAvailableHeight(v));
popupWindow.showAsDropDown(v);
}
});
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.addView(getMaxAvailableHeight);
frameLayout.addView(linearLayout);