I will show sample code to create a popup window in Android programmatically. A popup window can be used to display an arbitrary view
Please try it your self.
package com.j4android;
import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
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.PopupWindow;
public class PopupWindowActivity extends Activity {
PopupWindow popupWindow;
@Override
public void onCreate(Bundle bundle){
super.onCreate(bundle);
setContentView(R.layout.main);
Button popupBttn = (Button)findViewById(R.id.button1);
popupBttn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
popupSettings();
}
});
}
private void popupSettings() {
LayoutInflater inflater = (LayoutInflater) getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popup = inflater.inflate(R.layout.popup_xml, null);
popupWindow = new PopupWindow(popup,android.view.ViewGroup.LayoutParams.MATCH_PARENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
popupWindow.showAtLocation(popup, Gravity.BOTTOM, 0, 0);// Gravity.TOP etc.,
}
}
main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click to show PopupWindow" />
</LinearLayout>
note:
don't forget to create desire layout in popup.xml.
Please try it your self.
package com.j4android;
import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
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.PopupWindow;
public class PopupWindowActivity extends Activity {
PopupWindow popupWindow;
@Override
public void onCreate(Bundle bundle){
super.onCreate(bundle);
setContentView(R.layout.main);
Button popupBttn = (Button)findViewById(R.id.button1);
popupBttn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
popupSettings();
}
});
}
private void popupSettings() {
LayoutInflater inflater = (LayoutInflater) getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popup = inflater.inflate(R.layout.popup_xml, null);
popupWindow = new PopupWindow(popup,android.view.ViewGroup.LayoutParams.MATCH_PARENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
popupWindow.showAtLocation(popup, Gravity.BOTTOM, 0, 0);// Gravity.TOP etc.,
}
}
main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click to show PopupWindow" />
</LinearLayout>
note:
don't forget to create desire layout in popup.xml.
No comments:
Post a Comment