PowenKo, Android Tutorial 019-17, Dialog, PopupWindow resize



package com.powenko.Tutorial_Dialog_PopupWindow_resizeable;


import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout.LayoutParams;
import android.widget.PopupWindow;

public class Tutorial_Dialog_PopupWindow_resizeableActivity extends Activity implements OnClickListener{
	/** Called when the activity is first created. */

	private Button btn = null;
	private Button btn2 = null;
	private boolean click = false;
	PopupWindow mPopupWindow = null;	
	View vPopunwind = null;
	Drawable mDrawable1 = null;
	Drawable mDrawable2 = null;
	Resources r = null;
	LayoutInflater mLayoutInflater = null;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		btn = (Button)findViewById(R.id.button1);   
		btn.setOnClickListener(this);  
		r = this.getResources();
		
		mDrawable1 = r.getDrawable(R.drawable.icon);
		mDrawable2 = r.getDrawable(R.drawable.icon);
		
	}
	@Override
	public void onClick(View v) {
		Context mContext = Tutorial_Dialog_PopupWindow_resizeableActivity.this;   
		if (v.getId() == R.id.button1) {
			mLayoutInflater = (LayoutInflater) mContext   
			.getSystemService(LAYOUT_INFLATER_SERVICE);   
			vPopunwind = mLayoutInflater.inflate(   
					R.layout.popuplayout, null,false);   
			
//			mPopupWindow = new PopupWindow(vPopunwind,LayoutParams.WRAP_CONTENT,   
//					LayoutParams.WRAP_CONTENT);   
			mPopupWindow = new PopupWindow(mContext);
			mPopupWindow.setContentView(vPopunwind);
			mPopupWindow.setHeight(LayoutParams.WRAP_CONTENT);
			mPopupWindow.setWidth(LayoutParams.WRAP_CONTENT);
			mPopupWindow.setFocusable(true);
			mPopupWindow.setOutsideTouchable(true);
			mPopupWindow.setBackgroundDrawable(new ColorDrawable(2));
					
			mPopupWindow.showAtLocation(mLayoutInflater.inflate(   
					R.layout.main,null,false), Gravity.LEFT|Gravity.TOP, 0, 200);   
		
			btn2 = (Button) vPopunwind.findViewById(R.id.btn2);
			btn2.setOnClickListener(this);
		}
		
		if(v.getId() == R.id.btn2) {
			if(click){
				LayoutParams lp = (LayoutParams) vPopunwind.getLayoutParams();
				lp.height=180;
				lp.width=180;
				vPopunwind.setLayoutParams(lp);
				vPopunwind.setBackgroundDrawable(mDrawable1);
				mPopupWindow.setContentView(vPopunwind);
				mPopupWindow.setHeight(LayoutParams.WRAP_CONTENT);
				mPopupWindow.setWidth(LayoutParams.WRAP_CONTENT);
				mPopupWindow.setFocusable(true);
				mPopupWindow.setOutsideTouchable(true);
						
				mPopupWindow.showAtLocation(mLayoutInflater.inflate(   
						R.layout.main,null,false), Gravity.LEFT|Gravity.TOP, 0, 200);   
			}else{
				LayoutParams lp = (LayoutParams) vPopunwind.getLayoutParams();
				lp.height=180;
				lp.width=200;
				vPopunwind.setLayoutParams(lp);
				vPopunwind.setBackgroundDrawable(mDrawable2);
				mPopupWindow.setContentView(vPopunwind);
				mPopupWindow.setHeight(LayoutParams.WRAP_CONTENT);
				mPopupWindow.setWidth(LayoutParams.WRAP_CONTENT);
				mPopupWindow.setFocusable(true);
				mPopupWindow.setOutsideTouchable(true);
		
						
				mPopupWindow.showAtLocation(mLayoutInflater.inflate(   
						R.layout.main,null,false), Gravity.LEFT|Gravity.TOP, 20, 200);
			}
			
			click=!click;
		}

	}
}

res\layout\popuplayout.xml

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="@+id/btn2"
android:layout_width="234px"
android:layout_height="wrap_content"
android:text="OK"
android:textSize="26sp"
android:layout_x="49px"
android:layout_y="0px"
>
</Button>

<TextView
android:id="@+id/poptext"
android:layout_width="225px"
android:layout_height="81px"
android:background="#123456"
android:text="This is my new PopUp Window,\n www.powenko.com"
android:textSize="18sp"
android:textColor="#ff000000"
android:layout_x="53px"
android:layout_y="44px">
</TextView>


</AbsoluteLayout>

res\layout\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"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    
</LinearLayout>


Leave a Reply