Button into PopupView. - android

I have a LinearLayout into my PopupView, and a button into this LinearLayout. Event listener of this button works correctly, but animation of pressing (highlighting) is not starting. What I did wrong? Please, gelp!
public class PopupView extends View
{
private PopupWindow popUp;
private Button buttonOk;
private OnClickListener onClick;
private LayoutParams params;
private int popUpWidth = 0;
private int popUpHeight = 0;
private int popUpX, popUpY;
public PopupView(Context context, View parent)
{
super(context);
popUp = new PopupWindow(context);
popUpLayout = new LinearLayout(context);
buttonOk = new Button(context);
buttonOk.setText("OK");
buttonOk.setId(1);
params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
popUpLayout.setOrientation(LinearLayout.VERTICAL);
popUp.setContentView(popUpLayout);
onClick = new OnClickListener()
{
public void onClick (View v)
{
switch (v.getId())
{
case 1:
System.out.println("OK pressed!");
break;
case 2:
break;
}
}
};
buttonOk.setOnClickListener(onClick);
popUpLayout.removeAllViews();
popUpLayout.addView(buttonOk, params);
popUpWidth = 200;
popUpHeight = 400;
popUpX = 300;
popUpY = 300;
popUp.showAtLocation(parent, Gravity.NO_GRAVITY, popUpX, popUpY);
popUp.update(popUpX, popUpY, popUpWidth, popUpHeight);
}
}

In default PopupView is not focusble. Need to call method popup.setFocusable(true). Thanks for all!

Related

Why this button setText cannot show text in my custom dialog?

My code:
public class MyBottomDialog extends Dialog {
LinearLayout container;
public MyBottomDialog(Context context) {
super(context);
container = new LinearLayout(context);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
container.setLayoutParams(params);
container.setOrientation(LinearLayout.VERTICAL);
// container.setBackgroundColor(Color.TRANSPARENT);
int padding = context.getResources().getDimensionPixelSize(R.dimen.navigation_padding_bottom);
container.setPadding(padding, padding, padding, padding);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(container);
// setContentView(button);
setCancelable(true);
setCanceledOnTouchOutside(true);
getWindow().setGravity(Gravity.BOTTOM);
// getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
getWindow().setWindowAnimations(R.style.BottomDialogAnimation);
}
public void addMenuItems(String[] menuItemTitles, int[] menuItemTextColors) {
for (int i = 0; i < menuItemTitles.length; i++) {
// Button button = (Button) LayoutInflater.from(getContext()).inflate(R.layout.bottom_menu_item, container, false);
// button.setText(menuItemTitles[i]);
// button.setTextColor(menuItemTextColors[i]);
// container.addView(button);
addMenuItem(menuItemTitles[i], menuItemTextColors[i]);
}
}
public void addMenuItem(String menuItemTitle, int menuItemTextColor) {
// Button button = (Button) LayoutInflater.from(getContext()).inflate(R.layout.bottom_menu_item, container, false);
Button button = produceButton();
button.setText(menuItemTitle);
button.setTextColor(menuItemTextColor);
container.addView(button);
}
private Button produceButton() {
Button button = new Button(getContext());
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
button.setGravity(Gravity.CENTER);
// button.setBackground(getContext().getResources().getDrawable(R.drawable.bottom_menu_button2));
float[] outerRadii = new float[]{24, 24, 24, 24, 24, 24, 24, 24};
RoundRectShape rect = new RoundRectShape(outerRadii, null, null);
ShapeDrawable drawable = new ShapeDrawable(rect);
drawable.getPaint().setColor(Color.WHITE);
drawable.getPaint().setStyle(Paint.Style.FILL);
button.setBackground(drawable);
return button;
}
}
then I use it:
MyBottomDialog dlg = new MyBottomDialog(this);
dlg.addMenuItems(new String[]{"delete", "cancel"}, new int[]{android.R.color.holo_red_light, android.R.color.holo_blue_light});
dialog.show();
but there is no any text in these two button in dialog, why?

How to center PopupWindow?

I created an Activity in which i add a button that throws a popup when is clicked. Here is the code of showPopup() method:
private void showPopup() {
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popup_layout, (ViewGroup) findViewById(R.id.popup_element), false);
final PopupWindow pwindo = new PopupWindow(layout, LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT, true);
Button btnAgree = (Button) layout.findViewById(R.id.ok);
btnAgree.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
pwindo.dismiss();
}
});
pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
}
}
I would center it both vertically and orizzontally. I tried several ways that i see here on SO but none worked. Why i always get the popup window at the top of the screen?
you can use setHorizontalOffset:
ListPopupWindow popupWindow = new ListPopupWindow(this);
// Position
popupWindow.setAnchorView(btn);
popupWindow.setPromptPosition (ListPopupWindow.POSITION_PROMPT_ABOVE);
popupWindow.setHorizontalOffset((btn.layoutParams.width - popupWindow.getWidth())/2);
this code will show perfect popup in center of you specific view
// click event of specific view
#Override
public void onClick(View v) {
super.onClick(v);
if (v == lin_filterActionbar) {
PopupWindow popupwindow_obj; // create object
popupwindow_obj = dialog_AllergiesSelect(RR_HomeActivity.this,lin_filterActionbar);
popupwindow_obj.showAsDropDown(lin_filterActionbar);
}
}
private PopupWindow dialog_AllergiesSelect(Context context,LinearLayout lin) {
final PopupWindow dialog_AllergiesSelect = new PopupWindow(this);
View v = View.inflate(context, R.layout.dialog_filter_actionbar, null);
dialog_AllergiesSelect.setContentView(v);
dialog_AllergiesSelect.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
dialog_AllergiesSelect.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
dialog_AllergiesSelect.setFocusable(true);
// TextView lis_testSelection = (TextView) v.findViewById(R.id.dialogfilter_txt_filter);
// LinearLayout.LayoutParams lin_laLayoutParams = new LinearLayout.LayoutParams((int) (sunApplication.getDeviceWidth() - (sunApplication.getDeviceScale() * dialog_width_dvide_allerges_spinner_width)), (int) (sunApplication.getDeviceHeight() - (sunApplication.getDeviceScale() * dialog_width_dvide_allerges_height)));
// lis_testSelection.setLayoutParams(lin_laLayoutParams);
// Some offset to align the popup a bit to the right, and a bit down, relative to button's position.
int OFFSET_X = 0;
int OFFSET_Y = 0;
// Clear the default translucent background
dialog_AllergiesSelect.setBackgroundDrawable(new BitmapDrawable());
// Displaying the popup at the specified location, + offsets.
//dialog_AllergiesSelect.showAtLocation(v, Gravity.C, p.x + OFFSET_X, p.y + OFFSET_Y);
Rect location = locateView(lin);
dialog_AllergiesSelect.showAtLocation(v, Gravity.TOP|Gravity.CENTER, 0, location.bottom);
// Getting a reference to Close button, and close the popup when clicked.
return dialog_AllergiesSelect;
}
public static Rect locateView(View v)
{
int[] loc_int = new int[2];
if (v == null) return null;
try
{
v.getLocationOnScreen(loc_int);
} catch (NullPointerException npe)
{
//Happens when the view doesn't exist on screen anymore.
return null;
}
Rect location = new Rect();
location.left = loc_int[0];
location.top = loc_int[1];
location.right = location.left + v.getWidth();
location.bottom = location.top + v.getHeight();
return location;
}

Add Views programmatically to LinearLayout after it got inflated

I want to add some LinearLayouts to an existing Linearlayout.
The xml of the Activity looks as followed:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popupLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPopUp"
android:gravity="center"
android:orientation="vertical" >
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none" >
<LinearLayout
android:id="#+id/ll_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
The code of the Activity:
public class MainActivity extends Activity {
private Point p;
private PopupWindow popup;
private LinearLayout myLInearLayout;
private TextView valueTV;
private Button valueB;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button popUpButton = (Button) findViewById(R.id.open);
popUpButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (p != null)
showPopup(MainActivity.this, p);
}
});
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
int[] location = new int[2];
Button button = (Button) findViewById(R.id.open);
button.getLocationOnScreen(location);
// Initialize the Point with x, and y positions
p = new Point();
p.x = location[0];
p.y = location[1];
}
// The method that displays the popup.
private void showPopup(final Activity context, Point p) {
Rect rectgle = new Rect();
Window window = getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
int popupWidth = this.getResources().getDisplayMetrics().widthPixels;
int popupHeight = this.getResources().getDisplayMetrics().heightPixels / 4;
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) context
.findViewById(R.id.popupLinearLayout);
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup);
// Creating the PopupWindow
popup = new PopupWindow(context);
popup.setContentView(layout);
popup.setWidth(popupWidth);
popup.setHeight(popupHeight);
popup.setOutsideTouchable(true);
popup.setFocusable(true);
popup.setAnimationStyle(R.style.PopupWindowAnimation);
// Some offset to align the popup a bit to the right, and a bit down,
// relative to button's position.
int OFFSET_X = 0;
int OFFSET_Y = 0;
// Clear the default translucent background
popup.setBackgroundDrawable(new BitmapDrawable());
// Displaying the popup at the specified location, + offsets.
popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y
+ OFFSET_Y);
// add LInearLayout
myLInearLayout = (LinearLayout) findViewById(R.id.ll_horizontal);
// add LayoutParams
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.MATCH_PARENT);
myLInearLayout.setOrientation(LinearLayout.HORIZONTAL);
// add textView
valueTV = new TextView(this);
valueTV.setText("The developer world is yours");
valueTV.setId(5);
valueTV.setLayoutParams(params);
// add Button
valueB = new Button(this);
valueB.setText("thedeveloperworldisyours");
valueB.setId(5);
// add the textView and the Button to LinearLayout
myLInearLayout.addView(valueTV);
myLInearLayout.addView(valueB);
}
#Override
public void onBackPressed() {
if (popup != null && popup.isShowing()) {
popup.dismiss();
popup = null;
} else {
super.onBackPressed();
}
}
As you can see I want to add a Button and a TextView after I inflate a View and show it with an Animation in a PopupWindow. I would like to add some Views to the LinearLayout inside the ScrollView. This is for testing purposes, later I want to Add full LinearLayouts to the LinearLayout inside the ScrollView.
The Animation works perfectly. I just can't add some Views programmatically. Everything I get is a NullPointerException in the Line where I try to add the views.
I appreciate your help.
Try this way,hope this will help you to solve your problem.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button popUpButton = (Button) findViewById(R.id.open);
int[] location = new int[2];
popUpButton.getLocationOnScreen(location);
// Initialize the Point with x, and y positions
p = new Point();
p.x = location[0];
p.y = location[1];
popUpButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if (p != null)
showPopup(p);
}
});
}
// The method that displays the popup_layout.
private void showPopup(Point p) {
Rect rectgle = new Rect();
Window window = getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
int popupWidth = this.getResources().getDisplayMetrics().widthPixels;
int popupHeight = this.getResources().getDisplayMetrics().heightPixels / 4;
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) findViewById(R.id.popupLinearLayout);
View layout = LayoutInflater.from(this).inflate(R.layout.popup_layout, viewGroup);
// Creating the PopupWindow
popup = new PopupWindow(this);
popup.setContentView(layout);
popup.setWidth(popupWidth);
popup.setHeight(popupHeight);
popup.setOutsideTouchable(true);
popup.setFocusable(true);
// popup.setAnimationStyle(R.style.PopupWindowAnimation);
// Some offset to align the popup_layout a bit to the right, and a bit down,
// relative to button's position.
int OFFSET_X = 0;
int OFFSET_Y = 0;
// Clear the default translucent background
popup.setBackgroundDrawable(new BitmapDrawable());
// Displaying the popup_layout at the specified location, + offsets.
popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);
// add LInearLayout
myLInearLayout = (LinearLayout) layout.findViewById(R.id.ll_horizontal);
// add LayoutParams
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT);
myLInearLayout.setOrientation(LinearLayout.HORIZONTAL);
// add textView
valueTV = new TextView(this);
valueTV.setText("The developer world is yours");
valueTV.setId(5);
valueTV.setLayoutParams(params);
// add Button
valueB = new Button(this);
valueB.setText("thedeveloperworldisyours");
valueB.setId(5);
// add the textView and the Button to LinearLayout
myLInearLayout.addView(valueTV);
myLInearLayout.addView(valueB);
}
I figured out how to do it. I just had to inflate the popup.xml to be able to get a reference to the LinearLayout I wanted to customize.
Here is the code if someone is facing the same problem:
public class MainActivity extends Activity {
private Point p;
private PopupWindow popup;
private Button popUpButton;
private TextView valueTV;
private Button valueB;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
popUpButton = (Button) findViewById(R.id.open);
popUpButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (p != null)
showPopup(MainActivity.this, p);
}
});
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
int[] location = new int[2];
Button button = (Button) findViewById(R.id.open);
button.getLocationOnScreen(location);
// Initialize the Point with x, and y positions
p = new Point();
p.x = location[0];
p.y = location[1];
}
// The method that displays the popup.
private void showPopup(final Activity context, Point p) {
Rect rectgle = new Rect();
Window window = getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
int popupWidth = this.getResources().getDisplayMetrics().widthPixels;
int popupHeight = this.getResources().getDisplayMetrics().heightPixels / 4;
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View contentView = inflater.inflate(R.layout.popup_layout, null);
ViewGroup dlgView = (ViewGroup) contentView
.findViewById(R.id.ll_horizontal);
// Creating the PopupWindow
popup = new PopupWindow(context);
popup.setContentView(contentView);
popup.setWidth(popupWidth);
popup.setHeight(popupHeight);
popup.setFocusable(true);
popup.setAnimationStyle(R.style.PopupWindowAnimation);
// Some offset to align the popup a bit to the right, and a bit down,
// relative to button's position.
int OFFSET_X = 0;
int OFFSET_Y = 0;
// Clear the default translucent background
popup.setBackgroundDrawable(new BitmapDrawable());
// add textView
valueTV = new TextView(this);
valueTV.setText("The developer world is yours");
valueTV.setId(5);
// add Button
valueB = new Button(this);
valueB.setText("thedeveloperworldisyours");
valueB.setId(5);
// add the textView and the Button to LinearLayout
dlgView.addView(valueTV);
dlgView.addView(valueB);
// Displaying the popup at the specified location, + offsets.
popup.showAtLocation(popUpButton, Gravity.NO_GRAVITY, p.x + OFFSET_X,
p.y + OFFSET_Y);
}
}

Relative layout parent center true

I'm now implementing to drag & drop a relative_layout . I have a class that extends Relativelayout.
when I drag the relative layout to another layout , I would like to place the relative layout automatically in the center of the container (Relative Layout) from coding like(android:layout_centerInParent="true" from xml ).
This is my class extend Relative layout :
public class Test extends RelativeLayout {
ImageView image2;
public RL(Context context){
super(context);
TextView txtName= new TextView(context);
TextView bac_Number= new TextView(context);
ImageView image = new ImageView(context);
ImageView image1 = new ImageView (context);
ImageView image2 = new ImageView(context);
txtName.setText("First");
// bac.setId(1);
txtName.setText("Aung Aung");
image.setImageResource(R.drawable.fcb);
image1.setImageResource(R.drawable.upload);
image2.setImageResource(R.drawable.guest);
bac_Number.setText("8");
RelativeLayout.LayoutParams lpSecond = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
addView(txtName, lpSecond);
RelativeLayout.LayoutParams lpFirst = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//lpFirst.addRule(RelativeLayout.RIGHT_OF, second.getId());
addView(bac_Number, lpFirst);
RelativeLayout.LayoutParams lpThird = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
addView(image, lpThird);
}
}
This is the activity I called above class
public class MainActivity extends Activity {
Button btn_Choose;
int p;
String[] series = {"Photo","Name","Back Number"};
String[] series1 = {"myPhoto","GuestPhoto","Default"};
GridLayout layoutmain;
private static final String IMAGEVIEW_TAG = "The Android Logo";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RL r = new RL(this);
RL r1 = new RL(this);
RL r2 = new RL(this);
RL r3 = new RL(this);
RL r4 = new RL(this);
setContentView(R.layout.activity_main);
layoutmain = (GridLayout)findViewById(R.id.layoutmain);
layoutmain.addView(r);
r.setGravity(Gravity.CENTER);
layoutmain.addView(r1);
layoutmain.addView(r2);
layoutmain.addView(r3);
layoutmain.addView(r4);
btn_Choose = (Button)findViewById(R.id.btnselect);
r.setTag(IMAGEVIEW_TAG);
r1.setTag(IMAGEVIEW_TAG);
r2.setTag(IMAGEVIEW_TAG);
r3.setTag(IMAGEVIEW_TAG);
r4.setTag(IMAGEVIEW_TAG);
// myImage.setOnLongClickListener(new MyClickListener());
r.setOnLongClickListener(new MyClickListener());
r1.setOnLongClickListener(new MyClickListener());
r2.setOnLongClickListener(new MyClickListener());
r3.setOnLongClickListener(new MyClickListener());
r4.setOnLongClickListener(new MyClickListener());
findViewById(R.id.toplinear).setOnDragListener(new MyDragListener());
findViewById(R.id.bottomlinear).setOnDragListener(new MyDragListener());
findViewById(R.id.bottomlinear1).setOnDragListener(new MyDragListener());
findViewById(R.id.layoutlinear).setOnDragListener(new MyDragListener());
findViewById(R.id.layoutmain).setOnDragListener(new MyDragListener());
}
private final class MyClickListener implements OnLongClickListener {
#Override
public boolean onLongClick(View view) {
// TODO Auto-generated method stub
ClipData.Item item = new ClipData.Item((CharSequence)view.getTag());
String[] mimeTypes = { ClipDescription.MIMETYPE_TEXT_PLAIN };
ClipData data = new ClipData(view.getTag().toString(), mimeTypes, item);
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag( data,
shadowBuilder,
view,
0
);
return true;
}
}
class MyDragListener implements OnDragListener {
Drawable normalShape = getResources().getDrawable(R.drawable.normal_shape);
Drawable targetShape = getResources().getDrawable(R.drawable.target_shape);
#Override
public boolean onDrag(View v, DragEvent event) {
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
Toast.makeText(getApplicationContext(), "Start Drag ", Toast.LENGTH_LONG).show();
break;
case DragEvent.ACTION_DRAG_ENTERED:
v.setBackground(targetShape); //change the shape of the view
break;
case DragEvent.ACTION_DRAG_EXITED:
v.setBackground(normalShape); //change the shape of the view back to normal
break;
case DragEvent.ACTION_DROP:
if(v.getClass().toString().equals("class android.widget.RelativeLayout")){
Log.i("class","Relative");
View view = (View) event.getLocalState();
ViewGroup viewgroup = (ViewGroup) view.getParent();
viewgroup.removeView(view);
RelativeLayout containView = (RelativeLayout) v;
containView.addView(view);
view.setVisibility(View.VISIBLE);
}
else {
Log.i("CLAss", v.getClass()+"Cant drop");
View view = (View) event.getLocalState();
view.setVisibility(View.VISIBLE);
Context context = getApplicationContext();
Toast.makeText(context, "You can't drop the image here",
Toast.LENGTH_LONG).show();
break;
}
break;
case DragEvent.ACTION_DRAG_ENDED:
v.setBackground(normalShape); //go back to normal shape
default:
break;
}
return true;
}
}
public void goClick(View v){
if (v.getId() == R.id.btnselect){
RotationPickerDialog dp = new RotationPickerDialog(this, series, new RotationPickerListener() {
#Override
public void OnDoneButton(Dialog rotationdialog, String r, int position) {
// TODO Auto-generated method stub
rotationdialog.dismiss();
btn_Choose.setText(r);
p = position ;
}
});
WindowManager.LayoutParams WMLP = dp.getWindow().getAttributes();
WMLP.x = 100;
WMLP.y = 250;
dp.getWindow().setAttributes(WMLP);
dp.show();
Log.i("p", p+"");
Toast.makeText(this, "Opponent Team Name", Toast.LENGTH_LONG).show();
}
}
}
You can add rules to RelativeLayout.LayoutParams:
RelativeLayout.LayoutParams lp=(RelativeLayout.LayoutParams)r.getLayoutParams();
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
Try this .....
Rl.setGravity(RelativeLayout.CENTER_IN_PARENT);

Pop up android app

I have a pop up that displays an image upon clicking a button. I got to display the pop up but the image is so small and I want it to occupy the 90% of the screen and add a close button to it.
Here is my code:
PopupWindow popUp;
LinearLayout layout;
TextView tv;
LayoutParams params;
ImageView imageView;
boolean click = true;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bmi);
popUp = new PopupWindow(this);
layout = new LinearLayout(this);
tv = new TextView(this);
imageView = new ImageView(this);
imageView.setImageResource(R.drawable.animalbite);
WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int fullScreenWidth = display.getWidth();
int dialogWidth = (int) (fullScreenWidth * 0.9);
WindowManager.LayoutParams params = getWindow().getAttributes();
params.height = LayoutParams.WRAP_CONTENT;
params.width = dialogWidth;
getWindow().setAttributes(params);
imageView.setLayoutParams(params);
//tv.setText("Hi this is a sample text for popup window");
layout.addView(imageView, params);
popUp.setContentView(layout);
Button ViewBmi = (Button) findViewById(R.id.ViewBmi);
ViewBmi.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (click) {
popUp.showAtLocation(layout, Gravity.CENTER, 10 ,10);
click = false;
} else {
popUp.dismiss();
click = true;
}
}
});
}
Here is the display:
ANy ideas?
Try this...
PopupWindow popUp;
LinearLayout layout;
TextView tv;
LayoutParams params,params2;
ImageView imageView;
boolean click = true;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Display display = getWindowManager().getDefaultDisplay();
final int height = (display.getHeight()*90)/100;
final int width = (display.getWidth()*90)/100;
popUp = new PopupWindow(this);
layout = new LinearLayout(this);
tv = new TextView(this);
imageView = new ImageView(this);
imageView.setImageResource(R.drawable.ic_launcher);
params = new LayoutParams(width,height-50);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(imageView, params);
Button button = new Button(this);
button.setText("Close");
params2 = new LayoutParams(100,50);
layout.addView(button,params2);
popUp.setContentView(layout);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
popUp.dismiss();
click = true;
}
});
Button ViewBmi = (Button) findViewById(R.id.button1);
ViewBmi.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (click) {
popUp.showAtLocation(layout, Gravity.CENTER, 10, 10);
popUp.update(10, 20, width,height);
click = false;
} else {
popUp.dismiss();
click = true;
}
}
});
}
Try This....
Button button = new Button(this);
button.setText("Close");
params2 = new LayoutParams(100,50);
--->params2.setMargins(100, 0, 0, 0);
layout.addView(button,params2);
I'd do it like this:
height = (int)(0.9*getWindow().getWindowManager().getDefaultDisplay().getHeight());
params.height = height;
Try:
params = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
...
layout.addView(imageView, params);
This way the ImageView will be as wide, as possible.
If the underlying dialog is not wide enough, then you can set it's width also full screen:
WindowManager.LayoutParams params = getWindow().getAttributes();
params.height = LayoutParams.WRAP_CONTENT;
params.width = LayoutParams.MATCH_PARENT;
getWindow().setAttributes(params);
Or if you want it to be for example 90% of the screen, then:
// get the width of the whole screen
Display display = getWindow().getDefaultDisplay();
int fullScreenWidth = display.getWidth();
//then set 90% of it to the width of the Dialog:
int dialogWidth = (int) fullScreenWidth * 0.9);
WindowManager.LayoutParams params = getWindow().getAttributes();
params.height = LayoutParams.WRAP_CONTENT;
params.width = dialogWidth;
getWindow().setAttributes(params);

Categories

Resources