Android Dynamic Popup View example
Android 에서 동적으로 생성한 뷰를 출력하는 예
1초이상 스크린을 누를 때 코드를 이용하여 동적으로 뷰를 생성하여 해당위치에 팝업뷰를 보여주는 예
참고: AbsoluteLayout은 현재 deprecated 상태이며 사용하지 않기를 권하고 있으나 앞으로도 API에서 삭제되지는 않을 것 같다.
Dianne Hackborn of Android
http://groups.google.com/group/android-developers/msg/470467ddf2b59d2a
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<AbsoluteLayout
android:id="@+id/absLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_x="0px"
android:layout_y="0px"
android:text="1초이상 스크린을 터치하면 그 위치에 팝업뷰가 출력되는 예제, 첫번째 메뉴항목을 누르면 팝업뷰가 사라진다"/>
</AbsoluteLayout>
</LinearLayout>
PopupTestActivity.java
package com.dearpeople.android.test.longtab;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.RelativeLayout.LayoutParams;
public class PopupTestActivity extends Activity {
boolean longtab;
TextView textView;
float x,y;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AbsoluteLayout absLayout = (AbsoluteLayout)this.findViewById(R.id.absLayout);
textView = (TextView)this.findViewById(R.id.textView);
}
private void showPopView(){
// 지정한 시간동안 계속해서 눌리지 않았다면 롱탭으로 간주하지 않는다
if(!longtab) return;
AbsoluteLayout absLayout = (AbsoluteLayout)this.findViewById(R.id.absLayout);
final LinearLayout linearLayout = new LinearLayout(this);
AbsoluteLayout.LayoutParams params = new AbsoluteLayout.LayoutParams(150, 80, (int)x, (int)y-105);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setGravity(Gravity.BOTTOM);
//linearLayout.setBackgroundColor(Color.LTGRAY);
linearLayout.setBackgroundResource(R.drawable.longtap_bg);
linearLayout.setLayoutParams(params);
LinearLayout.LayoutParams llayout_params = new LinearLayout.LayoutParams(150,30);
//만약 동적으로 ImageView를 생성하려면 다음과 같이 하면 된다.
/*
ImageView imgView = new ImageView(this);
imgView.setImageResource(R.drawable.longtap_bg);
imgView.setLayoutParams(llayout_params);
linearLayout.addView(imgView);
*/
TextView tv01 = new TextView(this);
tv01.setLayoutParams(llayout_params);
tv01.setText("첫번째 아이템");
tv01.setTextColor(Color.BLACK);
tv01.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
linearLayout.setVisibility(View.GONE);
}
});
linearLayout.addView(tv01);
TextView tv02 = new TextView(this);
tv02.setLayoutParams(llayout_params);
tv02.setText("두번째 아이템");
tv02.setTextColor(Color.BLACK);
tv02.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
linearLayout.setVisibility(View.GONE);
}
});
linearLayout.addView(tv02);
absLayout.addView(linearLayout);
linearLayout.setVisibility(View.VISIBLE);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
TabCount timer = new TabCount(1000, 1000);
if(action==MotionEvent.ACTION_MOVE){
}else if(action==MotionEvent.ACTION_DOWN){
x = event.getX();
y = event.getY();
timer.start();
longtab = true;
}else if(action==MotionEvent.ACTION_UP){
longtab = false;
}
return super.onTouchEvent(event);
}
/*내부클래스로 선언한 타이머 */
class TabCount extends CountDownTimer {
public TabCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
showPopView();
}
@Override
public void onTick(long arg0) {
}
}// 내부클래스 끝
}