I am trying to display contents of a ListView in a PopupWindow that grows as the List is populated. It works fine except that the elements shift as the list grows until it stops adding new elements. Is there a way to keep the position of the window fixed as it grows downward on the screen? I am new to Android programming so any input is appreciated. Thanks!
private void initView(View v) {
LayoutInflater inflater = (LayoutInflater)
getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popupView = inflater.inflate(R.layout.popup_layout, null);
DisplayMetrics displaymetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().
getMetrics(displaymetrics);
int width = (int) ((int)displaymetrics.widthPixels * 0.9);
popupWindow = new PopupWindow(popupView,width,
ViewGroup.LayoutParams.WRAP_CONTENT);
m = (MainActivity) getActivity();
m.layout_main.getForeground().setAlpha(220);
btnScan = (Button) popupView.findViewById(R.id.scan);
btncancel = (Button) popupView.findViewById(R.id.close);
popupWindow.setOutsideTouchable(false);
popupWindow.setFocusable(true);
int loc_int[] = new int[2];
if (v == null)
try
{
v.getLocationOnScreen(loc_int);
} catch (NullPointerException npe)
{
//when the view doesn't exist on screen anymore.
}
Rect location = new Rect();
location.left = loc_int[0];
location.top = loc_int[1] ;
location.right = location.left + v.getWidth()/2;
location.bottom = location.top - view2.getHeight()/2 ;
popupWindow.showAtLocation(view2, Gravity.CENTER,
view2.getWidth()/2, location.top);
popupWindow.showAsDropDown(view2);
listDevicesFound = (ListView) popupView.findViewById(R.id.devicelist);
btAdapter = BluetoothAdapter.getDefaultAdapter();
adtDevice = new ArrayAdapter<String>(getActivity(),R.layout.list_text,
lsDevice);
listDevicesFound.setAdapter(adtDevice);
listDevicesFound.setOnItemClickListener(new onIntemClick());
}
I ended up fixing the width and height of the window using a fraction of width and height obtained using:
DisplayMetrics metrics = this.getResources().getDisplayMetrics();
MyApplication.width = metrics.widthPixels;
MyApplication.height = metrics.heightPixels;
With a fixed height the PopupWindow no more shifts with new additions to the List.
Related
I have a problem in relation to the relative location of Buttons in different devices. I have done a short application to show it. The Button, in relation to a red point, behaves as desire when dealing with wide measures (coordinate x). However, I don’t know why the same Button is progressively scrolling down from the red point (coordinate y) as I choose a smaller device. I choose the right size of the image for the different devices and for both, wide and height, I also choose the relative measure of the screen:
rel_btn.width = 4*width/100;
rel_btn.height = 7*height/100;
I show you the code and the resulting screen of the xlarge screen in case someone can help me:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//width and height of the screen
DisplayMetrics metrics = new DisplayMetrics();
WindowManager windowManager = (WindowManager) MainActivity.this.getSystemService(Context.WINDOW_SERVICE);
if (windowManager != null) {
windowManager.getDefaultDisplay().getMetrics(metrics);
}
int width = metrics.widthPixels;
int height = metrics.heightPixels;
RelativeLayout layout = (RelativeLayout) findViewById(R.id.test); // id del XML
RelativeLayout.LayoutParams rel_btn = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
rel_btn.width = 4*width/100; rel_btn.height = 7*height/100;
// x, y of the BUTTON
rel_btn.leftMargin = 15*width/100; rel_btn.topMargin = 66*height/100;
Button btnTag = new Button(this);
btnTag.setLayoutParams(rel_btn);
btnTag.setBackgroundColor(Color.TRANSPARENT);
btnTag.setId(0); // writes the ID
btnTag.setOnClickListener(prueba);
layout.addView(btnTag);
}
private View.OnClickListener prueba = new View.OnClickListener() {
#Override
public void onClick(View view) {
//width of the screen
DisplayMetrics metrics = new DisplayMetrics();
WindowManager windowManager = (WindowManager) MainActivity.this.getSystemService(Context.WINDOW_SERVICE);
if (windowManager != null) {
windowManager.getDefaultDisplay().getMetrics(metrics);
}
int width = metrics.widthPixels;
Button button = (Button) view;
GradientDrawable drawable = new GradientDrawable();
drawable.setShape(GradientDrawable.RECTANGLE);
drawable.setStroke(Math.round((float) (0.6*width/100)), Color.BLUE);
drawable.setColor(Color.TRANSPARENT);
button.setBackgroundDrawable(drawable);
}
};
}
I am trying to create a popup when clicking on ListView image with the same image just enlarged, this code works fine to me but I tried to position the View "vv" at the middle of the screen but the popup is still at (0,0)..
rowView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (!popu) {
View popupView = inflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
TextView txt = (TextView) popupView.findViewById(R.id.texttt);
ImageButton img = (ImageButton) popupView.findViewById(R.id.imgg);
txt.setText(imageId[position]);
img.setImageResource(imageId[position]);
View vv = new View(context);
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
vv.setX(width / 4);
vv.setY(height / 4);
img.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
popupWindow.dismiss();
popu = false;
}
});
popupWindow.showAsDropDown(vv, 50, -30);
popu = true;
}
}
});
I am not on an activity, it is my customadapter class..
EDIT:
Fixed, see Yasin Kaçmaz answer.
Here the ImageView is displaying only at one position, after closing the activity the next time the activity is opened the ImageView will be on another position... I want to display the ImageView randomly at diff position on the same activity itself. The image view should appear on one point suddenly the next second ImageView should disappear from that position and appear on the next position. How can i do it?
public class page2 extends ActionBarActivity {
ImageView b2;
int count = 0;
Handler handler = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page2);
Intent c = getIntent();
String name = c.getStringExtra("t");
Toast.makeText(getApplicationContext(), name, Toast.LENGTH_SHORT).show();
b2 = (ImageView) findViewById(R.id.redball);
AbsoluteLayout.LayoutParams absParams =
(AbsoluteLayout.LayoutParams)b2.getLayoutParams();
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int width = displaymetrics.widthPixels;
int height = displaymetrics.heightPixels;
Random r = new Random();
absParams.x = r.nextInt(width ) ;
absParams.y = r.nextInt(height );
b2.setLayoutParams(absParams);
Animation animation = AnimationUtils.loadAnimation(page2.this, R.anim.fade);
// Animation animation1 = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.activity_move);
b2.startAnimation(animation);
// b2.startAnimation(animation1);
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
count = count + 1;
}
});
handler = new Handler();
final Runnable t = new Runnable() {
public void run() {
Intent d = new Intent(getApplicationContext(), Page3.class);
d.putExtra("count", count);
d.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(d);
}
};
handler.postDelayed(t, 4000);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_page2, menu);
return true;
}
Don't use AbsoluteLayout, Why not use a custom view draw it?
You can achieve this by using Imageview within FrameLayout. Just change the layoutParams of the image to change its position.
As I understand it, you want that each time the activity is opened, so f you dont want to actually view to the user that the ImageView moves, why are you using Animation? You may just dynamically add the ImageView to the activity each time, and each time assign it different Margin attributes.
LinearLayout layout = (LinearLayout) view.findViewById(R.id.linear);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
params.setMargins(0, 0, 10, 0); //substitute parameters for left, top, right, bottom
ImageView iv = new ImageView(getActivity());
iv.setLayoutParams(params);
iv.setImageResource(R.drawable.yourimage);
layout.addView(iv);
for calucation
ContainerHeight = blinkerContainer.getHeight(); // total height of screen
ContainerWidth = blinkerContainer.getWidth(); //total width
blinkerHeight = blinkView.getHeight();
blinkerWidth = blinkView.getWidth();
minTopMargin = 30;
minLeftMargin = 30;
maxTopMargin = ContainerHeight - blinkerHeight - 30;
maxLeftMargin = ContainerWidth - blinkerWidth - 30;
for positioning
LinearLayout.LayoutParams params = (android.widget.LinearLayout.LayoutParams) blinkView
.getLayoutParams();
params.leftMargin = minLeftMargin
+ new Random().nextInt(maxLeftMargin - minLeftMargin);
params.topMargin = minTopMargin
+ new Random().nextInt(maxTopMargin - minTopMargin);
and you can use AlaramManager for Scheduling
Your solution is almost correct. Unfortunately, it looks like you're restarting the activity from your timer. Instead, you should just trigger the redraw.
This question has a couple of solutions on how to create a recurring timer. The solution with runOnUiThread() should allow you to execute the randomisation and re-displaying of the ImageView.
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;
}
I use the following code to draw GraphView on LinearLayout,whose parent is a RelativeLayout
the code for drawing GraphView on LinearLayout
mLinearView = (LinearLayout) findViewById(R.id.LinearView);//ChildLayout
mRelativeView = (RelativeLayout) findViewById(R.id.RelativeLay);//ParentLayout
YearXYGraph mYearGraph = new YearXYGraph();
final GraphicalView mGraphView = mYearGraph.execution(
MainActivity.this, mGirlHeightYearFirstGrade,
mGirlHeightYearSecondGrade,
mGirlHeightYearThirdGrade,
mGirlHeightYearFourthGrade,
mGirlHeightYearFifthGrade, mGirlWeightYearFirstGrade,
mGirlWeightYearSecondGrade, mGirlWeightYearThirdGrade,
mGirlWeightYearFourthGrade, mGirlWeightYearFifthGrade);
mLinearView.addView(mGraphView);
onclicking LinearLayout I'm Showing a popup,code follows
mGraphView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
double[] xy = mGraphView.toRealPoint(0);
p = new Point();
p.x = (int) xy[0];
p.y = (int) xy[1];
if (p != null){
showPopup(MainActivity.this, p);
}
}
});
Im also using onClick event on ParentLayout to get some coordinate
mRelativeView.setOnTouchListener(new RelativeLayout.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
System.out.println("in--");
p1 = new Point();
p1.x = (int) event.getX();
p1.y = (int) event.getY();
System.out.println("x--"+p1.x);
return false;
}
});
code for Showing popup
// The method that displays the popup.
private void showPopup(final Activity context, Point p) {
int popupWidth = 75;
int popupHeight = 40;
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup);
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup);
// Creating the PopupWindow
final PopupWindow popup = new PopupWindow(context);
popup.setContentView(layout);
popup.setWidth(popupWidth);
popup.setHeight(popupHeight);
popup.setFocusable(true);
Rect location = new Rect();
location.left =p1.x;
location.top = p1.y;
location.right = location.left + popup.getWidth();
location.bottom = location.top + popup.getHeight();
/* // Some offset to align the popup a bit to the right, and a bit down, relative to button's position.
int OFFSET_X = 30;
int OFFSET_Y = 30;*/
// Clear the default translucent background
popup.setBackgroundDrawable(new BitmapDrawable());
// Displaying the popup at the specified location, + offsets.
// popup.showAtLocation(layout, Gravity.TOP|Gravity.LEFT, location.left , location.bottom );
popup.showAtLocation(layout, Gravity.TOP|Gravity.LEFT, location.left , location.bottom );
// Getting a reference to Close button, and close the popup when clicked.
TextView firstView = (TextView) layout.findViewById(R.id.textView1);
firstView.setText(p.x + " , " +p.y);
firstView.setTextColor(Color.WHITE);
}
problem is onClickEvent not detected on ParentLayout,,Child Layout detects OnClick,please provide some suggestions